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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 28 | #include "compat.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 29 | #include "ctree.h" |
| 30 | #include "extent_map.h" |
| 31 | #include "disk-io.h" |
| 32 | #include "transaction.h" |
| 33 | #include "print-tree.h" |
| 34 | #include "volumes.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 35 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 36 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 37 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 38 | #include "math.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 39 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 40 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 41 | struct btrfs_root *root, |
| 42 | struct btrfs_device *device); |
| 43 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 44 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
| 45 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 46 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 47 | static DEFINE_MUTEX(uuid_mutex); |
| 48 | static LIST_HEAD(fs_uuids); |
| 49 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 50 | static void lock_chunks(struct btrfs_root *root) |
| 51 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 52 | mutex_lock(&root->fs_info->chunk_mutex); |
| 53 | } |
| 54 | |
| 55 | static void unlock_chunks(struct btrfs_root *root) |
| 56 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 57 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 58 | } |
| 59 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 60 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 61 | { |
| 62 | struct btrfs_device *device; |
| 63 | WARN_ON(fs_devices->opened); |
| 64 | while (!list_empty(&fs_devices->devices)) { |
| 65 | device = list_entry(fs_devices->devices.next, |
| 66 | struct btrfs_device, dev_list); |
| 67 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 68 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 69 | kfree(device); |
| 70 | } |
| 71 | kfree(fs_devices); |
| 72 | } |
| 73 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 74 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 75 | { |
| 76 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 77 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 78 | while (!list_empty(&fs_uuids)) { |
| 79 | fs_devices = list_entry(fs_uuids.next, |
| 80 | struct btrfs_fs_devices, list); |
| 81 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 82 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 83 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 86 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 87 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 88 | { |
| 89 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 90 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 91 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 92 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 93 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 94 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 95 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 100 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 101 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 102 | struct btrfs_fs_devices *fs_devices; |
| 103 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 104 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 105 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 106 | return fs_devices; |
| 107 | } |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 111 | static int |
| 112 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 113 | int flush, struct block_device **bdev, |
| 114 | struct buffer_head **bh) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 119 | |
| 120 | if (IS_ERR(*bdev)) { |
| 121 | ret = PTR_ERR(*bdev); |
| 122 | printk(KERN_INFO "btrfs: open %s failed\n", device_path); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | if (flush) |
| 127 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 128 | ret = set_blocksize(*bdev, 4096); |
| 129 | if (ret) { |
| 130 | blkdev_put(*bdev, flags); |
| 131 | goto error; |
| 132 | } |
| 133 | invalidate_bdev(*bdev); |
| 134 | *bh = btrfs_read_dev_super(*bdev); |
| 135 | if (!*bh) { |
| 136 | ret = -EINVAL; |
| 137 | blkdev_put(*bdev, flags); |
| 138 | goto error; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | |
| 143 | error: |
| 144 | *bdev = NULL; |
| 145 | *bh = NULL; |
| 146 | return ret; |
| 147 | } |
| 148 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 149 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 150 | struct bio *head, struct bio *tail) |
| 151 | { |
| 152 | |
| 153 | struct bio *old_head; |
| 154 | |
| 155 | old_head = pending_bios->head; |
| 156 | pending_bios->head = head; |
| 157 | if (pending_bios->tail) |
| 158 | tail->bi_next = old_head; |
| 159 | else |
| 160 | pending_bios->tail = tail; |
| 161 | } |
| 162 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 163 | /* |
| 164 | * we try to collect pending bios for a device so we don't get a large |
| 165 | * number of procs sending bios down to the same device. This greatly |
| 166 | * improves the schedulers ability to collect and merge the bios. |
| 167 | * |
| 168 | * But, it also turns into a long list of bios to process and that is sure |
| 169 | * to eventually make the worker thread block. The solution here is to |
| 170 | * make some progress and then put this work struct back at the end of |
| 171 | * the list if the block device is congested. This way, multiple devices |
| 172 | * can make progress from a single worker thread. |
| 173 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 174 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 175 | { |
| 176 | struct bio *pending; |
| 177 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 178 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 179 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 180 | struct bio *tail; |
| 181 | struct bio *cur; |
| 182 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 183 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 184 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 185 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 186 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 187 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 188 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 189 | struct blk_plug plug; |
| 190 | |
| 191 | /* |
| 192 | * this function runs all the bios we've collected for |
| 193 | * a particular device. We don't want to wander off to |
| 194 | * another device without first sending all of these down. |
| 195 | * So, setup a plug here and finish it off before we return |
| 196 | */ |
| 197 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 198 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 199 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 200 | fs_info = device->dev_root->fs_info; |
| 201 | limit = btrfs_async_submit_limit(fs_info); |
| 202 | limit = limit * 2 / 3; |
| 203 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 204 | loop: |
| 205 | spin_lock(&device->io_lock); |
| 206 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 207 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 208 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 209 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 210 | /* take all the bios off the list at once and process them |
| 211 | * later on (without the lock held). But, remember the |
| 212 | * tail and other pointers so the bios can be properly reinserted |
| 213 | * into the list if we hit congestion |
| 214 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 215 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 216 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 217 | force_reg = 1; |
| 218 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 219 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 220 | force_reg = 0; |
| 221 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 222 | |
| 223 | pending = pending_bios->head; |
| 224 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 225 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * if pending was null this time around, no bios need processing |
| 229 | * at all and we can stop. Otherwise it'll loop back up again |
| 230 | * and do an additional check so no bios are missed. |
| 231 | * |
| 232 | * device->running_pending is used to synchronize with the |
| 233 | * schedule_bio code. |
| 234 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 235 | if (device->pending_sync_bios.head == NULL && |
| 236 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 237 | again = 0; |
| 238 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 239 | } else { |
| 240 | again = 1; |
| 241 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 242 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 243 | |
| 244 | pending_bios->head = NULL; |
| 245 | pending_bios->tail = NULL; |
| 246 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 247 | spin_unlock(&device->io_lock); |
| 248 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 249 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 250 | |
| 251 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 252 | /* we want to work on both lists, but do more bios on the |
| 253 | * sync list than the regular list |
| 254 | */ |
| 255 | if ((num_run > 32 && |
| 256 | pending_bios != &device->pending_sync_bios && |
| 257 | device->pending_sync_bios.head) || |
| 258 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 259 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 260 | spin_lock(&device->io_lock); |
| 261 | requeue_list(pending_bios, pending, tail); |
| 262 | goto loop_lock; |
| 263 | } |
| 264 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 265 | cur = pending; |
| 266 | pending = pending->bi_next; |
| 267 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 268 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 269 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 270 | waitqueue_active(&fs_info->async_submit_wait)) |
| 271 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 272 | |
| 273 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 274 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 275 | /* |
| 276 | * if we're doing the sync list, record that our |
| 277 | * plug has some sync requests on it |
| 278 | * |
| 279 | * If we're doing the regular list and there are |
| 280 | * sync requests sitting around, unplug before |
| 281 | * we add more |
| 282 | */ |
| 283 | if (pending_bios == &device->pending_sync_bios) { |
| 284 | sync_pending = 1; |
| 285 | } else if (sync_pending) { |
| 286 | blk_finish_plug(&plug); |
| 287 | blk_start_plug(&plug); |
| 288 | sync_pending = 0; |
| 289 | } |
| 290 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 291 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 292 | num_run++; |
| 293 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 294 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 295 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * we made progress, there is more work to do and the bdi |
| 299 | * is now congested. Back off and let other work structs |
| 300 | * run instead |
| 301 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 302 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 303 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 304 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 305 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 306 | ioc = current->io_context; |
| 307 | |
| 308 | /* |
| 309 | * the main goal here is that we don't want to |
| 310 | * block if we're going to be able to submit |
| 311 | * more requests without blocking. |
| 312 | * |
| 313 | * This code does two great things, it pokes into |
| 314 | * the elevator code from a filesystem _and_ |
| 315 | * it makes assumptions about how batching works. |
| 316 | */ |
| 317 | if (ioc && ioc->nr_batch_requests > 0 && |
| 318 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 319 | (last_waited == 0 || |
| 320 | ioc->last_waited == last_waited)) { |
| 321 | /* |
| 322 | * we want to go through our batch of |
| 323 | * requests and stop. So, we copy out |
| 324 | * the ioc->last_waited time and test |
| 325 | * against it before looping |
| 326 | */ |
| 327 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 328 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 329 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 330 | continue; |
| 331 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 332 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 333 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 334 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 335 | |
| 336 | spin_unlock(&device->io_lock); |
| 337 | btrfs_requeue_work(&device->work); |
| 338 | goto done; |
| 339 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 340 | /* unplug every 64 requests just for good measure */ |
| 341 | if (batch_run % 64 == 0) { |
| 342 | blk_finish_plug(&plug); |
| 343 | blk_start_plug(&plug); |
| 344 | sync_pending = 0; |
| 345 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 346 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 347 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 348 | cond_resched(); |
| 349 | if (again) |
| 350 | goto loop; |
| 351 | |
| 352 | spin_lock(&device->io_lock); |
| 353 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 354 | goto loop_lock; |
| 355 | spin_unlock(&device->io_lock); |
| 356 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 357 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 358 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 359 | } |
| 360 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 361 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 362 | { |
| 363 | struct btrfs_device *device; |
| 364 | |
| 365 | device = container_of(work, struct btrfs_device, work); |
| 366 | run_scheduled_bios(device); |
| 367 | } |
| 368 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 369 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 370 | struct btrfs_super_block *disk_super, |
| 371 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 372 | { |
| 373 | struct btrfs_device *device; |
| 374 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 375 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 376 | u64 found_transid = btrfs_super_generation(disk_super); |
| 377 | |
| 378 | fs_devices = find_fsid(disk_super->fsid); |
| 379 | if (!fs_devices) { |
Chris Mason | 515dc32 | 2008-05-16 13:30:15 -0400 | [diff] [blame] | 380 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 381 | if (!fs_devices) |
| 382 | return -ENOMEM; |
| 383 | INIT_LIST_HEAD(&fs_devices->devices); |
Chris Mason | b307571 | 2008-04-22 09:22:07 -0400 | [diff] [blame] | 384 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 385 | list_add(&fs_devices->list, &fs_uuids); |
| 386 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| 387 | fs_devices->latest_devid = devid; |
| 388 | fs_devices->latest_trans = found_transid; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 389 | mutex_init(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 390 | device = NULL; |
| 391 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 392 | device = __find_device(&fs_devices->devices, devid, |
| 393 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 394 | } |
| 395 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 396 | if (fs_devices->opened) |
| 397 | return -EBUSY; |
| 398 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 399 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 400 | if (!device) { |
| 401 | /* we can safely leave the fs_devices entry around */ |
| 402 | return -ENOMEM; |
| 403 | } |
| 404 | device->devid = devid; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 405 | device->dev_stats_valid = 0; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 406 | device->work.func = pending_bios_fn; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 407 | memcpy(device->uuid, disk_super->dev_item.uuid, |
| 408 | BTRFS_UUID_SIZE); |
Chris Mason | b248a41 | 2008-04-14 09:48:18 -0400 | [diff] [blame] | 409 | spin_lock_init(&device->io_lock); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 410 | |
| 411 | name = rcu_string_strdup(path, GFP_NOFS); |
| 412 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 413 | kfree(device); |
| 414 | return -ENOMEM; |
| 415 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 416 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 417 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 418 | |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 419 | /* init readahead state */ |
| 420 | spin_lock_init(&device->reada_lock); |
| 421 | device->reada_curr_zone = NULL; |
| 422 | atomic_set(&device->reada_in_flight, 0); |
| 423 | device->reada_next = 0; |
| 424 | INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 425 | INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 426 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 427 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 428 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 429 | mutex_unlock(&fs_devices->device_list_mutex); |
| 430 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 431 | device->fs_devices = fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 432 | fs_devices->num_devices++; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 433 | } else if (!device->name || strcmp(device->name->str, path)) { |
| 434 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 435 | if (!name) |
| 436 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 437 | rcu_string_free(device->name); |
| 438 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 439 | if (device->missing) { |
| 440 | fs_devices->missing_devices--; |
| 441 | device->missing = 0; |
| 442 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (found_transid > fs_devices->latest_trans) { |
| 446 | fs_devices->latest_devid = devid; |
| 447 | fs_devices->latest_trans = found_transid; |
| 448 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 449 | *fs_devices_ret = fs_devices; |
| 450 | return 0; |
| 451 | } |
| 452 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 453 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 454 | { |
| 455 | struct btrfs_fs_devices *fs_devices; |
| 456 | struct btrfs_device *device; |
| 457 | struct btrfs_device *orig_dev; |
| 458 | |
| 459 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 460 | if (!fs_devices) |
| 461 | return ERR_PTR(-ENOMEM); |
| 462 | |
| 463 | INIT_LIST_HEAD(&fs_devices->devices); |
| 464 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
| 465 | INIT_LIST_HEAD(&fs_devices->list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 466 | mutex_init(&fs_devices->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 467 | fs_devices->latest_devid = orig->latest_devid; |
| 468 | fs_devices->latest_trans = orig->latest_trans; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 469 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 470 | memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); |
| 471 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 472 | /* 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] | 473 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 474 | struct rcu_string *name; |
| 475 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 476 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 477 | if (!device) |
| 478 | goto error; |
| 479 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 480 | /* |
| 481 | * This is ok to do without rcu read locked because we hold the |
| 482 | * uuid mutex so nothing we touch in here is going to disappear. |
| 483 | */ |
| 484 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 485 | if (!name) { |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 486 | kfree(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 487 | goto error; |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 488 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 489 | rcu_assign_pointer(device->name, name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 490 | |
| 491 | device->devid = orig_dev->devid; |
| 492 | device->work.func = pending_bios_fn; |
| 493 | memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 494 | spin_lock_init(&device->io_lock); |
| 495 | INIT_LIST_HEAD(&device->dev_list); |
| 496 | INIT_LIST_HEAD(&device->dev_alloc_list); |
| 497 | |
| 498 | list_add(&device->dev_list, &fs_devices->devices); |
| 499 | device->fs_devices = fs_devices; |
| 500 | fs_devices->num_devices++; |
| 501 | } |
| 502 | return fs_devices; |
| 503 | error: |
| 504 | free_fs_devices(fs_devices); |
| 505 | return ERR_PTR(-ENOMEM); |
| 506 | } |
| 507 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 508 | void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 509 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 510 | struct btrfs_device *device, *next; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 511 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 512 | struct block_device *latest_bdev = NULL; |
| 513 | u64 latest_devid = 0; |
| 514 | u64 latest_transid = 0; |
| 515 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 516 | mutex_lock(&uuid_mutex); |
| 517 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 518 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 519 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 520 | if (device->in_fs_metadata) { |
| 521 | if (!latest_transid || |
| 522 | device->generation > latest_transid) { |
| 523 | latest_devid = device->devid; |
| 524 | latest_transid = device->generation; |
| 525 | latest_bdev = device->bdev; |
| 526 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 527 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 528 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 529 | |
| 530 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 531 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 532 | device->bdev = NULL; |
| 533 | fs_devices->open_devices--; |
| 534 | } |
| 535 | if (device->writeable) { |
| 536 | list_del_init(&device->dev_alloc_list); |
| 537 | device->writeable = 0; |
| 538 | fs_devices->rw_devices--; |
| 539 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 540 | list_del_init(&device->dev_list); |
| 541 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 542 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 543 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 544 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 545 | |
| 546 | if (fs_devices->seed) { |
| 547 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 548 | goto again; |
| 549 | } |
| 550 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 551 | fs_devices->latest_bdev = latest_bdev; |
| 552 | fs_devices->latest_devid = latest_devid; |
| 553 | fs_devices->latest_trans = latest_transid; |
| 554 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 555 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 556 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 557 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 558 | static void __free_device(struct work_struct *work) |
| 559 | { |
| 560 | struct btrfs_device *device; |
| 561 | |
| 562 | device = container_of(work, struct btrfs_device, rcu_work); |
| 563 | |
| 564 | if (device->bdev) |
| 565 | blkdev_put(device->bdev, device->mode); |
| 566 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 567 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 568 | kfree(device); |
| 569 | } |
| 570 | |
| 571 | static void free_device(struct rcu_head *head) |
| 572 | { |
| 573 | struct btrfs_device *device; |
| 574 | |
| 575 | device = container_of(head, struct btrfs_device, rcu); |
| 576 | |
| 577 | INIT_WORK(&device->rcu_work, __free_device); |
| 578 | schedule_work(&device->rcu_work); |
| 579 | } |
| 580 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 581 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 582 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 583 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 584 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 585 | if (--fs_devices->opened > 0) |
| 586 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 587 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 588 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 589 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 590 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 591 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 592 | |
| 593 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 594 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 595 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 596 | if (device->writeable) { |
| 597 | list_del_init(&device->dev_alloc_list); |
| 598 | fs_devices->rw_devices--; |
| 599 | } |
| 600 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 601 | if (device->can_discard) |
| 602 | fs_devices->num_can_discard--; |
| 603 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 604 | new_device = kmalloc(sizeof(*new_device), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 605 | BUG_ON(!new_device); /* -ENOMEM */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 606 | memcpy(new_device, device, sizeof(*new_device)); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 607 | |
| 608 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 609 | if (device->name) { |
| 610 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 611 | BUG_ON(device->name && !name); /* -ENOMEM */ |
| 612 | rcu_assign_pointer(new_device->name, name); |
| 613 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 614 | new_device->bdev = NULL; |
| 615 | new_device->writeable = 0; |
| 616 | new_device->in_fs_metadata = 0; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 617 | new_device->can_discard = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 618 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 619 | |
| 620 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 621 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 622 | mutex_unlock(&fs_devices->device_list_mutex); |
| 623 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 624 | WARN_ON(fs_devices->open_devices); |
| 625 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 626 | fs_devices->opened = 0; |
| 627 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 628 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 632 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 633 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 634 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 635 | int ret; |
| 636 | |
| 637 | mutex_lock(&uuid_mutex); |
| 638 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 639 | if (!fs_devices->opened) { |
| 640 | seed_devices = fs_devices->seed; |
| 641 | fs_devices->seed = NULL; |
| 642 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 643 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 644 | |
| 645 | while (seed_devices) { |
| 646 | fs_devices = seed_devices; |
| 647 | seed_devices = fs_devices->seed; |
| 648 | __btrfs_close_devices(fs_devices); |
| 649 | free_fs_devices(fs_devices); |
| 650 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 651 | return ret; |
| 652 | } |
| 653 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 654 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 655 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 656 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 657 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 658 | struct block_device *bdev; |
| 659 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 660 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 661 | struct block_device *latest_bdev = NULL; |
| 662 | struct buffer_head *bh; |
| 663 | struct btrfs_super_block *disk_super; |
| 664 | u64 latest_devid = 0; |
| 665 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 666 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 667 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 668 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 669 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 670 | flags |= FMODE_EXCL; |
| 671 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 672 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 673 | if (device->bdev) |
| 674 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 675 | if (!device->name) |
| 676 | continue; |
| 677 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 678 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 679 | &bdev, &bh); |
| 680 | if (ret) |
| 681 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 682 | |
| 683 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 684 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 685 | if (devid != device->devid) |
| 686 | goto error_brelse; |
| 687 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 688 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 689 | BTRFS_UUID_SIZE)) |
| 690 | goto error_brelse; |
| 691 | |
| 692 | device->generation = btrfs_super_generation(disk_super); |
| 693 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 694 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 695 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 696 | latest_bdev = bdev; |
| 697 | } |
| 698 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 699 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 700 | device->writeable = 0; |
| 701 | } else { |
| 702 | device->writeable = !bdev_read_only(bdev); |
| 703 | seeding = 0; |
| 704 | } |
| 705 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 706 | q = bdev_get_queue(bdev); |
| 707 | if (blk_queue_discard(q)) { |
| 708 | device->can_discard = 1; |
| 709 | fs_devices->num_can_discard++; |
| 710 | } |
| 711 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 712 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 713 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 714 | device->mode = flags; |
| 715 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 716 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 717 | fs_devices->rotating = 1; |
| 718 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 719 | fs_devices->open_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 720 | if (device->writeable) { |
| 721 | fs_devices->rw_devices++; |
| 722 | list_add(&device->dev_alloc_list, |
| 723 | &fs_devices->alloc_list); |
| 724 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 725 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 726 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 727 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 728 | error_brelse: |
| 729 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 730 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 731 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 732 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 733 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 734 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 735 | goto out; |
| 736 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 737 | fs_devices->seeding = seeding; |
| 738 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 739 | fs_devices->latest_bdev = latest_bdev; |
| 740 | fs_devices->latest_devid = latest_devid; |
| 741 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 742 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 743 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 748 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 749 | { |
| 750 | int ret; |
| 751 | |
| 752 | mutex_lock(&uuid_mutex); |
| 753 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 754 | fs_devices->opened++; |
| 755 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 756 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 757 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 758 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 759 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 763 | 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] | 764 | struct btrfs_fs_devices **fs_devices_ret) |
| 765 | { |
| 766 | struct btrfs_super_block *disk_super; |
| 767 | struct block_device *bdev; |
| 768 | struct buffer_head *bh; |
| 769 | int ret; |
| 770 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 771 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 772 | u64 total_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 773 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 774 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 775 | mutex_lock(&uuid_mutex); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 776 | ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 777 | if (ret) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 778 | goto error; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 779 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 780 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 781 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 782 | total_devices = btrfs_super_num_devices(disk_super); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 783 | if (disk_super->label[0]) { |
| 784 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 785 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 786 | printk(KERN_INFO "device label %s ", disk_super->label); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 787 | } else { |
Ilya Dryomov | 22b63a2 | 2011-02-09 16:05:31 +0200 | [diff] [blame] | 788 | printk(KERN_INFO "device fsid %pU ", disk_super->fsid); |
Stefan Behrens | d03f918 | 2012-11-05 13:10:49 +0000 | [diff] [blame] | 789 | } |
Roland Dreier | 119e10c | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 790 | printk(KERN_CONT "devid %llu transid %llu %s\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 791 | (unsigned long long)devid, (unsigned long long)transid, path); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 792 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 793 | if (!ret && fs_devices_ret) |
| 794 | (*fs_devices_ret)->total_devices = total_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 795 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 796 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 797 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 798 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 799 | return ret; |
| 800 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 801 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 802 | /* helper to account the used device space in the range */ |
| 803 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 804 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 805 | { |
| 806 | struct btrfs_key key; |
| 807 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 808 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 809 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 810 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 811 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 812 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 813 | struct extent_buffer *l; |
| 814 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 815 | *length = 0; |
| 816 | |
| 817 | if (start >= device->total_bytes) |
| 818 | return 0; |
| 819 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 820 | path = btrfs_alloc_path(); |
| 821 | if (!path) |
| 822 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 823 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 824 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 825 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 826 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 827 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 828 | |
| 829 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 830 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 831 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 832 | if (ret > 0) { |
| 833 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 834 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 835 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 836 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 837 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 838 | while (1) { |
| 839 | l = path->nodes[0]; |
| 840 | slot = path->slots[0]; |
| 841 | if (slot >= btrfs_header_nritems(l)) { |
| 842 | ret = btrfs_next_leaf(root, path); |
| 843 | if (ret == 0) |
| 844 | continue; |
| 845 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 846 | goto out; |
| 847 | |
| 848 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 849 | } |
| 850 | btrfs_item_key_to_cpu(l, &key, slot); |
| 851 | |
| 852 | if (key.objectid < device->devid) |
| 853 | goto next; |
| 854 | |
| 855 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 856 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 857 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 858 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 859 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 860 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 861 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 862 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 863 | dev_extent); |
| 864 | if (key.offset <= start && extent_end > end) { |
| 865 | *length = end - start + 1; |
| 866 | break; |
| 867 | } else if (key.offset <= start && extent_end > start) |
| 868 | *length += extent_end - start; |
| 869 | else if (key.offset > start && extent_end <= end) |
| 870 | *length += extent_end - key.offset; |
| 871 | else if (key.offset > start && key.offset <= end) { |
| 872 | *length += end - key.offset + 1; |
| 873 | break; |
| 874 | } else if (key.offset > end) |
| 875 | break; |
| 876 | |
| 877 | next: |
| 878 | path->slots[0]++; |
| 879 | } |
| 880 | ret = 0; |
| 881 | out: |
| 882 | btrfs_free_path(path); |
| 883 | return ret; |
| 884 | } |
| 885 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 886 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 887 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 888 | * @device: the device which we search the free space in |
| 889 | * @num_bytes: the size of the free space that we need |
| 890 | * @start: store the start of the free space. |
| 891 | * @len: the size of the free space. that we find, or the size of the max |
| 892 | * free space if we don't find suitable free space |
| 893 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 894 | * this uses a pretty simple search, the expectation is that it is |
| 895 | * called very infrequently and that a given device has a small number |
| 896 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 897 | * |
| 898 | * @start is used to store the start of the free space if we find. But if we |
| 899 | * don't find suitable free space, it will be used to store the start position |
| 900 | * of the max free space. |
| 901 | * |
| 902 | * @len is used to store the size of the free space that we find. |
| 903 | * But if we don't find suitable free space, it is used to store the size of |
| 904 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 905 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 906 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 907 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 908 | { |
| 909 | struct btrfs_key key; |
| 910 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 911 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 912 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 913 | u64 hole_size; |
| 914 | u64 max_hole_start; |
| 915 | u64 max_hole_size; |
| 916 | u64 extent_end; |
| 917 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 918 | u64 search_end = device->total_bytes; |
| 919 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 920 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 921 | struct extent_buffer *l; |
| 922 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 923 | /* FIXME use last free of some kind */ |
| 924 | |
| 925 | /* we don't want to overwrite the superblock on the drive, |
| 926 | * so we make sure to start at an offset of at least 1MB |
| 927 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 928 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 929 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 930 | max_hole_start = search_start; |
| 931 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 932 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 933 | |
| 934 | if (search_start >= search_end) { |
| 935 | ret = -ENOSPC; |
| 936 | goto error; |
| 937 | } |
| 938 | |
| 939 | path = btrfs_alloc_path(); |
| 940 | if (!path) { |
| 941 | ret = -ENOMEM; |
| 942 | goto error; |
| 943 | } |
| 944 | path->reada = 2; |
| 945 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 946 | key.objectid = device->devid; |
| 947 | key.offset = search_start; |
| 948 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 949 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 950 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 951 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 952 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 953 | if (ret > 0) { |
| 954 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 955 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 956 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 957 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 958 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 959 | while (1) { |
| 960 | l = path->nodes[0]; |
| 961 | slot = path->slots[0]; |
| 962 | if (slot >= btrfs_header_nritems(l)) { |
| 963 | ret = btrfs_next_leaf(root, path); |
| 964 | if (ret == 0) |
| 965 | continue; |
| 966 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 967 | goto out; |
| 968 | |
| 969 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 970 | } |
| 971 | btrfs_item_key_to_cpu(l, &key, slot); |
| 972 | |
| 973 | if (key.objectid < device->devid) |
| 974 | goto next; |
| 975 | |
| 976 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 977 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 978 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 979 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
| 980 | goto next; |
| 981 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 982 | if (key.offset > search_start) { |
| 983 | hole_size = key.offset - search_start; |
| 984 | |
| 985 | if (hole_size > max_hole_size) { |
| 986 | max_hole_start = search_start; |
| 987 | max_hole_size = hole_size; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * If this free space is greater than which we need, |
| 992 | * it must be the max free space that we have found |
| 993 | * until now, so max_hole_start must point to the start |
| 994 | * of this free space and the length of this free space |
| 995 | * is stored in max_hole_size. Thus, we return |
| 996 | * max_hole_start and max_hole_size and go back to the |
| 997 | * caller. |
| 998 | */ |
| 999 | if (hole_size >= num_bytes) { |
| 1000 | ret = 0; |
| 1001 | goto out; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1005 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1006 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1007 | dev_extent); |
| 1008 | if (extent_end > search_start) |
| 1009 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1010 | next: |
| 1011 | path->slots[0]++; |
| 1012 | cond_resched(); |
| 1013 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1014 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1015 | /* |
| 1016 | * At this point, search_start should be the end of |
| 1017 | * allocated dev extents, and when shrinking the device, |
| 1018 | * search_end may be smaller than search_start. |
| 1019 | */ |
| 1020 | if (search_end > search_start) |
| 1021 | hole_size = search_end - search_start; |
| 1022 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1023 | if (hole_size > max_hole_size) { |
| 1024 | max_hole_start = search_start; |
| 1025 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1026 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1027 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1028 | /* See above. */ |
| 1029 | if (hole_size < num_bytes) |
| 1030 | ret = -ENOSPC; |
| 1031 | else |
| 1032 | ret = 0; |
| 1033 | |
| 1034 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1035 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1036 | error: |
| 1037 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1038 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1039 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1040 | return ret; |
| 1041 | } |
| 1042 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1043 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1044 | struct btrfs_device *device, |
| 1045 | u64 start) |
| 1046 | { |
| 1047 | int ret; |
| 1048 | struct btrfs_path *path; |
| 1049 | struct btrfs_root *root = device->dev_root; |
| 1050 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1051 | struct btrfs_key found_key; |
| 1052 | struct extent_buffer *leaf = NULL; |
| 1053 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1054 | |
| 1055 | path = btrfs_alloc_path(); |
| 1056 | if (!path) |
| 1057 | return -ENOMEM; |
| 1058 | |
| 1059 | key.objectid = device->devid; |
| 1060 | key.offset = start; |
| 1061 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1062 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1063 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1064 | if (ret > 0) { |
| 1065 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1066 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1067 | if (ret) |
| 1068 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1069 | leaf = path->nodes[0]; |
| 1070 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1071 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1072 | struct btrfs_dev_extent); |
| 1073 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1074 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1075 | key = found_key; |
| 1076 | btrfs_release_path(path); |
| 1077 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1078 | } else if (ret == 0) { |
| 1079 | leaf = path->nodes[0]; |
| 1080 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1081 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1082 | } else { |
| 1083 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1084 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1085 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1086 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1087 | if (device->bytes_used > 0) { |
| 1088 | u64 len = btrfs_dev_extent_length(leaf, extent); |
| 1089 | device->bytes_used -= len; |
| 1090 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1091 | root->fs_info->free_chunk_space += len; |
| 1092 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1093 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1094 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1095 | if (ret) { |
| 1096 | btrfs_error(root->fs_info, ret, |
| 1097 | "Failed to remove dev extent item"); |
| 1098 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1099 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1100 | btrfs_free_path(path); |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1104 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1105 | struct btrfs_device *device, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1106 | u64 chunk_tree, u64 chunk_objectid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1107 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1108 | { |
| 1109 | int ret; |
| 1110 | struct btrfs_path *path; |
| 1111 | struct btrfs_root *root = device->dev_root; |
| 1112 | struct btrfs_dev_extent *extent; |
| 1113 | struct extent_buffer *leaf; |
| 1114 | struct btrfs_key key; |
| 1115 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1116 | WARN_ON(!device->in_fs_metadata); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1117 | path = btrfs_alloc_path(); |
| 1118 | if (!path) |
| 1119 | return -ENOMEM; |
| 1120 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1121 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1122 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1123 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1124 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1125 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1126 | if (ret) |
| 1127 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1128 | |
| 1129 | leaf = path->nodes[0]; |
| 1130 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1131 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1132 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1133 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1134 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1135 | |
| 1136 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 1137 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), |
| 1138 | BTRFS_UUID_SIZE); |
| 1139 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1140 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1141 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1142 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1143 | btrfs_free_path(path); |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 1147 | static noinline int find_next_chunk(struct btrfs_root *root, |
| 1148 | u64 objectid, u64 *offset) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1149 | { |
| 1150 | struct btrfs_path *path; |
| 1151 | int ret; |
| 1152 | struct btrfs_key key; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1153 | struct btrfs_chunk *chunk; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1154 | struct btrfs_key found_key; |
| 1155 | |
| 1156 | path = btrfs_alloc_path(); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 1157 | if (!path) |
| 1158 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1159 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1160 | key.objectid = objectid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1161 | key.offset = (u64)-1; |
| 1162 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 1163 | |
| 1164 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1165 | if (ret < 0) |
| 1166 | goto error; |
| 1167 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1168 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1169 | |
| 1170 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); |
| 1171 | if (ret) { |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1172 | *offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1173 | } else { |
| 1174 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1175 | path->slots[0]); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1176 | if (found_key.objectid != objectid) |
| 1177 | *offset = 0; |
| 1178 | else { |
| 1179 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1180 | struct btrfs_chunk); |
| 1181 | *offset = found_key.offset + |
| 1182 | btrfs_chunk_length(path->nodes[0], chunk); |
| 1183 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1184 | } |
| 1185 | ret = 0; |
| 1186 | error: |
| 1187 | btrfs_free_path(path); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1191 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1192 | { |
| 1193 | int ret; |
| 1194 | struct btrfs_key key; |
| 1195 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1196 | struct btrfs_path *path; |
| 1197 | |
| 1198 | root = root->fs_info->chunk_root; |
| 1199 | |
| 1200 | path = btrfs_alloc_path(); |
| 1201 | if (!path) |
| 1202 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1203 | |
| 1204 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1205 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1206 | key.offset = (u64)-1; |
| 1207 | |
| 1208 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1209 | if (ret < 0) |
| 1210 | goto error; |
| 1211 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1212 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1213 | |
| 1214 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, |
| 1215 | BTRFS_DEV_ITEM_KEY); |
| 1216 | if (ret) { |
| 1217 | *objectid = 1; |
| 1218 | } else { |
| 1219 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1220 | path->slots[0]); |
| 1221 | *objectid = found_key.offset + 1; |
| 1222 | } |
| 1223 | ret = 0; |
| 1224 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1225 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1226 | return ret; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * the device information is stored in the chunk root |
| 1231 | * the btrfs_device struct should be fully filled in |
| 1232 | */ |
| 1233 | int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1234 | struct btrfs_root *root, |
| 1235 | struct btrfs_device *device) |
| 1236 | { |
| 1237 | int ret; |
| 1238 | struct btrfs_path *path; |
| 1239 | struct btrfs_dev_item *dev_item; |
| 1240 | struct extent_buffer *leaf; |
| 1241 | struct btrfs_key key; |
| 1242 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1243 | |
| 1244 | root = root->fs_info->chunk_root; |
| 1245 | |
| 1246 | path = btrfs_alloc_path(); |
| 1247 | if (!path) |
| 1248 | return -ENOMEM; |
| 1249 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1250 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1251 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1252 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1253 | |
| 1254 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1255 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1256 | if (ret) |
| 1257 | goto out; |
| 1258 | |
| 1259 | leaf = path->nodes[0]; |
| 1260 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1261 | |
| 1262 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1263 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1264 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1265 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1266 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1267 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1268 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1269 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1270 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1271 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1272 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1273 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1274 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1275 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1276 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1277 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
| 1278 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1279 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1280 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1281 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1282 | out: |
| 1283 | btrfs_free_path(path); |
| 1284 | return ret; |
| 1285 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1286 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1287 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1288 | struct btrfs_device *device) |
| 1289 | { |
| 1290 | int ret; |
| 1291 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1292 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1293 | struct btrfs_trans_handle *trans; |
| 1294 | |
| 1295 | root = root->fs_info->chunk_root; |
| 1296 | |
| 1297 | path = btrfs_alloc_path(); |
| 1298 | if (!path) |
| 1299 | return -ENOMEM; |
| 1300 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1301 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1302 | if (IS_ERR(trans)) { |
| 1303 | btrfs_free_path(path); |
| 1304 | return PTR_ERR(trans); |
| 1305 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1306 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1307 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1308 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1309 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1310 | |
| 1311 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1312 | if (ret < 0) |
| 1313 | goto out; |
| 1314 | |
| 1315 | if (ret > 0) { |
| 1316 | ret = -ENOENT; |
| 1317 | goto out; |
| 1318 | } |
| 1319 | |
| 1320 | ret = btrfs_del_item(trans, root, path); |
| 1321 | if (ret) |
| 1322 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1323 | out: |
| 1324 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1325 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1326 | btrfs_commit_transaction(trans, root); |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1331 | { |
| 1332 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1333 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1334 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1335 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1336 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1337 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1338 | u64 all_avail; |
| 1339 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1340 | u64 num_devices; |
| 1341 | u8 *dev_uuid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1342 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1343 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1344 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1345 | mutex_lock(&uuid_mutex); |
| 1346 | |
| 1347 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1348 | root->fs_info->avail_system_alloc_bits | |
| 1349 | root->fs_info->avail_metadata_alloc_bits; |
| 1350 | |
| 1351 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && |
Josef Bacik | 035fe03 | 2010-01-27 02:09:38 +0000 | [diff] [blame] | 1352 | root->fs_info->fs_devices->num_devices <= 4) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1353 | printk(KERN_ERR "btrfs: unable to go below four devices " |
| 1354 | "on raid10\n"); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1355 | ret = -EINVAL; |
| 1356 | goto out; |
| 1357 | } |
| 1358 | |
| 1359 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && |
Josef Bacik | 035fe03 | 2010-01-27 02:09:38 +0000 | [diff] [blame] | 1360 | root->fs_info->fs_devices->num_devices <= 2) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1361 | printk(KERN_ERR "btrfs: unable to go below two " |
| 1362 | "devices on raid1\n"); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1363 | ret = -EINVAL; |
| 1364 | goto out; |
| 1365 | } |
| 1366 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1367 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1368 | struct list_head *devices; |
| 1369 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1370 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1371 | device = NULL; |
| 1372 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1373 | /* |
| 1374 | * It is safe to read the devices since the volume_mutex |
| 1375 | * is held. |
| 1376 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1377 | list_for_each_entry(tmp, devices, dev_list) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1378 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1379 | device = tmp; |
| 1380 | break; |
| 1381 | } |
| 1382 | } |
| 1383 | bdev = NULL; |
| 1384 | bh = NULL; |
| 1385 | disk_super = NULL; |
| 1386 | if (!device) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1387 | printk(KERN_ERR "btrfs: no missing devices found to " |
| 1388 | "remove\n"); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1389 | goto out; |
| 1390 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1391 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1392 | ret = btrfs_get_bdev_and_sb(device_path, |
| 1393 | FMODE_READ | FMODE_EXCL, |
| 1394 | root->fs_info->bdev_holder, 0, |
| 1395 | &bdev, &bh); |
| 1396 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1397 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1398 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1399 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1400 | dev_uuid = disk_super->dev_item.uuid; |
| 1401 | device = btrfs_find_device(root, devid, dev_uuid, |
| 1402 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1403 | if (!device) { |
| 1404 | ret = -ENOENT; |
| 1405 | goto error_brelse; |
| 1406 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1407 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1408 | |
| 1409 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1410 | printk(KERN_ERR "btrfs: unable to remove the only writeable " |
| 1411 | "device\n"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1412 | ret = -EINVAL; |
| 1413 | goto error_brelse; |
| 1414 | } |
| 1415 | |
| 1416 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1417 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1418 | list_del_init(&device->dev_alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1419 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1420 | root->fs_info->fs_devices->rw_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1421 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1422 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1423 | |
| 1424 | ret = btrfs_shrink_device(device, 0); |
| 1425 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1426 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1427 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1428 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1429 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1430 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1431 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1432 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1433 | root->fs_info->free_chunk_space = device->total_bytes - |
| 1434 | device->bytes_used; |
| 1435 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1436 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1437 | device->in_fs_metadata = 0; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1438 | btrfs_scrub_cancel_dev(root, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1439 | |
| 1440 | /* |
| 1441 | * the device list mutex makes sure that we don't change |
| 1442 | * the device list while someone else is writing out all |
| 1443 | * the device supers. |
| 1444 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1445 | |
| 1446 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1447 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1448 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1449 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1450 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1451 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1452 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1453 | if (device->missing) |
| 1454 | root->fs_info->fs_devices->missing_devices--; |
| 1455 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1456 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1457 | struct btrfs_device, dev_list); |
| 1458 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1459 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1460 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1461 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1462 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1463 | if (device->bdev) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1464 | device->fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1465 | |
| 1466 | call_rcu(&device->rcu, free_device); |
| 1467 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1468 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1469 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1470 | btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1471 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1472 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1473 | struct btrfs_fs_devices *fs_devices; |
| 1474 | fs_devices = root->fs_info->fs_devices; |
| 1475 | while (fs_devices) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1476 | if (fs_devices->seed == cur_devices) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1477 | break; |
| 1478 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1479 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1480 | fs_devices->seed = cur_devices->seed; |
| 1481 | cur_devices->seed = NULL; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1482 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1483 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1484 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1485 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1488 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1489 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1490 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1491 | /* |
| 1492 | * at this point, the device is zero sized. We want to |
| 1493 | * remove it from the devices list and zero out the old super |
| 1494 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1495 | if (clear_super) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1496 | /* make sure this device isn't detected as part of |
| 1497 | * the FS anymore |
| 1498 | */ |
| 1499 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1500 | set_buffer_dirty(bh); |
| 1501 | sync_dirty_buffer(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1502 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1503 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1504 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1505 | |
| 1506 | error_brelse: |
| 1507 | brelse(bh); |
| 1508 | error_close: |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1509 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1510 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1511 | out: |
| 1512 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1513 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1514 | error_undo: |
| 1515 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1516 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1517 | list_add(&device->dev_alloc_list, |
| 1518 | &root->fs_info->fs_devices->alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1519 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1520 | root->fs_info->fs_devices->rw_devices++; |
| 1521 | } |
| 1522 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1523 | } |
| 1524 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame^] | 1525 | int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1526 | struct btrfs_device **device) |
| 1527 | { |
| 1528 | int ret = 0; |
| 1529 | struct btrfs_super_block *disk_super; |
| 1530 | u64 devid; |
| 1531 | u8 *dev_uuid; |
| 1532 | struct block_device *bdev; |
| 1533 | struct buffer_head *bh; |
| 1534 | |
| 1535 | *device = NULL; |
| 1536 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1537 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1538 | if (ret) |
| 1539 | return ret; |
| 1540 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1541 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1542 | dev_uuid = disk_super->dev_item.uuid; |
| 1543 | *device = btrfs_find_device(root, devid, dev_uuid, |
| 1544 | disk_super->fsid); |
| 1545 | brelse(bh); |
| 1546 | if (!*device) |
| 1547 | ret = -ENOENT; |
| 1548 | blkdev_put(bdev, FMODE_READ); |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
| 1552 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1553 | char *device_path, |
| 1554 | struct btrfs_device **device) |
| 1555 | { |
| 1556 | *device = NULL; |
| 1557 | if (strcmp(device_path, "missing") == 0) { |
| 1558 | struct list_head *devices; |
| 1559 | struct btrfs_device *tmp; |
| 1560 | |
| 1561 | devices = &root->fs_info->fs_devices->devices; |
| 1562 | /* |
| 1563 | * It is safe to read the devices since the volume_mutex |
| 1564 | * is held by the caller. |
| 1565 | */ |
| 1566 | list_for_each_entry(tmp, devices, dev_list) { |
| 1567 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1568 | *device = tmp; |
| 1569 | break; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | if (!*device) { |
| 1574 | pr_err("btrfs: no missing device found\n"); |
| 1575 | return -ENOENT; |
| 1576 | } |
| 1577 | |
| 1578 | return 0; |
| 1579 | } else { |
| 1580 | return btrfs_find_device_by_path(root, device_path, device); |
| 1581 | } |
| 1582 | } |
| 1583 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1584 | /* |
| 1585 | * does all the dirty work required for changing file system's UUID. |
| 1586 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1587 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1588 | { |
| 1589 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1590 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1591 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1592 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1593 | struct btrfs_device *device; |
| 1594 | u64 super_flags; |
| 1595 | |
| 1596 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1597 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1598 | return -EINVAL; |
| 1599 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1600 | seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
| 1601 | if (!seed_devices) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1602 | return -ENOMEM; |
| 1603 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1604 | old_devices = clone_fs_devices(fs_devices); |
| 1605 | if (IS_ERR(old_devices)) { |
| 1606 | kfree(seed_devices); |
| 1607 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1608 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1609 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1610 | list_add(&old_devices->list, &fs_uuids); |
| 1611 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1612 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1613 | seed_devices->opened = 1; |
| 1614 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1615 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1616 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1617 | |
| 1618 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1619 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1620 | synchronize_rcu); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1621 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1622 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1623 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1624 | list_for_each_entry(device, &seed_devices->devices, dev_list) { |
| 1625 | device->fs_devices = seed_devices; |
| 1626 | } |
| 1627 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1628 | fs_devices->seeding = 0; |
| 1629 | fs_devices->num_devices = 0; |
| 1630 | fs_devices->open_devices = 0; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1631 | fs_devices->total_devices = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1632 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1633 | |
| 1634 | generate_random_uuid(fs_devices->fsid); |
| 1635 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1636 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1637 | super_flags = btrfs_super_flags(disk_super) & |
| 1638 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 1639 | btrfs_set_super_flags(disk_super, super_flags); |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * strore the expected generation for seed devices in device items. |
| 1646 | */ |
| 1647 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 1648 | struct btrfs_root *root) |
| 1649 | { |
| 1650 | struct btrfs_path *path; |
| 1651 | struct extent_buffer *leaf; |
| 1652 | struct btrfs_dev_item *dev_item; |
| 1653 | struct btrfs_device *device; |
| 1654 | struct btrfs_key key; |
| 1655 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 1656 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1657 | u64 devid; |
| 1658 | int ret; |
| 1659 | |
| 1660 | path = btrfs_alloc_path(); |
| 1661 | if (!path) |
| 1662 | return -ENOMEM; |
| 1663 | |
| 1664 | root = root->fs_info->chunk_root; |
| 1665 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1666 | key.offset = 0; |
| 1667 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1668 | |
| 1669 | while (1) { |
| 1670 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1671 | if (ret < 0) |
| 1672 | goto error; |
| 1673 | |
| 1674 | leaf = path->nodes[0]; |
| 1675 | next_slot: |
| 1676 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1677 | ret = btrfs_next_leaf(root, path); |
| 1678 | if (ret > 0) |
| 1679 | break; |
| 1680 | if (ret < 0) |
| 1681 | goto error; |
| 1682 | leaf = path->nodes[0]; |
| 1683 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1684 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1685 | continue; |
| 1686 | } |
| 1687 | |
| 1688 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1689 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 1690 | key.type != BTRFS_DEV_ITEM_KEY) |
| 1691 | break; |
| 1692 | |
| 1693 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 1694 | struct btrfs_dev_item); |
| 1695 | devid = btrfs_device_id(leaf, dev_item); |
| 1696 | read_extent_buffer(leaf, dev_uuid, |
| 1697 | (unsigned long)btrfs_device_uuid(dev_item), |
| 1698 | BTRFS_UUID_SIZE); |
| 1699 | read_extent_buffer(leaf, fs_uuid, |
| 1700 | (unsigned long)btrfs_device_fsid(dev_item), |
| 1701 | BTRFS_UUID_SIZE); |
| 1702 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1703 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1704 | |
| 1705 | if (device->fs_devices->seeding) { |
| 1706 | btrfs_set_device_generation(leaf, dev_item, |
| 1707 | device->generation); |
| 1708 | btrfs_mark_buffer_dirty(leaf); |
| 1709 | } |
| 1710 | |
| 1711 | path->slots[0]++; |
| 1712 | goto next_slot; |
| 1713 | } |
| 1714 | ret = 0; |
| 1715 | error: |
| 1716 | btrfs_free_path(path); |
| 1717 | return ret; |
| 1718 | } |
| 1719 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1720 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 1721 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1722 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1723 | struct btrfs_trans_handle *trans; |
| 1724 | struct btrfs_device *device; |
| 1725 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1726 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1727 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1728 | struct rcu_string *name; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1729 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1730 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1731 | int ret = 0; |
| 1732 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1733 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 1734 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1735 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1736 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1737 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 1738 | if (IS_ERR(bdev)) |
| 1739 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1740 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1741 | if (root->fs_info->fs_devices->seeding) { |
| 1742 | seeding_dev = 1; |
| 1743 | down_write(&sb->s_umount); |
| 1744 | mutex_lock(&uuid_mutex); |
| 1745 | } |
| 1746 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 1747 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1748 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1749 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1750 | |
| 1751 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1752 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1753 | if (device->bdev == bdev) { |
| 1754 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1755 | mutex_unlock( |
| 1756 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1757 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1758 | } |
| 1759 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 1760 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1761 | |
| 1762 | device = kzalloc(sizeof(*device), GFP_NOFS); |
| 1763 | if (!device) { |
| 1764 | /* we can safely leave the fs_devices entry around */ |
| 1765 | ret = -ENOMEM; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1766 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1769 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 1770 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1771 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1772 | ret = -ENOMEM; |
| 1773 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1774 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1775 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1776 | |
| 1777 | ret = find_next_devid(root, &device->devid); |
| 1778 | if (ret) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1779 | rcu_string_free(device->name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1780 | kfree(device); |
| 1781 | goto error; |
| 1782 | } |
| 1783 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1784 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1785 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1786 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1787 | kfree(device); |
| 1788 | ret = PTR_ERR(trans); |
| 1789 | goto error; |
| 1790 | } |
| 1791 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1792 | lock_chunks(root); |
| 1793 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1794 | q = bdev_get_queue(bdev); |
| 1795 | if (blk_queue_discard(q)) |
| 1796 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1797 | device->writeable = 1; |
| 1798 | device->work.func = pending_bios_fn; |
| 1799 | generate_random_uuid(device->uuid); |
| 1800 | spin_lock_init(&device->io_lock); |
| 1801 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1802 | device->io_width = root->sectorsize; |
| 1803 | device->io_align = root->sectorsize; |
| 1804 | device->sector_size = root->sectorsize; |
| 1805 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 1806 | device->disk_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1807 | device->dev_root = root->fs_info->dev_root; |
| 1808 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1809 | device->in_fs_metadata = 1; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 1810 | device->mode = FMODE_EXCL; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 1811 | set_blocksize(device->bdev, 4096); |
| 1812 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1813 | if (seeding_dev) { |
| 1814 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1815 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1816 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1820 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1821 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1822 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1823 | list_add(&device->dev_alloc_list, |
| 1824 | &root->fs_info->fs_devices->alloc_list); |
| 1825 | root->fs_info->fs_devices->num_devices++; |
| 1826 | root->fs_info->fs_devices->open_devices++; |
| 1827 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1828 | root->fs_info->fs_devices->total_devices++; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1829 | if (device->can_discard) |
| 1830 | root->fs_info->fs_devices->num_can_discard++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1831 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 1832 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1833 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1834 | root->fs_info->free_chunk_space += device->total_bytes; |
| 1835 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1836 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 1837 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 1838 | root->fs_info->fs_devices->rotating = 1; |
| 1839 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1840 | total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); |
| 1841 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1842 | total_bytes + device->total_bytes); |
| 1843 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1844 | total_bytes = btrfs_super_num_devices(root->fs_info->super_copy); |
| 1845 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1846 | total_bytes + 1); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1847 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1848 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1849 | if (seeding_dev) { |
| 1850 | ret = init_first_rw_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 1851 | if (ret) { |
| 1852 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1853 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 1854 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1855 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 1856 | if (ret) { |
| 1857 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1858 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 1859 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1860 | } else { |
| 1861 | ret = btrfs_add_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 1862 | if (ret) { |
| 1863 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1864 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 1865 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1866 | } |
| 1867 | |
Chris Mason | 913d952 | 2009-03-10 13:17:18 -0400 | [diff] [blame] | 1868 | /* |
| 1869 | * we've got more storage, clear any full flags on the space |
| 1870 | * infos |
| 1871 | */ |
| 1872 | btrfs_clear_space_info_full(root->fs_info); |
| 1873 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1874 | unlock_chunks(root); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1875 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1876 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1877 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1878 | |
| 1879 | if (seeding_dev) { |
| 1880 | mutex_unlock(&uuid_mutex); |
| 1881 | up_write(&sb->s_umount); |
| 1882 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1883 | if (ret) /* transaction commit */ |
| 1884 | return ret; |
| 1885 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1886 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1887 | if (ret < 0) |
| 1888 | btrfs_error(root->fs_info, ret, |
| 1889 | "Failed to relocate sys chunks after " |
| 1890 | "device initialization. This can be fixed " |
| 1891 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 1892 | trans = btrfs_attach_transaction(root); |
| 1893 | if (IS_ERR(trans)) { |
| 1894 | if (PTR_ERR(trans) == -ENOENT) |
| 1895 | return 0; |
| 1896 | return PTR_ERR(trans); |
| 1897 | } |
| 1898 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1899 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1900 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1901 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1902 | |
| 1903 | error_trans: |
| 1904 | unlock_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1905 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1906 | rcu_string_free(device->name); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1907 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1908 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1909 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1910 | if (seeding_dev) { |
| 1911 | mutex_unlock(&uuid_mutex); |
| 1912 | up_write(&sb->s_umount); |
| 1913 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1914 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 1915 | } |
| 1916 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1917 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 1918 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1919 | { |
| 1920 | int ret; |
| 1921 | struct btrfs_path *path; |
| 1922 | struct btrfs_root *root; |
| 1923 | struct btrfs_dev_item *dev_item; |
| 1924 | struct extent_buffer *leaf; |
| 1925 | struct btrfs_key key; |
| 1926 | |
| 1927 | root = device->dev_root->fs_info->chunk_root; |
| 1928 | |
| 1929 | path = btrfs_alloc_path(); |
| 1930 | if (!path) |
| 1931 | return -ENOMEM; |
| 1932 | |
| 1933 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1934 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1935 | key.offset = device->devid; |
| 1936 | |
| 1937 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1938 | if (ret < 0) |
| 1939 | goto out; |
| 1940 | |
| 1941 | if (ret > 0) { |
| 1942 | ret = -ENOENT; |
| 1943 | goto out; |
| 1944 | } |
| 1945 | |
| 1946 | leaf = path->nodes[0]; |
| 1947 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1948 | |
| 1949 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 1950 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1951 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1952 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1953 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 1954 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1955 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 1956 | btrfs_mark_buffer_dirty(leaf); |
| 1957 | |
| 1958 | out: |
| 1959 | btrfs_free_path(path); |
| 1960 | return ret; |
| 1961 | } |
| 1962 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1963 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1964 | struct btrfs_device *device, u64 new_size) |
| 1965 | { |
| 1966 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1967 | device->dev_root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1968 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 1969 | u64 diff = new_size - device->total_bytes; |
| 1970 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1971 | if (!device->writeable) |
| 1972 | return -EACCES; |
| 1973 | if (new_size <= device->total_bytes) |
| 1974 | return -EINVAL; |
| 1975 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1976 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1977 | device->fs_devices->total_rw_bytes += diff; |
| 1978 | |
| 1979 | device->total_bytes = new_size; |
Chris Mason | 9779b72 | 2009-07-24 16:41:41 -0400 | [diff] [blame] | 1980 | device->disk_total_bytes = new_size; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 1981 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
| 1982 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1983 | return btrfs_update_device(trans, device); |
| 1984 | } |
| 1985 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1986 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 1987 | struct btrfs_device *device, u64 new_size) |
| 1988 | { |
| 1989 | int ret; |
| 1990 | lock_chunks(device->dev_root); |
| 1991 | ret = __btrfs_grow_device(trans, device, new_size); |
| 1992 | unlock_chunks(device->dev_root); |
| 1993 | return ret; |
| 1994 | } |
| 1995 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1996 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 1997 | struct btrfs_root *root, |
| 1998 | u64 chunk_tree, u64 chunk_objectid, |
| 1999 | u64 chunk_offset) |
| 2000 | { |
| 2001 | int ret; |
| 2002 | struct btrfs_path *path; |
| 2003 | struct btrfs_key key; |
| 2004 | |
| 2005 | root = root->fs_info->chunk_root; |
| 2006 | path = btrfs_alloc_path(); |
| 2007 | if (!path) |
| 2008 | return -ENOMEM; |
| 2009 | |
| 2010 | key.objectid = chunk_objectid; |
| 2011 | key.offset = chunk_offset; |
| 2012 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2013 | |
| 2014 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2015 | if (ret < 0) |
| 2016 | goto out; |
| 2017 | else if (ret > 0) { /* Logic error or corruption */ |
| 2018 | btrfs_error(root->fs_info, -ENOENT, |
| 2019 | "Failed lookup while freeing chunk."); |
| 2020 | ret = -ENOENT; |
| 2021 | goto out; |
| 2022 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2023 | |
| 2024 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2025 | if (ret < 0) |
| 2026 | btrfs_error(root->fs_info, ret, |
| 2027 | "Failed to delete chunk item."); |
| 2028 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2029 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2030 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2031 | } |
| 2032 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2033 | 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] | 2034 | chunk_offset) |
| 2035 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2036 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2037 | struct btrfs_disk_key *disk_key; |
| 2038 | struct btrfs_chunk *chunk; |
| 2039 | u8 *ptr; |
| 2040 | int ret = 0; |
| 2041 | u32 num_stripes; |
| 2042 | u32 array_size; |
| 2043 | u32 len = 0; |
| 2044 | u32 cur; |
| 2045 | struct btrfs_key key; |
| 2046 | |
| 2047 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2048 | |
| 2049 | ptr = super_copy->sys_chunk_array; |
| 2050 | cur = 0; |
| 2051 | |
| 2052 | while (cur < array_size) { |
| 2053 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2054 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2055 | |
| 2056 | len = sizeof(*disk_key); |
| 2057 | |
| 2058 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2059 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2060 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2061 | len += btrfs_chunk_item_size(num_stripes); |
| 2062 | } else { |
| 2063 | ret = -EIO; |
| 2064 | break; |
| 2065 | } |
| 2066 | if (key.objectid == chunk_objectid && |
| 2067 | key.offset == chunk_offset) { |
| 2068 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2069 | array_size -= len; |
| 2070 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2071 | } else { |
| 2072 | ptr += len; |
| 2073 | cur += len; |
| 2074 | } |
| 2075 | } |
| 2076 | return ret; |
| 2077 | } |
| 2078 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2079 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2080 | u64 chunk_tree, u64 chunk_objectid, |
| 2081 | u64 chunk_offset) |
| 2082 | { |
| 2083 | struct extent_map_tree *em_tree; |
| 2084 | struct btrfs_root *extent_root; |
| 2085 | struct btrfs_trans_handle *trans; |
| 2086 | struct extent_map *em; |
| 2087 | struct map_lookup *map; |
| 2088 | int ret; |
| 2089 | int i; |
| 2090 | |
| 2091 | root = root->fs_info->chunk_root; |
| 2092 | extent_root = root->fs_info->extent_root; |
| 2093 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2094 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2095 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2096 | if (ret) |
| 2097 | return -ENOSPC; |
| 2098 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2099 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2100 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2101 | if (ret) |
| 2102 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2103 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2104 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2105 | BUG_ON(IS_ERR(trans)); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2106 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2107 | lock_chunks(root); |
| 2108 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2109 | /* |
| 2110 | * step two, delete the device extents and the |
| 2111 | * chunk tree entries |
| 2112 | */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2113 | read_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2114 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2115 | read_unlock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2116 | |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 2117 | BUG_ON(!em || em->start > chunk_offset || |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2118 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2119 | map = (struct map_lookup *)em->bdev; |
| 2120 | |
| 2121 | for (i = 0; i < map->num_stripes; i++) { |
| 2122 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 2123 | map->stripes[i].physical); |
| 2124 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2125 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2126 | if (map->stripes[i].dev) { |
| 2127 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2128 | BUG_ON(ret); |
| 2129 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2130 | } |
| 2131 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2132 | chunk_offset); |
| 2133 | |
| 2134 | BUG_ON(ret); |
| 2135 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2136 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2137 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2138 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2139 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2140 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2141 | } |
| 2142 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2143 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2144 | BUG_ON(ret); |
| 2145 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2146 | write_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2147 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2148 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2149 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2150 | kfree(map); |
| 2151 | em->bdev = NULL; |
| 2152 | |
| 2153 | /* once for the tree */ |
| 2154 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2155 | /* once for us */ |
| 2156 | free_extent_map(em); |
| 2157 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2158 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2159 | btrfs_end_transaction(trans, root); |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2163 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2164 | { |
| 2165 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2166 | struct btrfs_path *path; |
| 2167 | struct extent_buffer *leaf; |
| 2168 | struct btrfs_chunk *chunk; |
| 2169 | struct btrfs_key key; |
| 2170 | struct btrfs_key found_key; |
| 2171 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2172 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2173 | bool retried = false; |
| 2174 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2175 | int ret; |
| 2176 | |
| 2177 | path = btrfs_alloc_path(); |
| 2178 | if (!path) |
| 2179 | return -ENOMEM; |
| 2180 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2181 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2182 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2183 | key.offset = (u64)-1; |
| 2184 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2185 | |
| 2186 | while (1) { |
| 2187 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2188 | if (ret < 0) |
| 2189 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2190 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2191 | |
| 2192 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2193 | key.type); |
| 2194 | if (ret < 0) |
| 2195 | goto error; |
| 2196 | if (ret > 0) |
| 2197 | break; |
| 2198 | |
| 2199 | leaf = path->nodes[0]; |
| 2200 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2201 | |
| 2202 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2203 | struct btrfs_chunk); |
| 2204 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2205 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2206 | |
| 2207 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2208 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2209 | found_key.objectid, |
| 2210 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2211 | if (ret == -ENOSPC) |
| 2212 | failed++; |
| 2213 | else if (ret) |
| 2214 | BUG(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | if (found_key.offset == 0) |
| 2218 | break; |
| 2219 | key.offset = found_key.offset - 1; |
| 2220 | } |
| 2221 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2222 | if (failed && !retried) { |
| 2223 | failed = 0; |
| 2224 | retried = true; |
| 2225 | goto again; |
| 2226 | } else if (failed && retried) { |
| 2227 | WARN_ON(1); |
| 2228 | ret = -ENOSPC; |
| 2229 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2230 | error: |
| 2231 | btrfs_free_path(path); |
| 2232 | return ret; |
| 2233 | } |
| 2234 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2235 | static int insert_balance_item(struct btrfs_root *root, |
| 2236 | struct btrfs_balance_control *bctl) |
| 2237 | { |
| 2238 | struct btrfs_trans_handle *trans; |
| 2239 | struct btrfs_balance_item *item; |
| 2240 | struct btrfs_disk_balance_args disk_bargs; |
| 2241 | struct btrfs_path *path; |
| 2242 | struct extent_buffer *leaf; |
| 2243 | struct btrfs_key key; |
| 2244 | int ret, err; |
| 2245 | |
| 2246 | path = btrfs_alloc_path(); |
| 2247 | if (!path) |
| 2248 | return -ENOMEM; |
| 2249 | |
| 2250 | trans = btrfs_start_transaction(root, 0); |
| 2251 | if (IS_ERR(trans)) { |
| 2252 | btrfs_free_path(path); |
| 2253 | return PTR_ERR(trans); |
| 2254 | } |
| 2255 | |
| 2256 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2257 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2258 | key.offset = 0; |
| 2259 | |
| 2260 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2261 | sizeof(*item)); |
| 2262 | if (ret) |
| 2263 | goto out; |
| 2264 | |
| 2265 | leaf = path->nodes[0]; |
| 2266 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2267 | |
| 2268 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2269 | |
| 2270 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2271 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2272 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2273 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2274 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2275 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2276 | |
| 2277 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2278 | |
| 2279 | btrfs_mark_buffer_dirty(leaf); |
| 2280 | out: |
| 2281 | btrfs_free_path(path); |
| 2282 | err = btrfs_commit_transaction(trans, root); |
| 2283 | if (err && !ret) |
| 2284 | ret = err; |
| 2285 | return ret; |
| 2286 | } |
| 2287 | |
| 2288 | static int del_balance_item(struct btrfs_root *root) |
| 2289 | { |
| 2290 | struct btrfs_trans_handle *trans; |
| 2291 | struct btrfs_path *path; |
| 2292 | struct btrfs_key key; |
| 2293 | int ret, err; |
| 2294 | |
| 2295 | path = btrfs_alloc_path(); |
| 2296 | if (!path) |
| 2297 | return -ENOMEM; |
| 2298 | |
| 2299 | trans = btrfs_start_transaction(root, 0); |
| 2300 | if (IS_ERR(trans)) { |
| 2301 | btrfs_free_path(path); |
| 2302 | return PTR_ERR(trans); |
| 2303 | } |
| 2304 | |
| 2305 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2306 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2307 | key.offset = 0; |
| 2308 | |
| 2309 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2310 | if (ret < 0) |
| 2311 | goto out; |
| 2312 | if (ret > 0) { |
| 2313 | ret = -ENOENT; |
| 2314 | goto out; |
| 2315 | } |
| 2316 | |
| 2317 | ret = btrfs_del_item(trans, root, path); |
| 2318 | out: |
| 2319 | btrfs_free_path(path); |
| 2320 | err = btrfs_commit_transaction(trans, root); |
| 2321 | if (err && !ret) |
| 2322 | ret = err; |
| 2323 | return ret; |
| 2324 | } |
| 2325 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2326 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2327 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2328 | * resume after balance was interrupted. |
| 2329 | */ |
| 2330 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2331 | { |
| 2332 | /* |
| 2333 | * Turn on soft mode for chunk types that were being converted. |
| 2334 | */ |
| 2335 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2336 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2337 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2338 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2339 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2340 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2341 | |
| 2342 | /* |
| 2343 | * Turn on usage filter if is not already used. The idea is |
| 2344 | * that chunks that we have already balanced should be |
| 2345 | * reasonably full. Don't do it for chunks that are being |
| 2346 | * converted - that will keep us from relocating unconverted |
| 2347 | * (albeit full) chunks. |
| 2348 | */ |
| 2349 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2350 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2351 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2352 | bctl->data.usage = 90; |
| 2353 | } |
| 2354 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2355 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2356 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2357 | bctl->sys.usage = 90; |
| 2358 | } |
| 2359 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2360 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2361 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2362 | bctl->meta.usage = 90; |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2367 | * Should be called with both balance and volume mutexes held to |
| 2368 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2369 | * restriper. Same goes for unset_balance_control. |
| 2370 | */ |
| 2371 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2372 | { |
| 2373 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2374 | |
| 2375 | BUG_ON(fs_info->balance_ctl); |
| 2376 | |
| 2377 | spin_lock(&fs_info->balance_lock); |
| 2378 | fs_info->balance_ctl = bctl; |
| 2379 | spin_unlock(&fs_info->balance_lock); |
| 2380 | } |
| 2381 | |
| 2382 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2383 | { |
| 2384 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2385 | |
| 2386 | BUG_ON(!fs_info->balance_ctl); |
| 2387 | |
| 2388 | spin_lock(&fs_info->balance_lock); |
| 2389 | fs_info->balance_ctl = NULL; |
| 2390 | spin_unlock(&fs_info->balance_lock); |
| 2391 | |
| 2392 | kfree(bctl); |
| 2393 | } |
| 2394 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2395 | /* |
| 2396 | * Balance filters. Return 1 if chunk should be filtered out |
| 2397 | * (should not be balanced). |
| 2398 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2399 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2400 | struct btrfs_balance_args *bargs) |
| 2401 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2402 | chunk_type = chunk_to_extended(chunk_type) & |
| 2403 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2404 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2405 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2406 | return 0; |
| 2407 | |
| 2408 | return 1; |
| 2409 | } |
| 2410 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2411 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2412 | struct btrfs_balance_args *bargs) |
| 2413 | { |
| 2414 | struct btrfs_block_group_cache *cache; |
| 2415 | u64 chunk_used, user_thresh; |
| 2416 | int ret = 1; |
| 2417 | |
| 2418 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2419 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2420 | |
| 2421 | user_thresh = div_factor_fine(cache->key.offset, bargs->usage); |
| 2422 | if (chunk_used < user_thresh) |
| 2423 | ret = 0; |
| 2424 | |
| 2425 | btrfs_put_block_group(cache); |
| 2426 | return ret; |
| 2427 | } |
| 2428 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2429 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2430 | struct btrfs_chunk *chunk, |
| 2431 | struct btrfs_balance_args *bargs) |
| 2432 | { |
| 2433 | struct btrfs_stripe *stripe; |
| 2434 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2435 | int i; |
| 2436 | |
| 2437 | for (i = 0; i < num_stripes; i++) { |
| 2438 | stripe = btrfs_stripe_nr(chunk, i); |
| 2439 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
| 2443 | return 1; |
| 2444 | } |
| 2445 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2446 | /* [pstart, pend) */ |
| 2447 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2448 | struct btrfs_chunk *chunk, |
| 2449 | u64 chunk_offset, |
| 2450 | struct btrfs_balance_args *bargs) |
| 2451 | { |
| 2452 | struct btrfs_stripe *stripe; |
| 2453 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2454 | u64 stripe_offset; |
| 2455 | u64 stripe_length; |
| 2456 | int factor; |
| 2457 | int i; |
| 2458 | |
| 2459 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 2460 | return 0; |
| 2461 | |
| 2462 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
| 2463 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2464 | factor = 2; |
| 2465 | else |
| 2466 | factor = 1; |
| 2467 | factor = num_stripes / factor; |
| 2468 | |
| 2469 | for (i = 0; i < num_stripes; i++) { |
| 2470 | stripe = btrfs_stripe_nr(chunk, i); |
| 2471 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 2472 | continue; |
| 2473 | |
| 2474 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 2475 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 2476 | do_div(stripe_length, factor); |
| 2477 | |
| 2478 | if (stripe_offset < bargs->pend && |
| 2479 | stripe_offset + stripe_length > bargs->pstart) |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | return 1; |
| 2484 | } |
| 2485 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2486 | /* [vstart, vend) */ |
| 2487 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 2488 | struct btrfs_chunk *chunk, |
| 2489 | u64 chunk_offset, |
| 2490 | struct btrfs_balance_args *bargs) |
| 2491 | { |
| 2492 | if (chunk_offset < bargs->vend && |
| 2493 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 2494 | /* at least part of the chunk is inside this vrange */ |
| 2495 | return 0; |
| 2496 | |
| 2497 | return 1; |
| 2498 | } |
| 2499 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2500 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2501 | struct btrfs_balance_args *bargs) |
| 2502 | { |
| 2503 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 2504 | return 0; |
| 2505 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2506 | chunk_type = chunk_to_extended(chunk_type) & |
| 2507 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2508 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2509 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2510 | return 1; |
| 2511 | |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2515 | static int should_balance_chunk(struct btrfs_root *root, |
| 2516 | struct extent_buffer *leaf, |
| 2517 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 2518 | { |
| 2519 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 2520 | struct btrfs_balance_args *bargs = NULL; |
| 2521 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 2522 | |
| 2523 | /* type filter */ |
| 2524 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 2525 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
| 2529 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 2530 | bargs = &bctl->data; |
| 2531 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2532 | bargs = &bctl->sys; |
| 2533 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 2534 | bargs = &bctl->meta; |
| 2535 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2536 | /* profiles filter */ |
| 2537 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 2538 | chunk_profiles_filter(chunk_type, bargs)) { |
| 2539 | return 0; |
| 2540 | } |
| 2541 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2542 | /* usage filter */ |
| 2543 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2544 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 2545 | return 0; |
| 2546 | } |
| 2547 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2548 | /* devid filter */ |
| 2549 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 2550 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 2551 | return 0; |
| 2552 | } |
| 2553 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2554 | /* drange filter, makes sense only with devid filter */ |
| 2555 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 2556 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2560 | /* vrange filter */ |
| 2561 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 2562 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2563 | return 0; |
| 2564 | } |
| 2565 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2566 | /* soft profile changing mode */ |
| 2567 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 2568 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2572 | return 1; |
| 2573 | } |
| 2574 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2575 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2576 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2577 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2578 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 2579 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 2580 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2581 | struct btrfs_device *device; |
| 2582 | u64 old_size; |
| 2583 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2584 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2585 | struct btrfs_path *path; |
| 2586 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2587 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2588 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2589 | struct extent_buffer *leaf; |
| 2590 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2591 | int ret; |
| 2592 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2593 | bool counting = true; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2594 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2595 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2596 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2597 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2598 | old_size = device->total_bytes; |
| 2599 | size_to_free = div_factor(old_size, 1); |
| 2600 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2601 | if (!device->writeable || |
| 2602 | device->total_bytes - device->bytes_used > size_to_free) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2603 | continue; |
| 2604 | |
| 2605 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2606 | if (ret == -ENOSPC) |
| 2607 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2608 | BUG_ON(ret); |
| 2609 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2610 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2611 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2612 | |
| 2613 | ret = btrfs_grow_device(trans, device, old_size); |
| 2614 | BUG_ON(ret); |
| 2615 | |
| 2616 | btrfs_end_transaction(trans, dev_root); |
| 2617 | } |
| 2618 | |
| 2619 | /* step two, relocate all the chunks */ |
| 2620 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 2621 | if (!path) { |
| 2622 | ret = -ENOMEM; |
| 2623 | goto error; |
| 2624 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2625 | |
| 2626 | /* zero out stat counters */ |
| 2627 | spin_lock(&fs_info->balance_lock); |
| 2628 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 2629 | spin_unlock(&fs_info->balance_lock); |
| 2630 | again: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2631 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2632 | key.offset = (u64)-1; |
| 2633 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2634 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2635 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2636 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2637 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2638 | ret = -ECANCELED; |
| 2639 | goto error; |
| 2640 | } |
| 2641 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2642 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2643 | if (ret < 0) |
| 2644 | goto error; |
| 2645 | |
| 2646 | /* |
| 2647 | * this shouldn't happen, it means the last relocate |
| 2648 | * failed |
| 2649 | */ |
| 2650 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2651 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2652 | |
| 2653 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 2654 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2655 | if (ret) { |
| 2656 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2657 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2658 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2659 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2660 | leaf = path->nodes[0]; |
| 2661 | slot = path->slots[0]; |
| 2662 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2663 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2664 | if (found_key.objectid != key.objectid) |
| 2665 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2666 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2667 | /* chunk zero is special */ |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2668 | if (found_key.offset == 0) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2669 | break; |
| 2670 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2671 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 2672 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2673 | if (!counting) { |
| 2674 | spin_lock(&fs_info->balance_lock); |
| 2675 | bctl->stat.considered++; |
| 2676 | spin_unlock(&fs_info->balance_lock); |
| 2677 | } |
| 2678 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2679 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 2680 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2681 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2682 | if (!ret) |
| 2683 | goto loop; |
| 2684 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2685 | if (counting) { |
| 2686 | spin_lock(&fs_info->balance_lock); |
| 2687 | bctl->stat.expected++; |
| 2688 | spin_unlock(&fs_info->balance_lock); |
| 2689 | goto loop; |
| 2690 | } |
| 2691 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2692 | ret = btrfs_relocate_chunk(chunk_root, |
| 2693 | chunk_root->root_key.objectid, |
| 2694 | found_key.objectid, |
| 2695 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 2696 | if (ret && ret != -ENOSPC) |
| 2697 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2698 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2699 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2700 | } else { |
| 2701 | spin_lock(&fs_info->balance_lock); |
| 2702 | bctl->stat.completed++; |
| 2703 | spin_unlock(&fs_info->balance_lock); |
| 2704 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2705 | loop: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2706 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2707 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2708 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2709 | if (counting) { |
| 2710 | btrfs_release_path(path); |
| 2711 | counting = false; |
| 2712 | goto again; |
| 2713 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2714 | error: |
| 2715 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2716 | if (enospc_errors) { |
| 2717 | printk(KERN_INFO "btrfs: %d enospc errors during balance\n", |
| 2718 | enospc_errors); |
| 2719 | if (!ret) |
| 2720 | ret = -ENOSPC; |
| 2721 | } |
| 2722 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2723 | return ret; |
| 2724 | } |
| 2725 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2726 | /** |
| 2727 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 2728 | * @flags: profile to validate |
| 2729 | * @extended: if true @flags is treated as an extended profile |
| 2730 | */ |
| 2731 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 2732 | { |
| 2733 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 2734 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 2735 | |
| 2736 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 2737 | |
| 2738 | /* 1) check that all other bits are zeroed */ |
| 2739 | if (flags & ~mask) |
| 2740 | return 0; |
| 2741 | |
| 2742 | /* 2) see if profile is reduced */ |
| 2743 | if (flags == 0) |
| 2744 | return !extended; /* "0" is valid for usual profiles */ |
| 2745 | |
| 2746 | /* true if exactly one bit set */ |
| 2747 | return (flags & (flags - 1)) == 0; |
| 2748 | } |
| 2749 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2750 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 2751 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2752 | /* cancel requested || normal exit path */ |
| 2753 | return atomic_read(&fs_info->balance_cancel_req) || |
| 2754 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 2755 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2756 | } |
| 2757 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2758 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 2759 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2760 | int ret; |
| 2761 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2762 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2763 | ret = del_balance_item(fs_info->tree_root); |
| 2764 | BUG_ON(ret); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2765 | } |
| 2766 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2767 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2768 | struct btrfs_ioctl_balance_args *bargs); |
| 2769 | |
| 2770 | /* |
| 2771 | * Should be called with both balance and volume mutexes held |
| 2772 | */ |
| 2773 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 2774 | struct btrfs_ioctl_balance_args *bargs) |
| 2775 | { |
| 2776 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2777 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2778 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2779 | int ret; |
| 2780 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2781 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2782 | atomic_read(&fs_info->balance_pause_req) || |
| 2783 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2784 | ret = -EINVAL; |
| 2785 | goto out; |
| 2786 | } |
| 2787 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2788 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 2789 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 2790 | mixed = 1; |
| 2791 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2792 | /* |
| 2793 | * In case of mixed groups both data and meta should be picked, |
| 2794 | * and identical options should be given for both of them. |
| 2795 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2796 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 2797 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2798 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 2799 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 2800 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
| 2801 | printk(KERN_ERR "btrfs: with mixed groups data and " |
| 2802 | "metadata balance options must be the same\n"); |
| 2803 | ret = -EINVAL; |
| 2804 | goto out; |
| 2805 | } |
| 2806 | } |
| 2807 | |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2808 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
| 2809 | if (fs_info->fs_devices->num_devices == 1) |
| 2810 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
| 2811 | else if (fs_info->fs_devices->num_devices < 4) |
| 2812 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
| 2813 | else |
| 2814 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
| 2815 | BTRFS_BLOCK_GROUP_RAID10); |
| 2816 | |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2817 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 2818 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 2819 | (bctl->data.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2820 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 2821 | "data profile %llu\n", |
| 2822 | (unsigned long long)bctl->data.target); |
| 2823 | ret = -EINVAL; |
| 2824 | goto out; |
| 2825 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2826 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 2827 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 2828 | (bctl->meta.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2829 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 2830 | "metadata profile %llu\n", |
| 2831 | (unsigned long long)bctl->meta.target); |
| 2832 | ret = -EINVAL; |
| 2833 | goto out; |
| 2834 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2835 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 2836 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 2837 | (bctl->sys.target & ~allowed))) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2838 | printk(KERN_ERR "btrfs: unable to start balance with target " |
| 2839 | "system profile %llu\n", |
| 2840 | (unsigned long long)bctl->sys.target); |
| 2841 | ret = -EINVAL; |
| 2842 | goto out; |
| 2843 | } |
| 2844 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2845 | /* allow dup'ed data chunks only in mixed mode */ |
| 2846 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 2847 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2848 | printk(KERN_ERR "btrfs: dup for data is not allowed\n"); |
| 2849 | ret = -EINVAL; |
| 2850 | goto out; |
| 2851 | } |
| 2852 | |
| 2853 | /* allow to reduce meta or sys integrity only if force set */ |
| 2854 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2855 | BTRFS_BLOCK_GROUP_RAID10; |
| 2856 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 2857 | (fs_info->avail_system_alloc_bits & allowed) && |
| 2858 | !(bctl->sys.target & allowed)) || |
| 2859 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 2860 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 2861 | !(bctl->meta.target & allowed))) { |
| 2862 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 2863 | printk(KERN_INFO "btrfs: force reducing metadata " |
| 2864 | "integrity\n"); |
| 2865 | } else { |
| 2866 | printk(KERN_ERR "btrfs: balance will reduce metadata " |
| 2867 | "integrity, use force if you want this\n"); |
| 2868 | ret = -EINVAL; |
| 2869 | goto out; |
| 2870 | } |
| 2871 | } |
| 2872 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2873 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 2874 | int num_tolerated_disk_barrier_failures; |
| 2875 | u64 target = bctl->sys.target; |
| 2876 | |
| 2877 | num_tolerated_disk_barrier_failures = |
| 2878 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 2879 | if (num_tolerated_disk_barrier_failures > 0 && |
| 2880 | (target & |
| 2881 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 2882 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 2883 | num_tolerated_disk_barrier_failures = 0; |
| 2884 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 2885 | (target & |
| 2886 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 2887 | num_tolerated_disk_barrier_failures = 1; |
| 2888 | |
| 2889 | fs_info->num_tolerated_disk_barrier_failures = |
| 2890 | num_tolerated_disk_barrier_failures; |
| 2891 | } |
| 2892 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2893 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2894 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2895 | goto out; |
| 2896 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2897 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 2898 | BUG_ON(ret == -EEXIST); |
| 2899 | set_balance_control(bctl); |
| 2900 | } else { |
| 2901 | BUG_ON(ret != -EEXIST); |
| 2902 | spin_lock(&fs_info->balance_lock); |
| 2903 | update_balance_args(bctl); |
| 2904 | spin_unlock(&fs_info->balance_lock); |
| 2905 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2906 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2907 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2908 | mutex_unlock(&fs_info->balance_mutex); |
| 2909 | |
| 2910 | ret = __btrfs_balance(fs_info); |
| 2911 | |
| 2912 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2913 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2914 | |
| 2915 | if (bargs) { |
| 2916 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2917 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2918 | } |
| 2919 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2920 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 2921 | balance_need_close(fs_info)) { |
| 2922 | __cancel_balance(fs_info); |
| 2923 | } |
| 2924 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2925 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 2926 | fs_info->num_tolerated_disk_barrier_failures = |
| 2927 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 2928 | } |
| 2929 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2930 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2931 | |
| 2932 | return ret; |
| 2933 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2934 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 2935 | __cancel_balance(fs_info); |
| 2936 | else |
| 2937 | kfree(bctl); |
| 2938 | return ret; |
| 2939 | } |
| 2940 | |
| 2941 | static int balance_kthread(void *data) |
| 2942 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 2943 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2944 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2945 | |
| 2946 | mutex_lock(&fs_info->volume_mutex); |
| 2947 | mutex_lock(&fs_info->balance_mutex); |
| 2948 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 2949 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2950 | printk(KERN_INFO "btrfs: continuing balance\n"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 2951 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2952 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2953 | |
| 2954 | mutex_unlock(&fs_info->balance_mutex); |
| 2955 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 2956 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2957 | return ret; |
| 2958 | } |
| 2959 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 2960 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 2961 | { |
| 2962 | struct task_struct *tsk; |
| 2963 | |
| 2964 | spin_lock(&fs_info->balance_lock); |
| 2965 | if (!fs_info->balance_ctl) { |
| 2966 | spin_unlock(&fs_info->balance_lock); |
| 2967 | return 0; |
| 2968 | } |
| 2969 | spin_unlock(&fs_info->balance_lock); |
| 2970 | |
| 2971 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
| 2972 | printk(KERN_INFO "btrfs: force skipping balance\n"); |
| 2973 | return 0; |
| 2974 | } |
| 2975 | |
| 2976 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
| 2977 | if (IS_ERR(tsk)) |
| 2978 | return PTR_ERR(tsk); |
| 2979 | |
| 2980 | return 0; |
| 2981 | } |
| 2982 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 2983 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2984 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2985 | struct btrfs_balance_control *bctl; |
| 2986 | struct btrfs_balance_item *item; |
| 2987 | struct btrfs_disk_balance_args disk_bargs; |
| 2988 | struct btrfs_path *path; |
| 2989 | struct extent_buffer *leaf; |
| 2990 | struct btrfs_key key; |
| 2991 | int ret; |
| 2992 | |
| 2993 | path = btrfs_alloc_path(); |
| 2994 | if (!path) |
| 2995 | return -ENOMEM; |
| 2996 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 2997 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2998 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2999 | key.offset = 0; |
| 3000 | |
| 3001 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3002 | if (ret < 0) |
| 3003 | goto out; |
| 3004 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3005 | ret = 0; |
| 3006 | goto out; |
| 3007 | } |
| 3008 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3009 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3010 | if (!bctl) { |
| 3011 | ret = -ENOMEM; |
| 3012 | goto out; |
| 3013 | } |
| 3014 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3015 | leaf = path->nodes[0]; |
| 3016 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3017 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3018 | bctl->fs_info = fs_info; |
| 3019 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3020 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3021 | |
| 3022 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3023 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3024 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3025 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3026 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3027 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3028 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3029 | mutex_lock(&fs_info->volume_mutex); |
| 3030 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3031 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3032 | set_balance_control(bctl); |
| 3033 | |
| 3034 | mutex_unlock(&fs_info->balance_mutex); |
| 3035 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3036 | out: |
| 3037 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3038 | return ret; |
| 3039 | } |
| 3040 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3041 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3042 | { |
| 3043 | int ret = 0; |
| 3044 | |
| 3045 | mutex_lock(&fs_info->balance_mutex); |
| 3046 | if (!fs_info->balance_ctl) { |
| 3047 | mutex_unlock(&fs_info->balance_mutex); |
| 3048 | return -ENOTCONN; |
| 3049 | } |
| 3050 | |
| 3051 | if (atomic_read(&fs_info->balance_running)) { |
| 3052 | atomic_inc(&fs_info->balance_pause_req); |
| 3053 | mutex_unlock(&fs_info->balance_mutex); |
| 3054 | |
| 3055 | wait_event(fs_info->balance_wait_q, |
| 3056 | atomic_read(&fs_info->balance_running) == 0); |
| 3057 | |
| 3058 | mutex_lock(&fs_info->balance_mutex); |
| 3059 | /* we are good with balance_ctl ripped off from under us */ |
| 3060 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3061 | atomic_dec(&fs_info->balance_pause_req); |
| 3062 | } else { |
| 3063 | ret = -ENOTCONN; |
| 3064 | } |
| 3065 | |
| 3066 | mutex_unlock(&fs_info->balance_mutex); |
| 3067 | return ret; |
| 3068 | } |
| 3069 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3070 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3071 | { |
| 3072 | mutex_lock(&fs_info->balance_mutex); |
| 3073 | if (!fs_info->balance_ctl) { |
| 3074 | mutex_unlock(&fs_info->balance_mutex); |
| 3075 | return -ENOTCONN; |
| 3076 | } |
| 3077 | |
| 3078 | atomic_inc(&fs_info->balance_cancel_req); |
| 3079 | /* |
| 3080 | * if we are running just wait and return, balance item is |
| 3081 | * deleted in btrfs_balance in this case |
| 3082 | */ |
| 3083 | if (atomic_read(&fs_info->balance_running)) { |
| 3084 | mutex_unlock(&fs_info->balance_mutex); |
| 3085 | wait_event(fs_info->balance_wait_q, |
| 3086 | atomic_read(&fs_info->balance_running) == 0); |
| 3087 | mutex_lock(&fs_info->balance_mutex); |
| 3088 | } else { |
| 3089 | /* __cancel_balance needs volume_mutex */ |
| 3090 | mutex_unlock(&fs_info->balance_mutex); |
| 3091 | mutex_lock(&fs_info->volume_mutex); |
| 3092 | mutex_lock(&fs_info->balance_mutex); |
| 3093 | |
| 3094 | if (fs_info->balance_ctl) |
| 3095 | __cancel_balance(fs_info); |
| 3096 | |
| 3097 | mutex_unlock(&fs_info->volume_mutex); |
| 3098 | } |
| 3099 | |
| 3100 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3101 | atomic_dec(&fs_info->balance_cancel_req); |
| 3102 | mutex_unlock(&fs_info->balance_mutex); |
| 3103 | return 0; |
| 3104 | } |
| 3105 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3106 | /* |
| 3107 | * shrinking a device means finding all of the device extents past |
| 3108 | * the new size, and then following the back refs to the chunks. |
| 3109 | * The chunk relocation code actually frees the device extent |
| 3110 | */ |
| 3111 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3112 | { |
| 3113 | struct btrfs_trans_handle *trans; |
| 3114 | struct btrfs_root *root = device->dev_root; |
| 3115 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3116 | struct btrfs_path *path; |
| 3117 | u64 length; |
| 3118 | u64 chunk_tree; |
| 3119 | u64 chunk_objectid; |
| 3120 | u64 chunk_offset; |
| 3121 | int ret; |
| 3122 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3123 | int failed = 0; |
| 3124 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3125 | struct extent_buffer *l; |
| 3126 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3127 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3128 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3129 | u64 old_size = device->total_bytes; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3130 | u64 diff = device->total_bytes - new_size; |
| 3131 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3132 | path = btrfs_alloc_path(); |
| 3133 | if (!path) |
| 3134 | return -ENOMEM; |
| 3135 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3136 | path->reada = 2; |
| 3137 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3138 | lock_chunks(root); |
| 3139 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3140 | device->total_bytes = new_size; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3141 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3142 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3143 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3144 | root->fs_info->free_chunk_space -= diff; |
| 3145 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3146 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3147 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3148 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3149 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3150 | key.objectid = device->devid; |
| 3151 | key.offset = (u64)-1; |
| 3152 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3153 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3154 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3155 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3156 | if (ret < 0) |
| 3157 | goto done; |
| 3158 | |
| 3159 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 3160 | if (ret < 0) |
| 3161 | goto done; |
| 3162 | if (ret) { |
| 3163 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3164 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3165 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | l = path->nodes[0]; |
| 3169 | slot = path->slots[0]; |
| 3170 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 3171 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3172 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3173 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3174 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3175 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3176 | |
| 3177 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3178 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3179 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3180 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3181 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3182 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3183 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3184 | |
| 3185 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 3186 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 3187 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3188 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3189 | |
| 3190 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 3191 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3192 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3193 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3194 | if (ret == -ENOSPC) |
| 3195 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3196 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3197 | |
| 3198 | if (failed && !retried) { |
| 3199 | failed = 0; |
| 3200 | retried = true; |
| 3201 | goto again; |
| 3202 | } else if (failed && retried) { |
| 3203 | ret = -ENOSPC; |
| 3204 | lock_chunks(root); |
| 3205 | |
| 3206 | device->total_bytes = old_size; |
| 3207 | if (device->writeable) |
| 3208 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3209 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3210 | root->fs_info->free_chunk_space += diff; |
| 3211 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3212 | unlock_chunks(root); |
| 3213 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3214 | } |
| 3215 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3216 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3217 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3218 | if (IS_ERR(trans)) { |
| 3219 | ret = PTR_ERR(trans); |
| 3220 | goto done; |
| 3221 | } |
| 3222 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3223 | lock_chunks(root); |
| 3224 | |
| 3225 | device->disk_total_bytes = new_size; |
| 3226 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 3227 | ret = btrfs_update_device(trans, device); |
| 3228 | if (ret) { |
| 3229 | unlock_chunks(root); |
| 3230 | btrfs_end_transaction(trans, root); |
| 3231 | goto done; |
| 3232 | } |
| 3233 | WARN_ON(diff > old_total); |
| 3234 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 3235 | unlock_chunks(root); |
| 3236 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3237 | done: |
| 3238 | btrfs_free_path(path); |
| 3239 | return ret; |
| 3240 | } |
| 3241 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3242 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3243 | struct btrfs_key *key, |
| 3244 | struct btrfs_chunk *chunk, int item_size) |
| 3245 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3246 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3247 | struct btrfs_disk_key disk_key; |
| 3248 | u32 array_size; |
| 3249 | u8 *ptr; |
| 3250 | |
| 3251 | array_size = btrfs_super_sys_array_size(super_copy); |
| 3252 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
| 3253 | return -EFBIG; |
| 3254 | |
| 3255 | ptr = super_copy->sys_chunk_array + array_size; |
| 3256 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 3257 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 3258 | ptr += sizeof(disk_key); |
| 3259 | memcpy(ptr, chunk, item_size); |
| 3260 | item_size += sizeof(disk_key); |
| 3261 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 3262 | return 0; |
| 3263 | } |
| 3264 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3265 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3266 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3267 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3268 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3269 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3270 | const struct btrfs_device_info *di_a = a; |
| 3271 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3272 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3273 | if (di_a->max_avail > di_b->max_avail) |
| 3274 | return -1; |
| 3275 | if (di_a->max_avail < di_b->max_avail) |
| 3276 | return 1; |
| 3277 | if (di_a->total_avail > di_b->total_avail) |
| 3278 | return -1; |
| 3279 | if (di_a->total_avail < di_b->total_avail) |
| 3280 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3281 | return 0; |
| 3282 | } |
| 3283 | |
| 3284 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 3285 | struct btrfs_root *extent_root, |
| 3286 | struct map_lookup **map_ret, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3287 | u64 *num_bytes_out, u64 *stripe_size_out, |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3288 | u64 start, u64 type) |
| 3289 | { |
| 3290 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3291 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 3292 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3293 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3294 | struct extent_map_tree *em_tree; |
| 3295 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3296 | struct btrfs_device_info *devices_info = NULL; |
| 3297 | u64 total_avail; |
| 3298 | int num_stripes; /* total number of stripes to allocate */ |
| 3299 | int sub_stripes; /* sub_stripes info for map */ |
| 3300 | int dev_stripes; /* stripes per dev */ |
| 3301 | int devs_max; /* max devs to use */ |
| 3302 | int devs_min; /* min devs needed */ |
| 3303 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 3304 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3305 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3306 | u64 max_stripe_size; |
| 3307 | u64 max_chunk_size; |
| 3308 | u64 stripe_size; |
| 3309 | u64 num_bytes; |
| 3310 | int ndevs; |
| 3311 | int i; |
| 3312 | int j; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3313 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3314 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3315 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3316 | if (list_empty(&fs_devices->alloc_list)) |
| 3317 | return -ENOSPC; |
| 3318 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3319 | sub_stripes = 1; |
| 3320 | dev_stripes = 1; |
| 3321 | devs_increment = 1; |
| 3322 | ncopies = 1; |
| 3323 | devs_max = 0; /* 0 == as many as possible */ |
| 3324 | devs_min = 1; |
| 3325 | |
| 3326 | /* |
| 3327 | * define the properties of each RAID type. |
| 3328 | * FIXME: move this to a global table and use it in all RAID |
| 3329 | * calculation code |
| 3330 | */ |
| 3331 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { |
| 3332 | dev_stripes = 2; |
| 3333 | ncopies = 2; |
| 3334 | devs_max = 1; |
| 3335 | } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) { |
| 3336 | devs_min = 2; |
| 3337 | } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
| 3338 | devs_increment = 2; |
| 3339 | ncopies = 2; |
| 3340 | devs_max = 2; |
| 3341 | devs_min = 2; |
| 3342 | } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
| 3343 | sub_stripes = 2; |
| 3344 | devs_increment = 2; |
| 3345 | ncopies = 2; |
| 3346 | devs_min = 4; |
| 3347 | } else { |
| 3348 | devs_max = 1; |
| 3349 | } |
| 3350 | |
| 3351 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 3352 | max_stripe_size = 1024 * 1024 * 1024; |
| 3353 | max_chunk_size = 10 * max_stripe_size; |
| 3354 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 3355 | /* for larger filesystems, use larger metadata chunks */ |
| 3356 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 3357 | max_stripe_size = 1024 * 1024 * 1024; |
| 3358 | else |
| 3359 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3360 | max_chunk_size = max_stripe_size; |
| 3361 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 3362 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3363 | max_chunk_size = 2 * max_stripe_size; |
| 3364 | } else { |
| 3365 | printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n", |
| 3366 | type); |
| 3367 | BUG_ON(1); |
| 3368 | } |
| 3369 | |
| 3370 | /* we don't want a chunk larger than 10% of writeable space */ |
| 3371 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 3372 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3373 | |
| 3374 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 3375 | GFP_NOFS); |
| 3376 | if (!devices_info) |
| 3377 | return -ENOMEM; |
| 3378 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3379 | cur = fs_devices->alloc_list.next; |
| 3380 | |
| 3381 | /* |
| 3382 | * in the first pass through the devices list, we gather information |
| 3383 | * about the available holes on each device. |
| 3384 | */ |
| 3385 | ndevs = 0; |
| 3386 | while (cur != &fs_devices->alloc_list) { |
| 3387 | struct btrfs_device *device; |
| 3388 | u64 max_avail; |
| 3389 | u64 dev_offset; |
| 3390 | |
| 3391 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 3392 | |
| 3393 | cur = cur->next; |
| 3394 | |
| 3395 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3396 | WARN(1, KERN_ERR |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3397 | "btrfs: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3398 | continue; |
| 3399 | } |
| 3400 | |
| 3401 | if (!device->in_fs_metadata) |
| 3402 | continue; |
| 3403 | |
| 3404 | if (device->total_bytes > device->bytes_used) |
| 3405 | total_avail = device->total_bytes - device->bytes_used; |
| 3406 | else |
| 3407 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 3408 | |
| 3409 | /* If there is no space on this device, skip it. */ |
| 3410 | if (total_avail == 0) |
| 3411 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3412 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3413 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3414 | max_stripe_size * dev_stripes, |
| 3415 | &dev_offset, &max_avail); |
| 3416 | if (ret && ret != -ENOSPC) |
| 3417 | goto error; |
| 3418 | |
| 3419 | if (ret == 0) |
| 3420 | max_avail = max_stripe_size * dev_stripes; |
| 3421 | |
| 3422 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 3423 | continue; |
| 3424 | |
| 3425 | devices_info[ndevs].dev_offset = dev_offset; |
| 3426 | devices_info[ndevs].max_avail = max_avail; |
| 3427 | devices_info[ndevs].total_avail = total_avail; |
| 3428 | devices_info[ndevs].dev = device; |
| 3429 | ++ndevs; |
| 3430 | } |
| 3431 | |
| 3432 | /* |
| 3433 | * now sort the devices by hole size / available space |
| 3434 | */ |
| 3435 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 3436 | btrfs_cmp_device_info, NULL); |
| 3437 | |
| 3438 | /* round down to number of usable stripes */ |
| 3439 | ndevs -= ndevs % devs_increment; |
| 3440 | |
| 3441 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 3442 | ret = -ENOSPC; |
| 3443 | goto error; |
| 3444 | } |
| 3445 | |
| 3446 | if (devs_max && ndevs > devs_max) |
| 3447 | ndevs = devs_max; |
| 3448 | /* |
| 3449 | * the primary goal is to maximize the number of stripes, so use as many |
| 3450 | * devices as possible, even if the stripes are not maximum sized. |
| 3451 | */ |
| 3452 | stripe_size = devices_info[ndevs-1].max_avail; |
| 3453 | num_stripes = ndevs * dev_stripes; |
| 3454 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3455 | if (stripe_size * ndevs > max_chunk_size * ncopies) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3456 | stripe_size = max_chunk_size * ncopies; |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3457 | do_div(stripe_size, ndevs); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 3461 | |
| 3462 | /* align to BTRFS_STRIPE_LEN */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3463 | do_div(stripe_size, BTRFS_STRIPE_LEN); |
| 3464 | stripe_size *= BTRFS_STRIPE_LEN; |
| 3465 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3466 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 3467 | if (!map) { |
| 3468 | ret = -ENOMEM; |
| 3469 | goto error; |
| 3470 | } |
| 3471 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 3472 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3473 | for (i = 0; i < ndevs; ++i) { |
| 3474 | for (j = 0; j < dev_stripes; ++j) { |
| 3475 | int s = i * dev_stripes + j; |
| 3476 | map->stripes[s].dev = devices_info[i].dev; |
| 3477 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 3478 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 3479 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3480 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3481 | map->sector_size = extent_root->sectorsize; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3482 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 3483 | map->io_align = BTRFS_STRIPE_LEN; |
| 3484 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3485 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3486 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3487 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3488 | *map_ret = map; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3489 | num_bytes = stripe_size * (num_stripes / ncopies); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3490 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3491 | *stripe_size_out = stripe_size; |
| 3492 | *num_bytes_out = num_bytes; |
| 3493 | |
| 3494 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3495 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 3496 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3497 | if (!em) { |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3498 | ret = -ENOMEM; |
| 3499 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3500 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3501 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3502 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3503 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3504 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3505 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3506 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3507 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3508 | write_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3509 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3510 | write_unlock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3511 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 3512 | if (ret) |
| 3513 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3514 | |
| 3515 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 3516 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3517 | start, num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3518 | if (ret) |
| 3519 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3520 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3521 | for (i = 0; i < map->num_stripes; ++i) { |
| 3522 | struct btrfs_device *device; |
| 3523 | u64 dev_offset; |
| 3524 | |
| 3525 | device = map->stripes[i].dev; |
| 3526 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3527 | |
| 3528 | ret = btrfs_alloc_dev_extent(trans, device, |
| 3529 | info->chunk_root->root_key.objectid, |
| 3530 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3531 | start, dev_offset, stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3532 | if (ret) { |
| 3533 | btrfs_abort_transaction(trans, extent_root, ret); |
| 3534 | goto error; |
| 3535 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3536 | } |
| 3537 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3538 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3539 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3540 | |
| 3541 | error: |
| 3542 | kfree(map); |
| 3543 | kfree(devices_info); |
| 3544 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3548 | struct btrfs_root *extent_root, |
| 3549 | struct map_lookup *map, u64 chunk_offset, |
| 3550 | u64 chunk_size, u64 stripe_size) |
| 3551 | { |
| 3552 | u64 dev_offset; |
| 3553 | struct btrfs_key key; |
| 3554 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 3555 | struct btrfs_device *device; |
| 3556 | struct btrfs_chunk *chunk; |
| 3557 | struct btrfs_stripe *stripe; |
| 3558 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); |
| 3559 | int index = 0; |
| 3560 | int ret; |
| 3561 | |
| 3562 | chunk = kzalloc(item_size, GFP_NOFS); |
| 3563 | if (!chunk) |
| 3564 | return -ENOMEM; |
| 3565 | |
| 3566 | index = 0; |
| 3567 | while (index < map->num_stripes) { |
| 3568 | device = map->stripes[index].dev; |
| 3569 | device->bytes_used += stripe_size; |
| 3570 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 3571 | if (ret) |
| 3572 | goto out_free; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3573 | index++; |
| 3574 | } |
| 3575 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3576 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 3577 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 3578 | map->num_stripes); |
| 3579 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 3580 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3581 | index = 0; |
| 3582 | stripe = &chunk->stripe; |
| 3583 | while (index < map->num_stripes) { |
| 3584 | device = map->stripes[index].dev; |
| 3585 | dev_offset = map->stripes[index].physical; |
| 3586 | |
| 3587 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 3588 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 3589 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 3590 | stripe++; |
| 3591 | index++; |
| 3592 | } |
| 3593 | |
| 3594 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 3595 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 3596 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 3597 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 3598 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 3599 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 3600 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 3601 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 3602 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 3603 | |
| 3604 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3605 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3606 | key.offset = chunk_offset; |
| 3607 | |
| 3608 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3609 | |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 3610 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 3611 | /* |
| 3612 | * TODO: Cleanup of inserted chunk root in case of |
| 3613 | * failure. |
| 3614 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3615 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3616 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3617 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3618 | |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 3619 | out_free: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3620 | kfree(chunk); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 3621 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | /* |
| 3625 | * Chunk allocation falls into two parts. The first part does works |
| 3626 | * that make the new allocated chunk useable, but not do any operation |
| 3627 | * that modifies the chunk tree. The second part does the works that |
| 3628 | * require modifying the chunk tree. This division is important for the |
| 3629 | * bootstrap process of adding storage to a seed btrfs. |
| 3630 | */ |
| 3631 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 3632 | struct btrfs_root *extent_root, u64 type) |
| 3633 | { |
| 3634 | u64 chunk_offset; |
| 3635 | u64 chunk_size; |
| 3636 | u64 stripe_size; |
| 3637 | struct map_lookup *map; |
| 3638 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 3639 | int ret; |
| 3640 | |
| 3641 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 3642 | &chunk_offset); |
| 3643 | if (ret) |
| 3644 | return ret; |
| 3645 | |
| 3646 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 3647 | &stripe_size, chunk_offset, type); |
| 3648 | if (ret) |
| 3649 | return ret; |
| 3650 | |
| 3651 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 3652 | chunk_size, stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3653 | if (ret) |
| 3654 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3655 | return 0; |
| 3656 | } |
| 3657 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3658 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3659 | struct btrfs_root *root, |
| 3660 | struct btrfs_device *device) |
| 3661 | { |
| 3662 | u64 chunk_offset; |
| 3663 | u64 sys_chunk_offset; |
| 3664 | u64 chunk_size; |
| 3665 | u64 sys_chunk_size; |
| 3666 | u64 stripe_size; |
| 3667 | u64 sys_stripe_size; |
| 3668 | u64 alloc_profile; |
| 3669 | struct map_lookup *map; |
| 3670 | struct map_lookup *sys_map; |
| 3671 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3672 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 3673 | int ret; |
| 3674 | |
| 3675 | ret = find_next_chunk(fs_info->chunk_root, |
| 3676 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3677 | if (ret) |
| 3678 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3679 | |
| 3680 | alloc_profile = BTRFS_BLOCK_GROUP_METADATA | |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3681 | fs_info->avail_metadata_alloc_bits; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3682 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); |
| 3683 | |
| 3684 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, |
| 3685 | &stripe_size, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3686 | if (ret) |
| 3687 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3688 | |
| 3689 | sys_chunk_offset = chunk_offset + chunk_size; |
| 3690 | |
| 3691 | alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM | |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3692 | fs_info->avail_system_alloc_bits; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3693 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); |
| 3694 | |
| 3695 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, |
| 3696 | &sys_chunk_size, &sys_stripe_size, |
| 3697 | sys_chunk_offset, alloc_profile); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3698 | if (ret) { |
| 3699 | btrfs_abort_transaction(trans, root, ret); |
| 3700 | goto out; |
| 3701 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3702 | |
| 3703 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3704 | if (ret) { |
| 3705 | btrfs_abort_transaction(trans, root, ret); |
| 3706 | goto out; |
| 3707 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3708 | |
| 3709 | /* |
| 3710 | * Modifying chunk tree needs allocating new blocks from both |
| 3711 | * system block group and metadata block group. So we only can |
| 3712 | * do operations require modifying the chunk tree after both |
| 3713 | * block groups were created. |
| 3714 | */ |
| 3715 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, |
| 3716 | chunk_size, stripe_size); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3717 | if (ret) { |
| 3718 | btrfs_abort_transaction(trans, root, ret); |
| 3719 | goto out; |
| 3720 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3721 | |
| 3722 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, |
| 3723 | sys_chunk_offset, sys_chunk_size, |
| 3724 | sys_stripe_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3725 | if (ret) |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3726 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3727 | |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3728 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3729 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3730 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 3734 | { |
| 3735 | struct extent_map *em; |
| 3736 | struct map_lookup *map; |
| 3737 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 3738 | int readonly = 0; |
| 3739 | int i; |
| 3740 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3741 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3742 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3743 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3744 | if (!em) |
| 3745 | return 1; |
| 3746 | |
Josef Bacik | f48b907 | 2010-01-27 02:07:59 +0000 | [diff] [blame] | 3747 | if (btrfs_test_opt(root, DEGRADED)) { |
| 3748 | free_extent_map(em); |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3752 | map = (struct map_lookup *)em->bdev; |
| 3753 | for (i = 0; i < map->num_stripes; i++) { |
| 3754 | if (!map->stripes[i].dev->writeable) { |
| 3755 | readonly = 1; |
| 3756 | break; |
| 3757 | } |
| 3758 | } |
| 3759 | free_extent_map(em); |
| 3760 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 3764 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 3765 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 3769 | { |
| 3770 | struct extent_map *em; |
| 3771 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3772 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3773 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3774 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 3775 | if (em) |
| 3776 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3777 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3778 | if (!em) |
| 3779 | break; |
| 3780 | kfree(em->bdev); |
| 3781 | /* once for us */ |
| 3782 | free_extent_map(em); |
| 3783 | /* once for the tree */ |
| 3784 | free_extent_map(em); |
| 3785 | } |
| 3786 | } |
| 3787 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3788 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
| 3789 | { |
| 3790 | struct extent_map *em; |
| 3791 | struct map_lookup *map; |
| 3792 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 3793 | int ret; |
| 3794 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3795 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3796 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3797 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3798 | BUG_ON(!em); |
| 3799 | |
| 3800 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 3801 | map = (struct map_lookup *)em->bdev; |
| 3802 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 3803 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3804 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 3805 | ret = map->sub_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3806 | else |
| 3807 | ret = 1; |
| 3808 | free_extent_map(em); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3809 | return ret; |
| 3810 | } |
| 3811 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3812 | static int find_live_mirror(struct map_lookup *map, int first, int num, |
| 3813 | int optimal) |
| 3814 | { |
| 3815 | int i; |
| 3816 | if (map->stripes[optimal].dev->bdev) |
| 3817 | return optimal; |
| 3818 | for (i = first; i < first + num; i++) { |
| 3819 | if (map->stripes[i].dev->bdev) |
| 3820 | return i; |
| 3821 | } |
| 3822 | /* we couldn't find one that doesn't fail. Just return something |
| 3823 | * and the io error handling code will clean up eventually |
| 3824 | */ |
| 3825 | return optimal; |
| 3826 | } |
| 3827 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3828 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 3829 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3830 | struct btrfs_bio **bbio_ret, |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 3831 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3832 | { |
| 3833 | struct extent_map *em; |
| 3834 | struct map_lookup *map; |
| 3835 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 3836 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3837 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3838 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3839 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3840 | u64 stripe_nr_orig; |
| 3841 | u64 stripe_nr_end; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3842 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3843 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 3844 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3845 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 3846 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3847 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3848 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3849 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3850 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 3851 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3852 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3853 | if (!em) { |
Daniel J Blueman | 4894066 | 2012-05-07 06:35:38 -0600 | [diff] [blame] | 3854 | printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3855 | (unsigned long long)logical, |
| 3856 | (unsigned long long)*length); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3857 | BUG(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3858 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3859 | |
| 3860 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 3861 | map = (struct map_lookup *)em->bdev; |
| 3862 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3863 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3864 | if (mirror_num > map->num_stripes) |
| 3865 | mirror_num = 0; |
| 3866 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3867 | stripe_nr = offset; |
| 3868 | /* |
| 3869 | * stripe_nr counts the total number of stripes we have to stride |
| 3870 | * to get to this block |
| 3871 | */ |
| 3872 | do_div(stripe_nr, map->stripe_len); |
| 3873 | |
| 3874 | stripe_offset = stripe_nr * map->stripe_len; |
| 3875 | BUG_ON(offset < stripe_offset); |
| 3876 | |
| 3877 | /* stripe_offset is the offset of this block in its stripe*/ |
| 3878 | stripe_offset = offset - stripe_offset; |
| 3879 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3880 | if (rw & REQ_DISCARD) |
| 3881 | *length = min_t(u64, em->len - offset, *length); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3882 | else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3883 | /* we limit the length of each bio to what fits in a stripe */ |
| 3884 | *length = min_t(u64, em->len - offset, |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3885 | map->stripe_len - stripe_offset); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3886 | } else { |
| 3887 | *length = em->len - offset; |
| 3888 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3889 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3890 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3891 | goto out; |
| 3892 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3893 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3894 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3895 | stripe_nr_orig = stripe_nr; |
| 3896 | stripe_nr_end = (offset + *length + map->stripe_len - 1) & |
| 3897 | (~(map->stripe_len - 1)); |
| 3898 | do_div(stripe_nr_end, map->stripe_len); |
| 3899 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 3900 | (offset + *length); |
| 3901 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 3902 | if (rw & REQ_DISCARD) |
| 3903 | num_stripes = min_t(u64, map->num_stripes, |
| 3904 | stripe_nr_end - stripe_nr_orig); |
| 3905 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 3906 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 3907 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3908 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 3909 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3910 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3911 | else { |
| 3912 | stripe_index = find_live_mirror(map, 0, |
| 3913 | map->num_stripes, |
| 3914 | current->pid % map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3915 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3916 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 3917 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3918 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3919 | if (rw & (REQ_WRITE | REQ_DISCARD)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3920 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3921 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3922 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3923 | } else { |
| 3924 | mirror_num = 1; |
| 3925 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 3926 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3927 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 3928 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3929 | |
| 3930 | stripe_index = do_div(stripe_nr, factor); |
| 3931 | stripe_index *= map->sub_stripes; |
| 3932 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 3933 | if (rw & REQ_WRITE) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3934 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3935 | else if (rw & REQ_DISCARD) |
| 3936 | num_stripes = min_t(u64, map->sub_stripes * |
| 3937 | (stripe_nr_end - stripe_nr_orig), |
| 3938 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3939 | else if (mirror_num) |
| 3940 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3941 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 3942 | int old_stripe_index = stripe_index; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3943 | stripe_index = find_live_mirror(map, stripe_index, |
| 3944 | map->sub_stripes, stripe_index + |
| 3945 | current->pid % map->sub_stripes); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 3946 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 3947 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3948 | } else { |
| 3949 | /* |
| 3950 | * after this do_div call, stripe_nr is the number of stripes |
| 3951 | * on this device we have to walk to find the data, and |
| 3952 | * stripe_index is the number of our device in the stripe array |
| 3953 | */ |
| 3954 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3955 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3956 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3957 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3958 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 3959 | bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS); |
| 3960 | if (!bbio) { |
| 3961 | ret = -ENOMEM; |
| 3962 | goto out; |
| 3963 | } |
| 3964 | atomic_set(&bbio->error, 0); |
| 3965 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3966 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 3967 | int factor = 0; |
| 3968 | int sub_stripes = 0; |
| 3969 | u64 stripes_per_dev = 0; |
| 3970 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3971 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 3972 | |
| 3973 | if (map->type & |
| 3974 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3975 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 3976 | sub_stripes = 1; |
| 3977 | else |
| 3978 | sub_stripes = map->sub_stripes; |
| 3979 | |
| 3980 | factor = map->num_stripes / sub_stripes; |
| 3981 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 3982 | stripe_nr_orig, |
| 3983 | factor, |
| 3984 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3985 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 3986 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 3987 | } |
| 3988 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3989 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3990 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 3991 | map->stripes[stripe_index].physical + |
| 3992 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 3993 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 3994 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 3995 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 3996 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3997 | bbio->stripes[i].length = stripes_per_dev * |
| 3998 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3999 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4000 | if (i / sub_stripes < remaining_stripes) |
| 4001 | bbio->stripes[i].length += |
| 4002 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4003 | |
| 4004 | /* |
| 4005 | * Special for the first stripe and |
| 4006 | * the last stripe: |
| 4007 | * |
| 4008 | * |-------|...|-------| |
| 4009 | * |----------| |
| 4010 | * off end_off |
| 4011 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4012 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4013 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4014 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4015 | |
| 4016 | if (stripe_index >= last_stripe && |
| 4017 | stripe_index <= (last_stripe + |
| 4018 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4019 | bbio->stripes[i].length -= |
| 4020 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4021 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 4022 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4023 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4024 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4025 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4026 | |
| 4027 | stripe_index++; |
| 4028 | if (stripe_index == map->num_stripes) { |
| 4029 | /* This could only happen for RAID0/10 */ |
| 4030 | stripe_index = 0; |
| 4031 | stripe_nr++; |
| 4032 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4033 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4034 | } else { |
| 4035 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4036 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 4037 | map->stripes[stripe_index].physical + |
| 4038 | stripe_offset + |
| 4039 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4040 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 4041 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4042 | stripe_index++; |
| 4043 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4044 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4045 | |
| 4046 | if (rw & REQ_WRITE) { |
| 4047 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4048 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4049 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4050 | max_errors = 1; |
| 4051 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4052 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4053 | |
| 4054 | *bbio_ret = bbio; |
| 4055 | bbio->num_stripes = num_stripes; |
| 4056 | bbio->max_errors = max_errors; |
| 4057 | bbio->mirror_num = mirror_num; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4058 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4059 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4060 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4061 | } |
| 4062 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4063 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
| 4064 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4065 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4066 | { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4067 | return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret, |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 4068 | mirror_num); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4069 | } |
| 4070 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4071 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 4072 | u64 chunk_start, u64 physical, u64 devid, |
| 4073 | u64 **logical, int *naddrs, int *stripe_len) |
| 4074 | { |
| 4075 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4076 | struct extent_map *em; |
| 4077 | struct map_lookup *map; |
| 4078 | u64 *buf; |
| 4079 | u64 bytenr; |
| 4080 | u64 length; |
| 4081 | u64 stripe_nr; |
| 4082 | int i, j, nr = 0; |
| 4083 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4084 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4085 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4086 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4087 | |
| 4088 | BUG_ON(!em || em->start != chunk_start); |
| 4089 | map = (struct map_lookup *)em->bdev; |
| 4090 | |
| 4091 | length = em->len; |
| 4092 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4093 | do_div(length, map->num_stripes / map->sub_stripes); |
| 4094 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 4095 | do_div(length, map->num_stripes); |
| 4096 | |
| 4097 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4098 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4099 | |
| 4100 | for (i = 0; i < map->num_stripes; i++) { |
| 4101 | if (devid && map->stripes[i].dev->devid != devid) |
| 4102 | continue; |
| 4103 | if (map->stripes[i].physical > physical || |
| 4104 | map->stripes[i].physical + length <= physical) |
| 4105 | continue; |
| 4106 | |
| 4107 | stripe_nr = physical - map->stripes[i].physical; |
| 4108 | do_div(stripe_nr, map->stripe_len); |
| 4109 | |
| 4110 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 4111 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 4112 | do_div(stripe_nr, map->sub_stripes); |
| 4113 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4114 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 4115 | } |
| 4116 | bytenr = chunk_start + stripe_nr * map->stripe_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4117 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4118 | for (j = 0; j < nr; j++) { |
| 4119 | if (buf[j] == bytenr) |
| 4120 | break; |
| 4121 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4122 | if (j == nr) { |
| 4123 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4124 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4125 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4126 | } |
| 4127 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 4128 | *logical = buf; |
| 4129 | *naddrs = nr; |
| 4130 | *stripe_len = map->stripe_len; |
| 4131 | |
| 4132 | free_extent_map(em); |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4136 | static void *merge_stripe_index_into_bio_private(void *bi_private, |
| 4137 | unsigned int stripe_index) |
| 4138 | { |
| 4139 | /* |
| 4140 | * with single, dup, RAID0, RAID1 and RAID10, stripe_index is |
| 4141 | * at most 1. |
| 4142 | * The alternative solution (instead of stealing bits from the |
| 4143 | * pointer) would be to allocate an intermediate structure |
| 4144 | * that contains the old private pointer plus the stripe_index. |
| 4145 | */ |
| 4146 | BUG_ON((((uintptr_t)bi_private) & 3) != 0); |
| 4147 | BUG_ON(stripe_index > 3); |
| 4148 | return (void *)(((uintptr_t)bi_private) | stripe_index); |
| 4149 | } |
| 4150 | |
| 4151 | static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private) |
| 4152 | { |
| 4153 | return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3)); |
| 4154 | } |
| 4155 | |
| 4156 | static unsigned int extract_stripe_index_from_bio_private(void *bi_private) |
| 4157 | { |
| 4158 | return (unsigned int)((uintptr_t)bi_private) & 3; |
| 4159 | } |
| 4160 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4161 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4162 | { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4163 | struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4164 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4165 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4166 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4167 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4168 | if (err == -EIO || err == -EREMOTEIO) { |
| 4169 | unsigned int stripe_index = |
| 4170 | extract_stripe_index_from_bio_private( |
| 4171 | bio->bi_private); |
| 4172 | struct btrfs_device *dev; |
| 4173 | |
| 4174 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 4175 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 4176 | if (dev->bdev) { |
| 4177 | if (bio->bi_rw & WRITE) |
| 4178 | btrfs_dev_stat_inc(dev, |
| 4179 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 4180 | else |
| 4181 | btrfs_dev_stat_inc(dev, |
| 4182 | BTRFS_DEV_STAT_READ_ERRS); |
| 4183 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 4184 | btrfs_dev_stat_inc(dev, |
| 4185 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 4186 | btrfs_dev_stat_print_on_error(dev); |
| 4187 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4188 | } |
| 4189 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4190 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4191 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4192 | is_orig_bio = 1; |
| 4193 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4194 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4195 | if (!is_orig_bio) { |
| 4196 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4197 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4198 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4199 | bio->bi_private = bbio->private; |
| 4200 | bio->bi_end_io = bbio->end_io; |
Jan Schmidt | 2774b2ca | 2011-06-16 12:05:19 +0200 | [diff] [blame] | 4201 | bio->bi_bdev = (struct block_device *) |
| 4202 | (unsigned long)bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4203 | /* only send an error to the higher layers if it is |
| 4204 | * beyond the tolerance of the multi-bio |
| 4205 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4206 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4207 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 4208 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4209 | /* |
| 4210 | * this bio is actually up to date, we didn't |
| 4211 | * go over the max number of errors |
| 4212 | */ |
| 4213 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4214 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 4215 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4216 | kfree(bbio); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4217 | |
| 4218 | bio_endio(bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 4219 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4220 | bio_put(bio); |
| 4221 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4222 | } |
| 4223 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4224 | struct async_sched { |
| 4225 | struct bio *bio; |
| 4226 | int rw; |
| 4227 | struct btrfs_fs_info *info; |
| 4228 | struct btrfs_work work; |
| 4229 | }; |
| 4230 | |
| 4231 | /* |
| 4232 | * see run_scheduled_bios for a description of why bios are collected for |
| 4233 | * async submit. |
| 4234 | * |
| 4235 | * This will add one bio to the pending list for a device and make sure |
| 4236 | * the work struct is scheduled. |
| 4237 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4238 | static noinline void schedule_bio(struct btrfs_root *root, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 4239 | struct btrfs_device *device, |
| 4240 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4241 | { |
| 4242 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4243 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4244 | |
| 4245 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 4246 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 4247 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 4248 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 4249 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4250 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 4254 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4255 | * higher layers. Otherwise, the async bio makes it appear we have |
| 4256 | * made progress against dirty pages when we've really just put it |
| 4257 | * on a queue for later |
| 4258 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 4259 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 4260 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4261 | bio->bi_next = NULL; |
| 4262 | bio->bi_rw |= rw; |
| 4263 | |
| 4264 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 4265 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4266 | pending_bios = &device->pending_sync_bios; |
| 4267 | else |
| 4268 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4269 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4270 | if (pending_bios->tail) |
| 4271 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4272 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4273 | pending_bios->tail = bio; |
| 4274 | if (!pending_bios->head) |
| 4275 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4276 | if (device->running_pending) |
| 4277 | should_queue = 0; |
| 4278 | |
| 4279 | spin_unlock(&device->io_lock); |
| 4280 | |
| 4281 | if (should_queue) |
Chris Mason | 1cc127b | 2008-06-12 14:46:17 -0400 | [diff] [blame] | 4282 | btrfs_queue_worker(&root->fs_info->submit_workers, |
| 4283 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4284 | } |
| 4285 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4286 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 4287 | sector_t sector) |
| 4288 | { |
| 4289 | struct bio_vec *prev; |
| 4290 | struct request_queue *q = bdev_get_queue(bdev); |
| 4291 | unsigned short max_sectors = queue_max_sectors(q); |
| 4292 | struct bvec_merge_data bvm = { |
| 4293 | .bi_bdev = bdev, |
| 4294 | .bi_sector = sector, |
| 4295 | .bi_rw = bio->bi_rw, |
| 4296 | }; |
| 4297 | |
| 4298 | if (bio->bi_vcnt == 0) { |
| 4299 | WARN_ON(1); |
| 4300 | return 1; |
| 4301 | } |
| 4302 | |
| 4303 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
| 4304 | if ((bio->bi_size >> 9) > max_sectors) |
| 4305 | return 0; |
| 4306 | |
| 4307 | if (!q->merge_bvec_fn) |
| 4308 | return 1; |
| 4309 | |
| 4310 | bvm.bi_size = bio->bi_size - prev->bv_len; |
| 4311 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 4312 | return 0; |
| 4313 | return 1; |
| 4314 | } |
| 4315 | |
| 4316 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 4317 | struct bio *bio, u64 physical, int dev_nr, |
| 4318 | int rw, int async) |
| 4319 | { |
| 4320 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 4321 | |
| 4322 | bio->bi_private = bbio; |
| 4323 | bio->bi_private = merge_stripe_index_into_bio_private( |
| 4324 | bio->bi_private, (unsigned int)dev_nr); |
| 4325 | bio->bi_end_io = btrfs_end_bio; |
| 4326 | bio->bi_sector = physical >> 9; |
| 4327 | #ifdef DEBUG |
| 4328 | { |
| 4329 | struct rcu_string *name; |
| 4330 | |
| 4331 | rcu_read_lock(); |
| 4332 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 4333 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4334 | "(%s id %llu), size=%u\n", rw, |
| 4335 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 4336 | name->str, dev->devid, bio->bi_size); |
| 4337 | rcu_read_unlock(); |
| 4338 | } |
| 4339 | #endif |
| 4340 | bio->bi_bdev = dev->bdev; |
| 4341 | if (async) |
| 4342 | schedule_bio(root, dev, rw, bio); |
| 4343 | else |
| 4344 | btrfsic_submit_bio(rw, bio); |
| 4345 | } |
| 4346 | |
| 4347 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 4348 | struct bio *first_bio, struct btrfs_device *dev, |
| 4349 | int dev_nr, int rw, int async) |
| 4350 | { |
| 4351 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 4352 | struct bio *bio; |
| 4353 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 4354 | u64 physical = bbio->stripes[dev_nr].physical; |
| 4355 | |
| 4356 | again: |
| 4357 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 4358 | if (!bio) |
| 4359 | return -ENOMEM; |
| 4360 | |
| 4361 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 4362 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 4363 | bvec->bv_offset) < bvec->bv_len) { |
| 4364 | u64 len = bio->bi_size; |
| 4365 | |
| 4366 | atomic_inc(&bbio->stripes_pending); |
| 4367 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 4368 | rw, async); |
| 4369 | physical += len; |
| 4370 | goto again; |
| 4371 | } |
| 4372 | bvec++; |
| 4373 | } |
| 4374 | |
| 4375 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 4376 | return 0; |
| 4377 | } |
| 4378 | |
| 4379 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 4380 | { |
| 4381 | atomic_inc(&bbio->error); |
| 4382 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
| 4383 | bio->bi_private = bbio->private; |
| 4384 | bio->bi_end_io = bbio->end_io; |
| 4385 | bio->bi_bdev = (struct block_device *) |
| 4386 | (unsigned long)bbio->mirror_num; |
| 4387 | bio->bi_sector = logical >> 9; |
| 4388 | kfree(bbio); |
| 4389 | bio_endio(bio, -EIO); |
| 4390 | } |
| 4391 | } |
| 4392 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4393 | 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] | 4394 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4395 | { |
| 4396 | struct btrfs_mapping_tree *map_tree; |
| 4397 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4398 | struct bio *first_bio = bio; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 4399 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4400 | u64 length = 0; |
| 4401 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4402 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4403 | int dev_nr = 0; |
| 4404 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4405 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4406 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4407 | length = bio->bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4408 | map_tree = &root->fs_info->mapping_tree; |
| 4409 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4410 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4411 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4412 | mirror_num); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4413 | if (ret) /* -ENOMEM */ |
| 4414 | return ret; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4415 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4416 | total_devs = bbio->num_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4417 | if (map_length < length) { |
Daniel J Blueman | 4894066 | 2012-05-07 06:35:38 -0600 | [diff] [blame] | 4418 | printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu " |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4419 | "len %llu\n", (unsigned long long)logical, |
| 4420 | (unsigned long long)length, |
| 4421 | (unsigned long long)map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4422 | BUG(); |
| 4423 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4424 | |
| 4425 | bbio->orig_bio = first_bio; |
| 4426 | bbio->private = first_bio->bi_private; |
| 4427 | bbio->end_io = first_bio->bi_end_io; |
| 4428 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4429 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4430 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4431 | dev = bbio->stripes[dev_nr].dev; |
| 4432 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 4433 | bbio_error(bbio, first_bio, logical); |
| 4434 | dev_nr++; |
| 4435 | continue; |
| 4436 | } |
| 4437 | |
| 4438 | /* |
| 4439 | * Check and see if we're ok with this bio based on it's size |
| 4440 | * and offset with the given device. |
| 4441 | */ |
| 4442 | if (!bio_size_ok(dev->bdev, first_bio, |
| 4443 | bbio->stripes[dev_nr].physical >> 9)) { |
| 4444 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 4445 | dev_nr, rw, async_submit); |
| 4446 | BUG_ON(ret); |
| 4447 | dev_nr++; |
| 4448 | continue; |
| 4449 | } |
| 4450 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4451 | if (dev_nr < total_devs - 1) { |
| 4452 | bio = bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4453 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4454 | } else { |
| 4455 | bio = first_bio; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4456 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 4457 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 4458 | submit_stripe_bio(root, bbio, bio, |
| 4459 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 4460 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4461 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4462 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4463 | return 0; |
| 4464 | } |
| 4465 | |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 4466 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4467 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4468 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4469 | struct btrfs_device *device; |
| 4470 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4471 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4472 | cur_devices = root->fs_info->fs_devices; |
| 4473 | while (cur_devices) { |
| 4474 | if (!fsid || |
| 4475 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 4476 | device = __find_device(&cur_devices->devices, |
| 4477 | devid, uuid); |
| 4478 | if (device) |
| 4479 | return device; |
| 4480 | } |
| 4481 | cur_devices = cur_devices->seed; |
| 4482 | } |
| 4483 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4484 | } |
| 4485 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4486 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 4487 | u64 devid, u8 *dev_uuid) |
| 4488 | { |
| 4489 | struct btrfs_device *device; |
| 4490 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 4491 | |
| 4492 | device = kzalloc(sizeof(*device), GFP_NOFS); |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 4493 | if (!device) |
| 4494 | return NULL; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4495 | list_add(&device->dev_list, |
| 4496 | &fs_devices->devices); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4497 | device->dev_root = root->fs_info->dev_root; |
| 4498 | device->devid = devid; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 4499 | device->work.func = pending_bios_fn; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4500 | device->fs_devices = fs_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 4501 | device->missing = 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4502 | fs_devices->num_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 4503 | fs_devices->missing_devices++; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4504 | spin_lock_init(&device->io_lock); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 4505 | INIT_LIST_HEAD(&device->dev_alloc_list); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4506 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
| 4507 | return device; |
| 4508 | } |
| 4509 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4510 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 4511 | struct extent_buffer *leaf, |
| 4512 | struct btrfs_chunk *chunk) |
| 4513 | { |
| 4514 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4515 | struct map_lookup *map; |
| 4516 | struct extent_map *em; |
| 4517 | u64 logical; |
| 4518 | u64 length; |
| 4519 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 4520 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4521 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4522 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4523 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4524 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 4525 | logical = key->offset; |
| 4526 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4527 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4528 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4529 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4530 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4531 | |
| 4532 | /* already mapped? */ |
| 4533 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 4534 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4535 | return 0; |
| 4536 | } else if (em) { |
| 4537 | free_extent_map(em); |
| 4538 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4539 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4540 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4541 | if (!em) |
| 4542 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4543 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 4544 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4545 | if (!map) { |
| 4546 | free_extent_map(em); |
| 4547 | return -ENOMEM; |
| 4548 | } |
| 4549 | |
| 4550 | em->bdev = (struct block_device *)map; |
| 4551 | em->start = logical; |
| 4552 | em->len = length; |
| 4553 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4554 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4555 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4556 | map->num_stripes = num_stripes; |
| 4557 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 4558 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 4559 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 4560 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 4561 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4562 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4563 | for (i = 0; i < num_stripes; i++) { |
| 4564 | map->stripes[i].physical = |
| 4565 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 4566 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 4567 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 4568 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 4569 | BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4570 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid, |
| 4571 | NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4572 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4573 | kfree(map); |
| 4574 | free_extent_map(em); |
| 4575 | return -EIO; |
| 4576 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4577 | if (!map->stripes[i].dev) { |
| 4578 | map->stripes[i].dev = |
| 4579 | add_missing_dev(root, devid, uuid); |
| 4580 | if (!map->stripes[i].dev) { |
| 4581 | kfree(map); |
| 4582 | free_extent_map(em); |
| 4583 | return -EIO; |
| 4584 | } |
| 4585 | } |
| 4586 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4587 | } |
| 4588 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4589 | write_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4590 | ret = add_extent_mapping(&map_tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4591 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4592 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4593 | free_extent_map(em); |
| 4594 | |
| 4595 | return 0; |
| 4596 | } |
| 4597 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4598 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4599 | struct btrfs_dev_item *dev_item, |
| 4600 | struct btrfs_device *device) |
| 4601 | { |
| 4602 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4603 | |
| 4604 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4605 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 4606 | device->total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4607 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 4608 | device->type = btrfs_device_type(leaf, dev_item); |
| 4609 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 4610 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 4611 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4612 | |
| 4613 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 4614 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4615 | } |
| 4616 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4617 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 4618 | { |
| 4619 | struct btrfs_fs_devices *fs_devices; |
| 4620 | int ret; |
| 4621 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 4622 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4623 | |
| 4624 | fs_devices = root->fs_info->fs_devices->seed; |
| 4625 | while (fs_devices) { |
| 4626 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 4627 | ret = 0; |
| 4628 | goto out; |
| 4629 | } |
| 4630 | fs_devices = fs_devices->seed; |
| 4631 | } |
| 4632 | |
| 4633 | fs_devices = find_fsid(fsid); |
| 4634 | if (!fs_devices) { |
| 4635 | ret = -ENOENT; |
| 4636 | goto out; |
| 4637 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4638 | |
| 4639 | fs_devices = clone_fs_devices(fs_devices); |
| 4640 | if (IS_ERR(fs_devices)) { |
| 4641 | ret = PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4642 | goto out; |
| 4643 | } |
| 4644 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 4645 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 4646 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 4647 | if (ret) { |
| 4648 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4649 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 4650 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4651 | |
| 4652 | if (!fs_devices->seeding) { |
| 4653 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4654 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4655 | ret = -EINVAL; |
| 4656 | goto out; |
| 4657 | } |
| 4658 | |
| 4659 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 4660 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4661 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4662 | return ret; |
| 4663 | } |
| 4664 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4665 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4666 | struct extent_buffer *leaf, |
| 4667 | struct btrfs_dev_item *dev_item) |
| 4668 | { |
| 4669 | struct btrfs_device *device; |
| 4670 | u64 devid; |
| 4671 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4672 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 4673 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 4674 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4675 | devid = btrfs_device_id(leaf, dev_item); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 4676 | read_extent_buffer(leaf, dev_uuid, |
| 4677 | (unsigned long)btrfs_device_uuid(dev_item), |
| 4678 | BTRFS_UUID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4679 | read_extent_buffer(leaf, fs_uuid, |
| 4680 | (unsigned long)btrfs_device_fsid(dev_item), |
| 4681 | BTRFS_UUID_SIZE); |
| 4682 | |
| 4683 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 4684 | ret = open_seed_devices(root, fs_uuid); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4685 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4686 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); |
| 4690 | if (!device || !device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4691 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4692 | return -EIO; |
| 4693 | |
| 4694 | if (!device) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4695 | printk(KERN_WARNING "warning devid %llu missing\n", |
| 4696 | (unsigned long long)devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4697 | device = add_missing_dev(root, devid, dev_uuid); |
| 4698 | if (!device) |
| 4699 | return -ENOMEM; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 4700 | } else if (!device->missing) { |
| 4701 | /* |
| 4702 | * this happens when a device that was properly setup |
| 4703 | * in the device info lists suddenly goes bad. |
| 4704 | * device->bdev is NULL, and so we have to set |
| 4705 | * device->missing to one here |
| 4706 | */ |
| 4707 | root->fs_info->fs_devices->missing_devices++; |
| 4708 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 4713 | BUG_ON(device->writeable); |
| 4714 | if (device->generation != |
| 4715 | btrfs_device_generation(leaf, dev_item)) |
| 4716 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4717 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4718 | |
| 4719 | fill_device_from_item(leaf, dev_item, device); |
| 4720 | device->dev_root = root->fs_info->dev_root; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4721 | device->in_fs_metadata = 1; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4722 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4723 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4724 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4725 | root->fs_info->free_chunk_space += device->total_bytes - |
| 4726 | device->bytes_used; |
| 4727 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4728 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4729 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4730 | return ret; |
| 4731 | } |
| 4732 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4733 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4734 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4735 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4736 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4737 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4738 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 4739 | u8 *ptr; |
| 4740 | unsigned long sb_ptr; |
| 4741 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4742 | u32 num_stripes; |
| 4743 | u32 array_size; |
| 4744 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4745 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 4746 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4747 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 4748 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4749 | BTRFS_SUPER_INFO_SIZE); |
| 4750 | if (!sb) |
| 4751 | return -ENOMEM; |
| 4752 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 4753 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 4754 | /* |
| 4755 | * The sb extent buffer is artifical and just used to read the system array. |
| 4756 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 4757 | * pages up-to-date when the page is larger: extent does not cover the |
| 4758 | * whole page and consequently check_page_uptodate does not find all |
| 4759 | * the page's extents up-to-date (the hole beyond sb), |
| 4760 | * write_extent_buffer then triggers a WARN_ON. |
| 4761 | * |
| 4762 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 4763 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 4764 | * to silence the warning eg. on PowerPC 64. |
| 4765 | */ |
| 4766 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4767 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4768 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4769 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4770 | array_size = btrfs_super_sys_array_size(super_copy); |
| 4771 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4772 | ptr = super_copy->sys_chunk_array; |
| 4773 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 4774 | cur = 0; |
| 4775 | |
| 4776 | while (cur < array_size) { |
| 4777 | disk_key = (struct btrfs_disk_key *)ptr; |
| 4778 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 4779 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4780 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4781 | sb_ptr += len; |
| 4782 | cur += len; |
| 4783 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4784 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4785 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4786 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 4787 | if (ret) |
| 4788 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4789 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 4790 | len = btrfs_chunk_item_size(num_stripes); |
| 4791 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 4792 | ret = -EIO; |
| 4793 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4794 | } |
| 4795 | ptr += len; |
| 4796 | sb_ptr += len; |
| 4797 | cur += len; |
| 4798 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4799 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 4800 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 4804 | { |
| 4805 | struct btrfs_path *path; |
| 4806 | struct extent_buffer *leaf; |
| 4807 | struct btrfs_key key; |
| 4808 | struct btrfs_key found_key; |
| 4809 | int ret; |
| 4810 | int slot; |
| 4811 | |
| 4812 | root = root->fs_info->chunk_root; |
| 4813 | |
| 4814 | path = btrfs_alloc_path(); |
| 4815 | if (!path) |
| 4816 | return -ENOMEM; |
| 4817 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 4818 | mutex_lock(&uuid_mutex); |
| 4819 | lock_chunks(root); |
| 4820 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4821 | /* first we search for all of the device items, and then we |
| 4822 | * read in all of the chunk items. This way we can create chunk |
| 4823 | * mappings that reference all of the devices that are afound |
| 4824 | */ |
| 4825 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 4826 | key.offset = 0; |
| 4827 | key.type = 0; |
| 4828 | again: |
| 4829 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 4830 | if (ret < 0) |
| 4831 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4832 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4833 | leaf = path->nodes[0]; |
| 4834 | slot = path->slots[0]; |
| 4835 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4836 | ret = btrfs_next_leaf(root, path); |
| 4837 | if (ret == 0) |
| 4838 | continue; |
| 4839 | if (ret < 0) |
| 4840 | goto error; |
| 4841 | break; |
| 4842 | } |
| 4843 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4844 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 4845 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) |
| 4846 | break; |
| 4847 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 4848 | struct btrfs_dev_item *dev_item; |
| 4849 | dev_item = btrfs_item_ptr(leaf, slot, |
| 4850 | struct btrfs_dev_item); |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4851 | ret = read_one_dev(root, leaf, dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4852 | if (ret) |
| 4853 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4854 | } |
| 4855 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 4856 | struct btrfs_chunk *chunk; |
| 4857 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 4858 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4859 | if (ret) |
| 4860 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4861 | } |
| 4862 | path->slots[0]++; |
| 4863 | } |
| 4864 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { |
| 4865 | key.objectid = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4866 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4867 | goto again; |
| 4868 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4869 | ret = 0; |
| 4870 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 4871 | unlock_chunks(root); |
| 4872 | mutex_unlock(&uuid_mutex); |
| 4873 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4874 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4875 | return ret; |
| 4876 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 4877 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 4878 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 4879 | { |
| 4880 | int i; |
| 4881 | |
| 4882 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 4883 | btrfs_dev_stat_reset(dev, i); |
| 4884 | } |
| 4885 | |
| 4886 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 4887 | { |
| 4888 | struct btrfs_key key; |
| 4889 | struct btrfs_key found_key; |
| 4890 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 4891 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 4892 | struct extent_buffer *eb; |
| 4893 | int slot; |
| 4894 | int ret = 0; |
| 4895 | struct btrfs_device *device; |
| 4896 | struct btrfs_path *path = NULL; |
| 4897 | int i; |
| 4898 | |
| 4899 | path = btrfs_alloc_path(); |
| 4900 | if (!path) { |
| 4901 | ret = -ENOMEM; |
| 4902 | goto out; |
| 4903 | } |
| 4904 | |
| 4905 | mutex_lock(&fs_devices->device_list_mutex); |
| 4906 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 4907 | int item_size; |
| 4908 | struct btrfs_dev_stats_item *ptr; |
| 4909 | |
| 4910 | key.objectid = 0; |
| 4911 | key.type = BTRFS_DEV_STATS_KEY; |
| 4912 | key.offset = device->devid; |
| 4913 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 4914 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 4915 | __btrfs_reset_dev_stats(device); |
| 4916 | device->dev_stats_valid = 1; |
| 4917 | btrfs_release_path(path); |
| 4918 | continue; |
| 4919 | } |
| 4920 | slot = path->slots[0]; |
| 4921 | eb = path->nodes[0]; |
| 4922 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4923 | item_size = btrfs_item_size_nr(eb, slot); |
| 4924 | |
| 4925 | ptr = btrfs_item_ptr(eb, slot, |
| 4926 | struct btrfs_dev_stats_item); |
| 4927 | |
| 4928 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 4929 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 4930 | btrfs_dev_stat_set(device, i, |
| 4931 | btrfs_dev_stats_value(eb, ptr, i)); |
| 4932 | else |
| 4933 | btrfs_dev_stat_reset(device, i); |
| 4934 | } |
| 4935 | |
| 4936 | device->dev_stats_valid = 1; |
| 4937 | btrfs_dev_stat_print_on_load(device); |
| 4938 | btrfs_release_path(path); |
| 4939 | } |
| 4940 | mutex_unlock(&fs_devices->device_list_mutex); |
| 4941 | |
| 4942 | out: |
| 4943 | btrfs_free_path(path); |
| 4944 | return ret < 0 ? ret : 0; |
| 4945 | } |
| 4946 | |
| 4947 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 4948 | struct btrfs_root *dev_root, |
| 4949 | struct btrfs_device *device) |
| 4950 | { |
| 4951 | struct btrfs_path *path; |
| 4952 | struct btrfs_key key; |
| 4953 | struct extent_buffer *eb; |
| 4954 | struct btrfs_dev_stats_item *ptr; |
| 4955 | int ret; |
| 4956 | int i; |
| 4957 | |
| 4958 | key.objectid = 0; |
| 4959 | key.type = BTRFS_DEV_STATS_KEY; |
| 4960 | key.offset = device->devid; |
| 4961 | |
| 4962 | path = btrfs_alloc_path(); |
| 4963 | BUG_ON(!path); |
| 4964 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 4965 | if (ret < 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 4966 | printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n", |
| 4967 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 4968 | goto out; |
| 4969 | } |
| 4970 | |
| 4971 | if (ret == 0 && |
| 4972 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 4973 | /* need to delete old one and insert a new one */ |
| 4974 | ret = btrfs_del_item(trans, dev_root, path); |
| 4975 | if (ret != 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 4976 | printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n", |
| 4977 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 4978 | goto out; |
| 4979 | } |
| 4980 | ret = 1; |
| 4981 | } |
| 4982 | |
| 4983 | if (ret == 1) { |
| 4984 | /* need to insert a new item */ |
| 4985 | btrfs_release_path(path); |
| 4986 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 4987 | &key, sizeof(*ptr)); |
| 4988 | if (ret < 0) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 4989 | printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n", |
| 4990 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 4991 | goto out; |
| 4992 | } |
| 4993 | } |
| 4994 | |
| 4995 | eb = path->nodes[0]; |
| 4996 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 4997 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 4998 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 4999 | btrfs_dev_stat_read(device, i)); |
| 5000 | btrfs_mark_buffer_dirty(eb); |
| 5001 | |
| 5002 | out: |
| 5003 | btrfs_free_path(path); |
| 5004 | return ret; |
| 5005 | } |
| 5006 | |
| 5007 | /* |
| 5008 | * called from commit_transaction. Writes all changed device stats to disk. |
| 5009 | */ |
| 5010 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 5011 | struct btrfs_fs_info *fs_info) |
| 5012 | { |
| 5013 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 5014 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 5015 | struct btrfs_device *device; |
| 5016 | int ret = 0; |
| 5017 | |
| 5018 | mutex_lock(&fs_devices->device_list_mutex); |
| 5019 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 5020 | if (!device->dev_stats_valid || !device->dev_stats_dirty) |
| 5021 | continue; |
| 5022 | |
| 5023 | ret = update_dev_stat_item(trans, dev_root, device); |
| 5024 | if (!ret) |
| 5025 | device->dev_stats_dirty = 0; |
| 5026 | } |
| 5027 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5028 | |
| 5029 | return ret; |
| 5030 | } |
| 5031 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5032 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 5033 | { |
| 5034 | btrfs_dev_stat_inc(dev, index); |
| 5035 | btrfs_dev_stat_print_on_error(dev); |
| 5036 | } |
| 5037 | |
| 5038 | void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
| 5039 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5040 | if (!dev->dev_stats_valid) |
| 5041 | return; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5042 | printk_ratelimited_in_rcu(KERN_ERR |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5043 | "btrfs: 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] | 5044 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5045 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 5046 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 5047 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 5048 | btrfs_dev_stat_read(dev, |
| 5049 | BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 5050 | btrfs_dev_stat_read(dev, |
| 5051 | BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 5052 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5053 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5054 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 5055 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 5056 | int i; |
| 5057 | |
| 5058 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5059 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 5060 | break; |
| 5061 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 5062 | return; /* all values == 0, suppress message */ |
| 5063 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5064 | printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", |
| 5065 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5066 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 5067 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 5068 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 5069 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 5070 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 5071 | } |
| 5072 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5073 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 5074 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5075 | { |
| 5076 | struct btrfs_device *dev; |
| 5077 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5078 | int i; |
| 5079 | |
| 5080 | mutex_lock(&fs_devices->device_list_mutex); |
| 5081 | dev = btrfs_find_device(root, stats->devid, NULL, NULL); |
| 5082 | mutex_unlock(&fs_devices->device_list_mutex); |
| 5083 | |
| 5084 | if (!dev) { |
| 5085 | printk(KERN_WARNING |
| 5086 | "btrfs: get dev_stats failed, device not found\n"); |
| 5087 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 5088 | } else if (!dev->dev_stats_valid) { |
| 5089 | printk(KERN_WARNING |
| 5090 | "btrfs: get dev_stats failed, not yet valid\n"); |
| 5091 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 5092 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5093 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 5094 | if (stats->nr_items > i) |
| 5095 | stats->values[i] = |
| 5096 | btrfs_dev_stat_read_and_reset(dev, i); |
| 5097 | else |
| 5098 | btrfs_dev_stat_reset(dev, i); |
| 5099 | } |
| 5100 | } else { |
| 5101 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 5102 | if (stats->nr_items > i) |
| 5103 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 5104 | } |
| 5105 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 5106 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 5107 | return 0; |
| 5108 | } |