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