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