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