Btrfs: fix cache_block_group error handling
[linux-2.6-block.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>
8a4b83cc 20#include <linux/buffer_head.h>
f2d8d74d 21#include <linux/blkdev.h>
788f20eb 22#include <linux/random.h>
593060d7 23#include <asm/div64.h>
0b86a832
CM
24#include "ctree.h"
25#include "extent_map.h"
26#include "disk-io.h"
27#include "transaction.h"
28#include "print-tree.h"
29#include "volumes.h"
8b712842 30#include "async-thread.h"
0b86a832 31
593060d7
CM
32struct map_lookup {
33 u64 type;
34 int io_align;
35 int io_width;
36 int stripe_len;
37 int sector_size;
38 int num_stripes;
321aecc6 39 int sub_stripes;
cea9e445 40 struct btrfs_bio_stripe stripes[];
593060d7
CM
41};
42
43#define map_lookup_size(n) (sizeof(struct map_lookup) + \
cea9e445 44 (sizeof(struct btrfs_bio_stripe) * (n)))
593060d7 45
8a4b83cc
CM
46static DEFINE_MUTEX(uuid_mutex);
47static LIST_HEAD(fs_uuids);
48
a061fc8d
CM
49void btrfs_lock_volumes(void)
50{
51 mutex_lock(&uuid_mutex);
52}
53
54void btrfs_unlock_volumes(void)
55{
56 mutex_unlock(&uuid_mutex);
57}
58
7d9eb12c
CM
59static void lock_chunks(struct btrfs_root *root)
60{
61 mutex_lock(&root->fs_info->alloc_mutex);
62 mutex_lock(&root->fs_info->chunk_mutex);
63}
64
65static void unlock_chunks(struct btrfs_root *root)
66{
67 mutex_unlock(&root->fs_info->alloc_mutex);
68 mutex_unlock(&root->fs_info->chunk_mutex);
69}
70
8a4b83cc
CM
71int btrfs_cleanup_fs_uuids(void)
72{
73 struct btrfs_fs_devices *fs_devices;
74 struct list_head *uuid_cur;
75 struct list_head *devices_cur;
76 struct btrfs_device *dev;
77
78 list_for_each(uuid_cur, &fs_uuids) {
79 fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
80 list);
81 while(!list_empty(&fs_devices->devices)) {
82 devices_cur = fs_devices->devices.next;
83 dev = list_entry(devices_cur, struct btrfs_device,
84 dev_list);
8a4b83cc 85 if (dev->bdev) {
8a4b83cc 86 close_bdev_excl(dev->bdev);
a0af469b 87 fs_devices->open_devices--;
8a4b83cc
CM
88 }
89 list_del(&dev->dev_list);
dfe25020 90 kfree(dev->name);
8a4b83cc
CM
91 kfree(dev);
92 }
93 }
94 return 0;
95}
96
a1b32a59
CM
97static noinline struct btrfs_device *__find_device(struct list_head *head,
98 u64 devid, u8 *uuid)
8a4b83cc
CM
99{
100 struct btrfs_device *dev;
101 struct list_head *cur;
102
103 list_for_each(cur, head) {
104 dev = list_entry(cur, struct btrfs_device, dev_list);
a443755f 105 if (dev->devid == devid &&
8f18cf13 106 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
8a4b83cc 107 return dev;
a443755f 108 }
8a4b83cc
CM
109 }
110 return NULL;
111}
112
a1b32a59 113static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
8a4b83cc
CM
114{
115 struct list_head *cur;
116 struct btrfs_fs_devices *fs_devices;
117
118 list_for_each(cur, &fs_uuids) {
119 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
120 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
121 return fs_devices;
122 }
123 return NULL;
124}
125
8b712842
CM
126/*
127 * we try to collect pending bios for a device so we don't get a large
128 * number of procs sending bios down to the same device. This greatly
129 * improves the schedulers ability to collect and merge the bios.
130 *
131 * But, it also turns into a long list of bios to process and that is sure
132 * to eventually make the worker thread block. The solution here is to
133 * make some progress and then put this work struct back at the end of
134 * the list if the block device is congested. This way, multiple devices
135 * can make progress from a single worker thread.
136 */
a1b32a59 137static int noinline run_scheduled_bios(struct btrfs_device *device)
8b712842
CM
138{
139 struct bio *pending;
140 struct backing_dev_info *bdi;
b64a2851 141 struct btrfs_fs_info *fs_info;
8b712842
CM
142 struct bio *tail;
143 struct bio *cur;
144 int again = 0;
145 unsigned long num_run = 0;
b64a2851 146 unsigned long limit;
8b712842
CM
147
148 bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
b64a2851
CM
149 fs_info = device->dev_root->fs_info;
150 limit = btrfs_async_submit_limit(fs_info);
151 limit = limit * 2 / 3;
152
8b712842
CM
153loop:
154 spin_lock(&device->io_lock);
155
156 /* take all the bios off the list at once and process them
157 * later on (without the lock held). But, remember the
158 * tail and other pointers so the bios can be properly reinserted
159 * into the list if we hit congestion
160 */
161 pending = device->pending_bios;
162 tail = device->pending_bio_tail;
163 WARN_ON(pending && !tail);
164 device->pending_bios = NULL;
165 device->pending_bio_tail = NULL;
166
167 /*
168 * if pending was null this time around, no bios need processing
169 * at all and we can stop. Otherwise it'll loop back up again
170 * and do an additional check so no bios are missed.
171 *
172 * device->running_pending is used to synchronize with the
173 * schedule_bio code.
174 */
175 if (pending) {
176 again = 1;
177 device->running_pending = 1;
178 } else {
179 again = 0;
180 device->running_pending = 0;
181 }
182 spin_unlock(&device->io_lock);
183
184 while(pending) {
185 cur = pending;
186 pending = pending->bi_next;
187 cur->bi_next = NULL;
b64a2851
CM
188 atomic_dec(&fs_info->nr_async_bios);
189
190 if (atomic_read(&fs_info->nr_async_bios) < limit &&
191 waitqueue_active(&fs_info->async_submit_wait))
192 wake_up(&fs_info->async_submit_wait);
492bb6de
CM
193
194 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
195 bio_get(cur);
8b712842 196 submit_bio(cur->bi_rw, cur);
492bb6de 197 bio_put(cur);
8b712842
CM
198 num_run++;
199
200 /*
201 * we made progress, there is more work to do and the bdi
202 * is now congested. Back off and let other work structs
203 * run instead
204 */
492bb6de 205 if (pending && bdi_write_congested(bdi)) {
8b712842
CM
206 struct bio *old_head;
207
208 spin_lock(&device->io_lock);
492bb6de 209
8b712842
CM
210 old_head = device->pending_bios;
211 device->pending_bios = pending;
212 if (device->pending_bio_tail)
213 tail->bi_next = old_head;
214 else
215 device->pending_bio_tail = tail;
216
217 spin_unlock(&device->io_lock);
218 btrfs_requeue_work(&device->work);
219 goto done;
220 }
221 }
222 if (again)
223 goto loop;
224done:
225 return 0;
226}
227
228void pending_bios_fn(struct btrfs_work *work)
229{
230 struct btrfs_device *device;
231
232 device = container_of(work, struct btrfs_device, work);
233 run_scheduled_bios(device);
234}
235
a1b32a59 236static noinline int device_list_add(const char *path,
8a4b83cc
CM
237 struct btrfs_super_block *disk_super,
238 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
239{
240 struct btrfs_device *device;
241 struct btrfs_fs_devices *fs_devices;
242 u64 found_transid = btrfs_super_generation(disk_super);
243
244 fs_devices = find_fsid(disk_super->fsid);
245 if (!fs_devices) {
515dc322 246 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
8a4b83cc
CM
247 if (!fs_devices)
248 return -ENOMEM;
249 INIT_LIST_HEAD(&fs_devices->devices);
b3075717 250 INIT_LIST_HEAD(&fs_devices->alloc_list);
8a4b83cc
CM
251 list_add(&fs_devices->list, &fs_uuids);
252 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
253 fs_devices->latest_devid = devid;
254 fs_devices->latest_trans = found_transid;
8a4b83cc
CM
255 device = NULL;
256 } else {
a443755f
CM
257 device = __find_device(&fs_devices->devices, devid,
258 disk_super->dev_item.uuid);
8a4b83cc
CM
259 }
260 if (!device) {
261 device = kzalloc(sizeof(*device), GFP_NOFS);
262 if (!device) {
263 /* we can safely leave the fs_devices entry around */
264 return -ENOMEM;
265 }
266 device->devid = devid;
8b712842 267 device->work.func = pending_bios_fn;
a443755f
CM
268 memcpy(device->uuid, disk_super->dev_item.uuid,
269 BTRFS_UUID_SIZE);
f2984462 270 device->barriers = 1;
b248a415 271 spin_lock_init(&device->io_lock);
8a4b83cc
CM
272 device->name = kstrdup(path, GFP_NOFS);
273 if (!device->name) {
274 kfree(device);
275 return -ENOMEM;
276 }
277 list_add(&device->dev_list, &fs_devices->devices);
b3075717 278 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
8a4b83cc
CM
279 fs_devices->num_devices++;
280 }
281
282 if (found_transid > fs_devices->latest_trans) {
283 fs_devices->latest_devid = devid;
284 fs_devices->latest_trans = found_transid;
285 }
8a4b83cc
CM
286 *fs_devices_ret = fs_devices;
287 return 0;
288}
289
dfe25020
CM
290int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
291{
292 struct list_head *head = &fs_devices->devices;
293 struct list_head *cur;
294 struct btrfs_device *device;
295
296 mutex_lock(&uuid_mutex);
297again:
298 list_for_each(cur, head) {
299 device = list_entry(cur, struct btrfs_device, dev_list);
300 if (!device->in_fs_metadata) {
a74a4b97 301 struct block_device *bdev;
dfe25020
CM
302 list_del(&device->dev_list);
303 list_del(&device->dev_alloc_list);
304 fs_devices->num_devices--;
a74a4b97
CM
305 if (device->bdev) {
306 bdev = device->bdev;
307 fs_devices->open_devices--;
308 mutex_unlock(&uuid_mutex);
309 close_bdev_excl(bdev);
310 mutex_lock(&uuid_mutex);
311 }
dfe25020
CM
312 kfree(device->name);
313 kfree(device);
314 goto again;
315 }
316 }
317 mutex_unlock(&uuid_mutex);
318 return 0;
319}
a0af469b 320
8a4b83cc
CM
321int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
322{
323 struct list_head *head = &fs_devices->devices;
324 struct list_head *cur;
325 struct btrfs_device *device;
326
327 mutex_lock(&uuid_mutex);
328 list_for_each(cur, head) {
329 device = list_entry(cur, struct btrfs_device, dev_list);
330 if (device->bdev) {
331 close_bdev_excl(device->bdev);
a0af469b 332 fs_devices->open_devices--;
8a4b83cc
CM
333 }
334 device->bdev = NULL;
dfe25020 335 device->in_fs_metadata = 0;
8a4b83cc 336 }
a0af469b 337 fs_devices->mounted = 0;
8a4b83cc
CM
338 mutex_unlock(&uuid_mutex);
339 return 0;
340}
341
342int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
343 int flags, void *holder)
344{
345 struct block_device *bdev;
346 struct list_head *head = &fs_devices->devices;
347 struct list_head *cur;
348 struct btrfs_device *device;
a0af469b
CM
349 struct block_device *latest_bdev = NULL;
350 struct buffer_head *bh;
351 struct btrfs_super_block *disk_super;
352 u64 latest_devid = 0;
353 u64 latest_transid = 0;
354 u64 transid;
355 u64 devid;
356 int ret = 0;
8a4b83cc
CM
357
358 mutex_lock(&uuid_mutex);
a0af469b
CM
359 if (fs_devices->mounted)
360 goto out;
361
8a4b83cc
CM
362 list_for_each(cur, head) {
363 device = list_entry(cur, struct btrfs_device, dev_list);
c1c4d91c
CM
364 if (device->bdev)
365 continue;
366
dfe25020
CM
367 if (!device->name)
368 continue;
369
8a4b83cc 370 bdev = open_bdev_excl(device->name, flags, holder);
e17cade2 371
8a4b83cc
CM
372 if (IS_ERR(bdev)) {
373 printk("open %s failed\n", device->name);
a0af469b 374 goto error;
8a4b83cc 375 }
a061fc8d 376 set_blocksize(bdev, 4096);
a0af469b
CM
377
378 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
379 if (!bh)
380 goto error_close;
381
382 disk_super = (struct btrfs_super_block *)bh->b_data;
383 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
384 sizeof(disk_super->magic)))
385 goto error_brelse;
386
387 devid = le64_to_cpu(disk_super->dev_item.devid);
388 if (devid != device->devid)
389 goto error_brelse;
390
391 transid = btrfs_super_generation(disk_super);
6af5ac3c 392 if (!latest_transid || transid > latest_transid) {
a0af469b
CM
393 latest_devid = devid;
394 latest_transid = transid;
395 latest_bdev = bdev;
396 }
397
8a4b83cc 398 device->bdev = bdev;
dfe25020 399 device->in_fs_metadata = 0;
a0af469b
CM
400 fs_devices->open_devices++;
401 continue;
a061fc8d 402
a0af469b
CM
403error_brelse:
404 brelse(bh);
405error_close:
406 close_bdev_excl(bdev);
407error:
408 continue;
8a4b83cc 409 }
a0af469b
CM
410 if (fs_devices->open_devices == 0) {
411 ret = -EIO;
412 goto out;
413 }
414 fs_devices->mounted = 1;
415 fs_devices->latest_bdev = latest_bdev;
416 fs_devices->latest_devid = latest_devid;
417 fs_devices->latest_trans = latest_transid;
418out:
8a4b83cc 419 mutex_unlock(&uuid_mutex);
8a4b83cc
CM
420 return ret;
421}
422
423int btrfs_scan_one_device(const char *path, int flags, void *holder,
424 struct btrfs_fs_devices **fs_devices_ret)
425{
426 struct btrfs_super_block *disk_super;
427 struct block_device *bdev;
428 struct buffer_head *bh;
429 int ret;
430 u64 devid;
f2984462 431 u64 transid;
8a4b83cc
CM
432
433 mutex_lock(&uuid_mutex);
434
8a4b83cc
CM
435 bdev = open_bdev_excl(path, flags, holder);
436
437 if (IS_ERR(bdev)) {
8a4b83cc
CM
438 ret = PTR_ERR(bdev);
439 goto error;
440 }
441
442 ret = set_blocksize(bdev, 4096);
443 if (ret)
444 goto error_close;
445 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
446 if (!bh) {
447 ret = -EIO;
448 goto error_close;
449 }
450 disk_super = (struct btrfs_super_block *)bh->b_data;
451 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
452 sizeof(disk_super->magic))) {
e58ca020 453 ret = -EINVAL;
8a4b83cc
CM
454 goto error_brelse;
455 }
456 devid = le64_to_cpu(disk_super->dev_item.devid);
f2984462 457 transid = btrfs_super_generation(disk_super);
7ae9c09d
CM
458 if (disk_super->label[0])
459 printk("device label %s ", disk_super->label);
460 else {
461 /* FIXME, make a readl uuid parser */
462 printk("device fsid %llx-%llx ",
463 *(unsigned long long *)disk_super->fsid,
464 *(unsigned long long *)(disk_super->fsid + 8));
465 }
466 printk("devid %Lu transid %Lu %s\n", devid, transid, path);
8a4b83cc
CM
467 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
468
469error_brelse:
470 brelse(bh);
471error_close:
472 close_bdev_excl(bdev);
8a4b83cc
CM
473error:
474 mutex_unlock(&uuid_mutex);
475 return ret;
476}
0b86a832
CM
477
478/*
479 * this uses a pretty simple search, the expectation is that it is
480 * called very infrequently and that a given device has a small number
481 * of extents
482 */
a1b32a59
CM
483static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
484 struct btrfs_device *device,
485 struct btrfs_path *path,
486 u64 num_bytes, u64 *start)
0b86a832
CM
487{
488 struct btrfs_key key;
489 struct btrfs_root *root = device->dev_root;
490 struct btrfs_dev_extent *dev_extent = NULL;
491 u64 hole_size = 0;
492 u64 last_byte = 0;
493 u64 search_start = 0;
494 u64 search_end = device->total_bytes;
495 int ret;
496 int slot = 0;
497 int start_found;
498 struct extent_buffer *l;
499
500 start_found = 0;
501 path->reada = 2;
502
503 /* FIXME use last free of some kind */
504
8a4b83cc
CM
505 /* we don't want to overwrite the superblock on the drive,
506 * so we make sure to start at an offset of at least 1MB
507 */
508 search_start = max((u64)1024 * 1024, search_start);
8f18cf13
CM
509
510 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
511 search_start = max(root->fs_info->alloc_start, search_start);
512
0b86a832
CM
513 key.objectid = device->devid;
514 key.offset = search_start;
515 key.type = BTRFS_DEV_EXTENT_KEY;
516 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
517 if (ret < 0)
518 goto error;
519 ret = btrfs_previous_item(root, path, 0, key.type);
520 if (ret < 0)
521 goto error;
522 l = path->nodes[0];
523 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
524 while (1) {
525 l = path->nodes[0];
526 slot = path->slots[0];
527 if (slot >= btrfs_header_nritems(l)) {
528 ret = btrfs_next_leaf(root, path);
529 if (ret == 0)
530 continue;
531 if (ret < 0)
532 goto error;
533no_more_items:
534 if (!start_found) {
535 if (search_start >= search_end) {
536 ret = -ENOSPC;
537 goto error;
538 }
539 *start = search_start;
540 start_found = 1;
541 goto check_pending;
542 }
543 *start = last_byte > search_start ?
544 last_byte : search_start;
545 if (search_end <= *start) {
546 ret = -ENOSPC;
547 goto error;
548 }
549 goto check_pending;
550 }
551 btrfs_item_key_to_cpu(l, &key, slot);
552
553 if (key.objectid < device->devid)
554 goto next;
555
556 if (key.objectid > device->devid)
557 goto no_more_items;
558
559 if (key.offset >= search_start && key.offset > last_byte &&
560 start_found) {
561 if (last_byte < search_start)
562 last_byte = search_start;
563 hole_size = key.offset - last_byte;
564 if (key.offset > last_byte &&
565 hole_size >= num_bytes) {
566 *start = last_byte;
567 goto check_pending;
568 }
569 }
570 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
571 goto next;
572 }
573
574 start_found = 1;
575 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
576 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
577next:
578 path->slots[0]++;
579 cond_resched();
580 }
581check_pending:
582 /* we have to make sure we didn't find an extent that has already
583 * been allocated by the map tree or the original allocation
584 */
585 btrfs_release_path(root, path);
586 BUG_ON(*start < search_start);
587
6324fbf3 588 if (*start + num_bytes > search_end) {
0b86a832
CM
589 ret = -ENOSPC;
590 goto error;
591 }
592 /* check for pending inserts here */
593 return 0;
594
595error:
596 btrfs_release_path(root, path);
597 return ret;
598}
599
8f18cf13
CM
600int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
601 struct btrfs_device *device,
602 u64 start)
603{
604 int ret;
605 struct btrfs_path *path;
606 struct btrfs_root *root = device->dev_root;
607 struct btrfs_key key;
a061fc8d
CM
608 struct btrfs_key found_key;
609 struct extent_buffer *leaf = NULL;
610 struct btrfs_dev_extent *extent = NULL;
8f18cf13
CM
611
612 path = btrfs_alloc_path();
613 if (!path)
614 return -ENOMEM;
615
616 key.objectid = device->devid;
617 key.offset = start;
618 key.type = BTRFS_DEV_EXTENT_KEY;
619
620 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
a061fc8d
CM
621 if (ret > 0) {
622 ret = btrfs_previous_item(root, path, key.objectid,
623 BTRFS_DEV_EXTENT_KEY);
624 BUG_ON(ret);
625 leaf = path->nodes[0];
626 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
627 extent = btrfs_item_ptr(leaf, path->slots[0],
628 struct btrfs_dev_extent);
629 BUG_ON(found_key.offset > start || found_key.offset +
630 btrfs_dev_extent_length(leaf, extent) < start);
631 ret = 0;
632 } else if (ret == 0) {
633 leaf = path->nodes[0];
634 extent = btrfs_item_ptr(leaf, path->slots[0],
635 struct btrfs_dev_extent);
636 }
8f18cf13
CM
637 BUG_ON(ret);
638
dfe25020
CM
639 if (device->bytes_used > 0)
640 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
8f18cf13
CM
641 ret = btrfs_del_item(trans, root, path);
642 BUG_ON(ret);
643
644 btrfs_free_path(path);
645 return ret;
646}
647
a1b32a59 648int noinline btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
0b86a832 649 struct btrfs_device *device,
e17cade2
CM
650 u64 chunk_tree, u64 chunk_objectid,
651 u64 chunk_offset,
652 u64 num_bytes, u64 *start)
0b86a832
CM
653{
654 int ret;
655 struct btrfs_path *path;
656 struct btrfs_root *root = device->dev_root;
657 struct btrfs_dev_extent *extent;
658 struct extent_buffer *leaf;
659 struct btrfs_key key;
660
dfe25020 661 WARN_ON(!device->in_fs_metadata);
0b86a832
CM
662 path = btrfs_alloc_path();
663 if (!path)
664 return -ENOMEM;
665
666 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
6324fbf3 667 if (ret) {
0b86a832 668 goto err;
6324fbf3 669 }
0b86a832
CM
670
671 key.objectid = device->devid;
672 key.offset = *start;
673 key.type = BTRFS_DEV_EXTENT_KEY;
674 ret = btrfs_insert_empty_item(trans, root, path, &key,
675 sizeof(*extent));
676 BUG_ON(ret);
677
678 leaf = path->nodes[0];
679 extent = btrfs_item_ptr(leaf, path->slots[0],
680 struct btrfs_dev_extent);
e17cade2
CM
681 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
682 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
683 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
684
685 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
686 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
687 BTRFS_UUID_SIZE);
688
0b86a832
CM
689 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
690 btrfs_mark_buffer_dirty(leaf);
691err:
692 btrfs_free_path(path);
693 return ret;
694}
695
a1b32a59
CM
696static noinline int find_next_chunk(struct btrfs_root *root,
697 u64 objectid, u64 *offset)
0b86a832
CM
698{
699 struct btrfs_path *path;
700 int ret;
701 struct btrfs_key key;
e17cade2 702 struct btrfs_chunk *chunk;
0b86a832
CM
703 struct btrfs_key found_key;
704
705 path = btrfs_alloc_path();
706 BUG_ON(!path);
707
e17cade2 708 key.objectid = objectid;
0b86a832
CM
709 key.offset = (u64)-1;
710 key.type = BTRFS_CHUNK_ITEM_KEY;
711
712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
713 if (ret < 0)
714 goto error;
715
716 BUG_ON(ret == 0);
717
718 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
719 if (ret) {
e17cade2 720 *offset = 0;
0b86a832
CM
721 } else {
722 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
723 path->slots[0]);
e17cade2
CM
724 if (found_key.objectid != objectid)
725 *offset = 0;
726 else {
727 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
728 struct btrfs_chunk);
729 *offset = found_key.offset +
730 btrfs_chunk_length(path->nodes[0], chunk);
731 }
0b86a832
CM
732 }
733 ret = 0;
734error:
735 btrfs_free_path(path);
736 return ret;
737}
738
a1b32a59
CM
739static noinline int find_next_devid(struct btrfs_root *root,
740 struct btrfs_path *path, u64 *objectid)
0b86a832
CM
741{
742 int ret;
743 struct btrfs_key key;
744 struct btrfs_key found_key;
745
746 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
747 key.type = BTRFS_DEV_ITEM_KEY;
748 key.offset = (u64)-1;
749
750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
751 if (ret < 0)
752 goto error;
753
754 BUG_ON(ret == 0);
755
756 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
757 BTRFS_DEV_ITEM_KEY);
758 if (ret) {
759 *objectid = 1;
760 } else {
761 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
762 path->slots[0]);
763 *objectid = found_key.offset + 1;
764 }
765 ret = 0;
766error:
767 btrfs_release_path(root, path);
768 return ret;
769}
770
771/*
772 * the device information is stored in the chunk root
773 * the btrfs_device struct should be fully filled in
774 */
775int btrfs_add_device(struct btrfs_trans_handle *trans,
776 struct btrfs_root *root,
777 struct btrfs_device *device)
778{
779 int ret;
780 struct btrfs_path *path;
781 struct btrfs_dev_item *dev_item;
782 struct extent_buffer *leaf;
783 struct btrfs_key key;
784 unsigned long ptr;
006a58a2 785 u64 free_devid = 0;
0b86a832
CM
786
787 root = root->fs_info->chunk_root;
788
789 path = btrfs_alloc_path();
790 if (!path)
791 return -ENOMEM;
792
793 ret = find_next_devid(root, path, &free_devid);
794 if (ret)
795 goto out;
796
797 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
798 key.type = BTRFS_DEV_ITEM_KEY;
799 key.offset = free_devid;
800
801 ret = btrfs_insert_empty_item(trans, root, path, &key,
0d81ba5d 802 sizeof(*dev_item));
0b86a832
CM
803 if (ret)
804 goto out;
805
806 leaf = path->nodes[0];
807 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
808
8a4b83cc 809 device->devid = free_devid;
0b86a832
CM
810 btrfs_set_device_id(leaf, dev_item, device->devid);
811 btrfs_set_device_type(leaf, dev_item, device->type);
812 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
813 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
814 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
0b86a832
CM
815 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
816 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
e17cade2
CM
817 btrfs_set_device_group(leaf, dev_item, 0);
818 btrfs_set_device_seek_speed(leaf, dev_item, 0);
819 btrfs_set_device_bandwidth(leaf, dev_item, 0);
0b86a832 820
0b86a832 821 ptr = (unsigned long)btrfs_device_uuid(dev_item);
e17cade2 822 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
0b86a832
CM
823 btrfs_mark_buffer_dirty(leaf);
824 ret = 0;
825
826out:
827 btrfs_free_path(path);
828 return ret;
829}
8f18cf13 830
a061fc8d
CM
831static int btrfs_rm_dev_item(struct btrfs_root *root,
832 struct btrfs_device *device)
833{
834 int ret;
835 struct btrfs_path *path;
836 struct block_device *bdev = device->bdev;
837 struct btrfs_device *next_dev;
838 struct btrfs_key key;
839 u64 total_bytes;
840 struct btrfs_fs_devices *fs_devices;
841 struct btrfs_trans_handle *trans;
842
843 root = root->fs_info->chunk_root;
844
845 path = btrfs_alloc_path();
846 if (!path)
847 return -ENOMEM;
848
849 trans = btrfs_start_transaction(root, 1);
850 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
851 key.type = BTRFS_DEV_ITEM_KEY;
852 key.offset = device->devid;
7d9eb12c 853 lock_chunks(root);
a061fc8d
CM
854
855 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
856 if (ret < 0)
857 goto out;
858
859 if (ret > 0) {
860 ret = -ENOENT;
861 goto out;
862 }
863
864 ret = btrfs_del_item(trans, root, path);
865 if (ret)
866 goto out;
867
868 /*
869 * at this point, the device is zero sized. We want to
870 * remove it from the devices list and zero out the old super
871 */
872 list_del_init(&device->dev_list);
873 list_del_init(&device->dev_alloc_list);
874 fs_devices = root->fs_info->fs_devices;
875
876 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
877 dev_list);
a061fc8d
CM
878 if (bdev == root->fs_info->sb->s_bdev)
879 root->fs_info->sb->s_bdev = next_dev->bdev;
880 if (bdev == fs_devices->latest_bdev)
881 fs_devices->latest_bdev = next_dev->bdev;
882
a061fc8d
CM
883 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
884 btrfs_set_super_num_devices(&root->fs_info->super_copy,
885 total_bytes - 1);
886out:
887 btrfs_free_path(path);
7d9eb12c 888 unlock_chunks(root);
a061fc8d
CM
889 btrfs_commit_transaction(trans, root);
890 return ret;
891}
892
893int btrfs_rm_device(struct btrfs_root *root, char *device_path)
894{
895 struct btrfs_device *device;
896 struct block_device *bdev;
dfe25020 897 struct buffer_head *bh = NULL;
a061fc8d
CM
898 struct btrfs_super_block *disk_super;
899 u64 all_avail;
900 u64 devid;
901 int ret = 0;
902
a061fc8d 903 mutex_lock(&uuid_mutex);
7d9eb12c 904 mutex_lock(&root->fs_info->volume_mutex);
a061fc8d
CM
905
906 all_avail = root->fs_info->avail_data_alloc_bits |
907 root->fs_info->avail_system_alloc_bits |
908 root->fs_info->avail_metadata_alloc_bits;
909
910 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
dfe25020 911 btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
a061fc8d
CM
912 printk("btrfs: unable to go below four devices on raid10\n");
913 ret = -EINVAL;
914 goto out;
915 }
916
917 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
dfe25020 918 btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
a061fc8d
CM
919 printk("btrfs: unable to go below two devices on raid1\n");
920 ret = -EINVAL;
921 goto out;
922 }
923
dfe25020
CM
924 if (strcmp(device_path, "missing") == 0) {
925 struct list_head *cur;
926 struct list_head *devices;
927 struct btrfs_device *tmp;
a061fc8d 928
dfe25020
CM
929 device = NULL;
930 devices = &root->fs_info->fs_devices->devices;
931 list_for_each(cur, devices) {
932 tmp = list_entry(cur, struct btrfs_device, dev_list);
933 if (tmp->in_fs_metadata && !tmp->bdev) {
934 device = tmp;
935 break;
936 }
937 }
938 bdev = NULL;
939 bh = NULL;
940 disk_super = NULL;
941 if (!device) {
942 printk("btrfs: no missing devices found to remove\n");
943 goto out;
944 }
945
946 } else {
947 bdev = open_bdev_excl(device_path, 0,
948 root->fs_info->bdev_holder);
949 if (IS_ERR(bdev)) {
950 ret = PTR_ERR(bdev);
951 goto out;
952 }
a061fc8d 953
dfe25020
CM
954 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
955 if (!bh) {
956 ret = -EIO;
957 goto error_close;
958 }
959 disk_super = (struct btrfs_super_block *)bh->b_data;
960 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
961 sizeof(disk_super->magic))) {
962 ret = -ENOENT;
963 goto error_brelse;
964 }
965 if (memcmp(disk_super->fsid, root->fs_info->fsid,
966 BTRFS_FSID_SIZE)) {
967 ret = -ENOENT;
968 goto error_brelse;
969 }
970 devid = le64_to_cpu(disk_super->dev_item.devid);
971 device = btrfs_find_device(root, devid, NULL);
972 if (!device) {
973 ret = -ENOENT;
974 goto error_brelse;
975 }
976
977 }
a061fc8d 978 root->fs_info->fs_devices->num_devices--;
0ef3e66b 979 root->fs_info->fs_devices->open_devices--;
a061fc8d
CM
980
981 ret = btrfs_shrink_device(device, 0);
982 if (ret)
983 goto error_brelse;
984
985
986 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
987 if (ret)
988 goto error_brelse;
989
dfe25020
CM
990 if (bh) {
991 /* make sure this device isn't detected as part of
992 * the FS anymore
993 */
994 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
995 set_buffer_dirty(bh);
996 sync_dirty_buffer(bh);
a061fc8d 997
dfe25020
CM
998 brelse(bh);
999 }
a061fc8d 1000
dfe25020
CM
1001 if (device->bdev) {
1002 /* one close for the device struct or super_block */
1003 close_bdev_excl(device->bdev);
1004 }
1005 if (bdev) {
1006 /* one close for us */
1007 close_bdev_excl(bdev);
1008 }
a061fc8d
CM
1009 kfree(device->name);
1010 kfree(device);
1011 ret = 0;
1012 goto out;
1013
1014error_brelse:
1015 brelse(bh);
1016error_close:
dfe25020
CM
1017 if (bdev)
1018 close_bdev_excl(bdev);
a061fc8d 1019out:
7d9eb12c 1020 mutex_unlock(&root->fs_info->volume_mutex);
a061fc8d 1021 mutex_unlock(&uuid_mutex);
a061fc8d
CM
1022 return ret;
1023}
1024
788f20eb
CM
1025int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1026{
1027 struct btrfs_trans_handle *trans;
1028 struct btrfs_device *device;
1029 struct block_device *bdev;
1030 struct list_head *cur;
1031 struct list_head *devices;
1032 u64 total_bytes;
1033 int ret = 0;
1034
1035
1036 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
1037 if (!bdev) {
1038 return -EIO;
1039 }
a2135011 1040
7d9eb12c 1041 mutex_lock(&root->fs_info->volume_mutex);
a2135011 1042
788f20eb 1043 trans = btrfs_start_transaction(root, 1);
7d9eb12c 1044 lock_chunks(root);
788f20eb
CM
1045 devices = &root->fs_info->fs_devices->devices;
1046 list_for_each(cur, devices) {
1047 device = list_entry(cur, struct btrfs_device, dev_list);
1048 if (device->bdev == bdev) {
1049 ret = -EEXIST;
1050 goto out;
1051 }
1052 }
1053
1054 device = kzalloc(sizeof(*device), GFP_NOFS);
1055 if (!device) {
1056 /* we can safely leave the fs_devices entry around */
1057 ret = -ENOMEM;
1058 goto out_close_bdev;
1059 }
1060
1061 device->barriers = 1;
8b712842 1062 device->work.func = pending_bios_fn;
788f20eb
CM
1063 generate_random_uuid(device->uuid);
1064 spin_lock_init(&device->io_lock);
1065 device->name = kstrdup(device_path, GFP_NOFS);
1066 if (!device->name) {
1067 kfree(device);
1068 goto out_close_bdev;
1069 }
1070 device->io_width = root->sectorsize;
1071 device->io_align = root->sectorsize;
1072 device->sector_size = root->sectorsize;
1073 device->total_bytes = i_size_read(bdev->bd_inode);
1074 device->dev_root = root->fs_info->dev_root;
1075 device->bdev = bdev;
dfe25020 1076 device->in_fs_metadata = 1;
788f20eb
CM
1077
1078 ret = btrfs_add_device(trans, root, device);
1079 if (ret)
1080 goto out_close_bdev;
1081
325cd4ba
ZY
1082 set_blocksize(device->bdev, 4096);
1083
788f20eb
CM
1084 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1085 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1086 total_bytes + device->total_bytes);
1087
1088 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1089 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1090 total_bytes + 1);
1091
1092 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1093 list_add(&device->dev_alloc_list,
1094 &root->fs_info->fs_devices->alloc_list);
1095 root->fs_info->fs_devices->num_devices++;
a0af469b 1096 root->fs_info->fs_devices->open_devices++;
788f20eb 1097out:
7d9eb12c 1098 unlock_chunks(root);
788f20eb 1099 btrfs_end_transaction(trans, root);
7d9eb12c 1100 mutex_unlock(&root->fs_info->volume_mutex);
a2135011 1101
788f20eb
CM
1102 return ret;
1103
1104out_close_bdev:
1105 close_bdev_excl(bdev);
1106 goto out;
1107}
1108
a1b32a59
CM
1109int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
1110 struct btrfs_device *device)
0b86a832
CM
1111{
1112 int ret;
1113 struct btrfs_path *path;
1114 struct btrfs_root *root;
1115 struct btrfs_dev_item *dev_item;
1116 struct extent_buffer *leaf;
1117 struct btrfs_key key;
1118
1119 root = device->dev_root->fs_info->chunk_root;
1120
1121 path = btrfs_alloc_path();
1122 if (!path)
1123 return -ENOMEM;
1124
1125 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1126 key.type = BTRFS_DEV_ITEM_KEY;
1127 key.offset = device->devid;
1128
1129 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1130 if (ret < 0)
1131 goto out;
1132
1133 if (ret > 0) {
1134 ret = -ENOENT;
1135 goto out;
1136 }
1137
1138 leaf = path->nodes[0];
1139 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1140
1141 btrfs_set_device_id(leaf, dev_item, device->devid);
1142 btrfs_set_device_type(leaf, dev_item, device->type);
1143 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1144 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1145 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
0b86a832
CM
1146 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1147 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1148 btrfs_mark_buffer_dirty(leaf);
1149
1150out:
1151 btrfs_free_path(path);
1152 return ret;
1153}
1154
7d9eb12c 1155static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
8f18cf13
CM
1156 struct btrfs_device *device, u64 new_size)
1157{
1158 struct btrfs_super_block *super_copy =
1159 &device->dev_root->fs_info->super_copy;
1160 u64 old_total = btrfs_super_total_bytes(super_copy);
1161 u64 diff = new_size - device->total_bytes;
1162
1163 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1164 return btrfs_update_device(trans, device);
1165}
1166
7d9eb12c
CM
1167int btrfs_grow_device(struct btrfs_trans_handle *trans,
1168 struct btrfs_device *device, u64 new_size)
1169{
1170 int ret;
1171 lock_chunks(device->dev_root);
1172 ret = __btrfs_grow_device(trans, device, new_size);
1173 unlock_chunks(device->dev_root);
1174 return ret;
1175}
1176
8f18cf13
CM
1177static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root,
1179 u64 chunk_tree, u64 chunk_objectid,
1180 u64 chunk_offset)
1181{
1182 int ret;
1183 struct btrfs_path *path;
1184 struct btrfs_key key;
1185
1186 root = root->fs_info->chunk_root;
1187 path = btrfs_alloc_path();
1188 if (!path)
1189 return -ENOMEM;
1190
1191 key.objectid = chunk_objectid;
1192 key.offset = chunk_offset;
1193 key.type = BTRFS_CHUNK_ITEM_KEY;
1194
1195 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1196 BUG_ON(ret);
1197
1198 ret = btrfs_del_item(trans, root, path);
1199 BUG_ON(ret);
1200
1201 btrfs_free_path(path);
1202 return 0;
1203}
1204
1205int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1206 chunk_offset)
1207{
1208 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1209 struct btrfs_disk_key *disk_key;
1210 struct btrfs_chunk *chunk;
1211 u8 *ptr;
1212 int ret = 0;
1213 u32 num_stripes;
1214 u32 array_size;
1215 u32 len = 0;
1216 u32 cur;
1217 struct btrfs_key key;
1218
1219 array_size = btrfs_super_sys_array_size(super_copy);
1220
1221 ptr = super_copy->sys_chunk_array;
1222 cur = 0;
1223
1224 while (cur < array_size) {
1225 disk_key = (struct btrfs_disk_key *)ptr;
1226 btrfs_disk_key_to_cpu(&key, disk_key);
1227
1228 len = sizeof(*disk_key);
1229
1230 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1231 chunk = (struct btrfs_chunk *)(ptr + len);
1232 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1233 len += btrfs_chunk_item_size(num_stripes);
1234 } else {
1235 ret = -EIO;
1236 break;
1237 }
1238 if (key.objectid == chunk_objectid &&
1239 key.offset == chunk_offset) {
1240 memmove(ptr, ptr + len, array_size - (cur + len));
1241 array_size -= len;
1242 btrfs_set_super_sys_array_size(super_copy, array_size);
1243 } else {
1244 ptr += len;
1245 cur += len;
1246 }
1247 }
1248 return ret;
1249}
1250
1251
1252int btrfs_relocate_chunk(struct btrfs_root *root,
1253 u64 chunk_tree, u64 chunk_objectid,
1254 u64 chunk_offset)
1255{
1256 struct extent_map_tree *em_tree;
1257 struct btrfs_root *extent_root;
1258 struct btrfs_trans_handle *trans;
1259 struct extent_map *em;
1260 struct map_lookup *map;
1261 int ret;
1262 int i;
1263
323da79c
CM
1264 printk("btrfs relocating chunk %llu\n",
1265 (unsigned long long)chunk_offset);
8f18cf13
CM
1266 root = root->fs_info->chunk_root;
1267 extent_root = root->fs_info->extent_root;
1268 em_tree = &root->fs_info->mapping_tree.map_tree;
1269
1270 /* step one, relocate all the extents inside this chunk */
1271 ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
1272 BUG_ON(ret);
1273
1274 trans = btrfs_start_transaction(root, 1);
1275 BUG_ON(!trans);
1276
7d9eb12c
CM
1277 lock_chunks(root);
1278
8f18cf13
CM
1279 /*
1280 * step two, delete the device extents and the
1281 * chunk tree entries
1282 */
1283 spin_lock(&em_tree->lock);
1284 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1285 spin_unlock(&em_tree->lock);
1286
a061fc8d
CM
1287 BUG_ON(em->start > chunk_offset ||
1288 em->start + em->len < chunk_offset);
8f18cf13
CM
1289 map = (struct map_lookup *)em->bdev;
1290
1291 for (i = 0; i < map->num_stripes; i++) {
1292 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1293 map->stripes[i].physical);
1294 BUG_ON(ret);
a061fc8d 1295
dfe25020
CM
1296 if (map->stripes[i].dev) {
1297 ret = btrfs_update_device(trans, map->stripes[i].dev);
1298 BUG_ON(ret);
1299 }
8f18cf13
CM
1300 }
1301 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1302 chunk_offset);
1303
1304 BUG_ON(ret);
1305
1306 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1307 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1308 BUG_ON(ret);
8f18cf13
CM
1309 }
1310
8f18cf13
CM
1311 spin_lock(&em_tree->lock);
1312 remove_extent_mapping(em_tree, em);
1313 kfree(map);
1314 em->bdev = NULL;
1315
1316 /* once for the tree */
1317 free_extent_map(em);
1318 spin_unlock(&em_tree->lock);
1319
8f18cf13
CM
1320 /* once for us */
1321 free_extent_map(em);
1322
7d9eb12c 1323 unlock_chunks(root);
8f18cf13
CM
1324 btrfs_end_transaction(trans, root);
1325 return 0;
1326}
1327
ec44a35c
CM
1328static u64 div_factor(u64 num, int factor)
1329{
1330 if (factor == 10)
1331 return num;
1332 num *= factor;
1333 do_div(num, 10);
1334 return num;
1335}
1336
1337
1338int btrfs_balance(struct btrfs_root *dev_root)
1339{
1340 int ret;
1341 struct list_head *cur;
1342 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1343 struct btrfs_device *device;
1344 u64 old_size;
1345 u64 size_to_free;
1346 struct btrfs_path *path;
1347 struct btrfs_key key;
1348 struct btrfs_chunk *chunk;
1349 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1350 struct btrfs_trans_handle *trans;
1351 struct btrfs_key found_key;
1352
1353
7d9eb12c 1354 mutex_lock(&dev_root->fs_info->volume_mutex);
ec44a35c
CM
1355 dev_root = dev_root->fs_info->dev_root;
1356
ec44a35c
CM
1357 /* step one make some room on all the devices */
1358 list_for_each(cur, devices) {
1359 device = list_entry(cur, struct btrfs_device, dev_list);
1360 old_size = device->total_bytes;
1361 size_to_free = div_factor(old_size, 1);
1362 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1363 if (device->total_bytes - device->bytes_used > size_to_free)
1364 continue;
1365
1366 ret = btrfs_shrink_device(device, old_size - size_to_free);
1367 BUG_ON(ret);
1368
1369 trans = btrfs_start_transaction(dev_root, 1);
1370 BUG_ON(!trans);
1371
1372 ret = btrfs_grow_device(trans, device, old_size);
1373 BUG_ON(ret);
1374
1375 btrfs_end_transaction(trans, dev_root);
1376 }
1377
1378 /* step two, relocate all the chunks */
1379 path = btrfs_alloc_path();
1380 BUG_ON(!path);
1381
1382 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1383 key.offset = (u64)-1;
1384 key.type = BTRFS_CHUNK_ITEM_KEY;
1385
1386 while(1) {
1387 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1388 if (ret < 0)
1389 goto error;
1390
1391 /*
1392 * this shouldn't happen, it means the last relocate
1393 * failed
1394 */
1395 if (ret == 0)
1396 break;
1397
1398 ret = btrfs_previous_item(chunk_root, path, 0,
1399 BTRFS_CHUNK_ITEM_KEY);
7d9eb12c 1400 if (ret)
ec44a35c 1401 break;
7d9eb12c 1402
ec44a35c
CM
1403 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1404 path->slots[0]);
1405 if (found_key.objectid != key.objectid)
1406 break;
7d9eb12c 1407
ec44a35c
CM
1408 chunk = btrfs_item_ptr(path->nodes[0],
1409 path->slots[0],
1410 struct btrfs_chunk);
1411 key.offset = found_key.offset;
1412 /* chunk zero is special */
1413 if (key.offset == 0)
1414 break;
1415
7d9eb12c 1416 btrfs_release_path(chunk_root, path);
ec44a35c
CM
1417 ret = btrfs_relocate_chunk(chunk_root,
1418 chunk_root->root_key.objectid,
1419 found_key.objectid,
1420 found_key.offset);
1421 BUG_ON(ret);
ec44a35c
CM
1422 }
1423 ret = 0;
1424error:
1425 btrfs_free_path(path);
7d9eb12c 1426 mutex_unlock(&dev_root->fs_info->volume_mutex);
ec44a35c
CM
1427 return ret;
1428}
1429
8f18cf13
CM
1430/*
1431 * shrinking a device means finding all of the device extents past
1432 * the new size, and then following the back refs to the chunks.
1433 * The chunk relocation code actually frees the device extent
1434 */
1435int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1436{
1437 struct btrfs_trans_handle *trans;
1438 struct btrfs_root *root = device->dev_root;
1439 struct btrfs_dev_extent *dev_extent = NULL;
1440 struct btrfs_path *path;
1441 u64 length;
1442 u64 chunk_tree;
1443 u64 chunk_objectid;
1444 u64 chunk_offset;
1445 int ret;
1446 int slot;
1447 struct extent_buffer *l;
1448 struct btrfs_key key;
1449 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1450 u64 old_total = btrfs_super_total_bytes(super_copy);
1451 u64 diff = device->total_bytes - new_size;
1452
1453
1454 path = btrfs_alloc_path();
1455 if (!path)
1456 return -ENOMEM;
1457
1458 trans = btrfs_start_transaction(root, 1);
1459 if (!trans) {
1460 ret = -ENOMEM;
1461 goto done;
1462 }
1463
1464 path->reada = 2;
1465
7d9eb12c
CM
1466 lock_chunks(root);
1467
8f18cf13
CM
1468 device->total_bytes = new_size;
1469 ret = btrfs_update_device(trans, device);
1470 if (ret) {
7d9eb12c 1471 unlock_chunks(root);
8f18cf13
CM
1472 btrfs_end_transaction(trans, root);
1473 goto done;
1474 }
1475 WARN_ON(diff > old_total);
1476 btrfs_set_super_total_bytes(super_copy, old_total - diff);
7d9eb12c 1477 unlock_chunks(root);
8f18cf13
CM
1478 btrfs_end_transaction(trans, root);
1479
1480 key.objectid = device->devid;
1481 key.offset = (u64)-1;
1482 key.type = BTRFS_DEV_EXTENT_KEY;
1483
1484 while (1) {
1485 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1486 if (ret < 0)
1487 goto done;
1488
1489 ret = btrfs_previous_item(root, path, 0, key.type);
1490 if (ret < 0)
1491 goto done;
1492 if (ret) {
1493 ret = 0;
1494 goto done;
1495 }
1496
1497 l = path->nodes[0];
1498 slot = path->slots[0];
1499 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1500
1501 if (key.objectid != device->devid)
1502 goto done;
1503
1504 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1505 length = btrfs_dev_extent_length(l, dev_extent);
1506
1507 if (key.offset + length <= new_size)
1508 goto done;
1509
1510 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1511 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1512 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1513 btrfs_release_path(root, path);
1514
1515 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1516 chunk_offset);
1517 if (ret)
1518 goto done;
1519 }
1520
1521done:
1522 btrfs_free_path(path);
1523 return ret;
1524}
1525
0b86a832
CM
1526int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
1527 struct btrfs_root *root,
1528 struct btrfs_key *key,
1529 struct btrfs_chunk *chunk, int item_size)
1530{
1531 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1532 struct btrfs_disk_key disk_key;
1533 u32 array_size;
1534 u8 *ptr;
1535
1536 array_size = btrfs_super_sys_array_size(super_copy);
1537 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
1538 return -EFBIG;
1539
1540 ptr = super_copy->sys_chunk_array + array_size;
1541 btrfs_cpu_key_to_disk(&disk_key, key);
1542 memcpy(ptr, &disk_key, sizeof(disk_key));
1543 ptr += sizeof(disk_key);
1544 memcpy(ptr, chunk, item_size);
1545 item_size += sizeof(disk_key);
1546 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
1547 return 0;
1548}
1549
a1b32a59
CM
1550static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
1551 int num_stripes, int sub_stripes)
9b3f68b9
CM
1552{
1553 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
1554 return calc_size;
1555 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1556 return calc_size * (num_stripes / sub_stripes);
1557 else
1558 return calc_size * num_stripes;
1559}
1560
1561
0b86a832
CM
1562int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
1563 struct btrfs_root *extent_root, u64 *start,
6324fbf3 1564 u64 *num_bytes, u64 type)
0b86a832
CM
1565{
1566 u64 dev_offset;
593060d7 1567 struct btrfs_fs_info *info = extent_root->fs_info;
0b86a832 1568 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
8f18cf13 1569 struct btrfs_path *path;
0b86a832
CM
1570 struct btrfs_stripe *stripes;
1571 struct btrfs_device *device = NULL;
1572 struct btrfs_chunk *chunk;
6324fbf3 1573 struct list_head private_devs;
b3075717 1574 struct list_head *dev_list;
6324fbf3 1575 struct list_head *cur;
0b86a832
CM
1576 struct extent_map_tree *em_tree;
1577 struct map_lookup *map;
1578 struct extent_map *em;
a40a90a0 1579 int min_stripe_size = 1 * 1024 * 1024;
0b86a832
CM
1580 u64 physical;
1581 u64 calc_size = 1024 * 1024 * 1024;
9b3f68b9
CM
1582 u64 max_chunk_size = calc_size;
1583 u64 min_free;
6324fbf3
CM
1584 u64 avail;
1585 u64 max_avail = 0;
9b3f68b9 1586 u64 percent_max;
6324fbf3 1587 int num_stripes = 1;
a40a90a0 1588 int min_stripes = 1;
321aecc6 1589 int sub_stripes = 0;
6324fbf3 1590 int looped = 0;
0b86a832 1591 int ret;
6324fbf3 1592 int index;
593060d7 1593 int stripe_len = 64 * 1024;
0b86a832
CM
1594 struct btrfs_key key;
1595
ec44a35c
CM
1596 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
1597 (type & BTRFS_BLOCK_GROUP_DUP)) {
1598 WARN_ON(1);
1599 type &= ~BTRFS_BLOCK_GROUP_DUP;
1600 }
b3075717 1601 dev_list = &extent_root->fs_info->fs_devices->alloc_list;
6324fbf3
CM
1602 if (list_empty(dev_list))
1603 return -ENOSPC;
593060d7 1604
a40a90a0 1605 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
0ef3e66b 1606 num_stripes = extent_root->fs_info->fs_devices->open_devices;
a40a90a0
CM
1607 min_stripes = 2;
1608 }
1609 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
611f0e00 1610 num_stripes = 2;
a40a90a0
CM
1611 min_stripes = 2;
1612 }
8790d502
CM
1613 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
1614 num_stripes = min_t(u64, 2,
0ef3e66b 1615 extent_root->fs_info->fs_devices->open_devices);
9b3f68b9
CM
1616 if (num_stripes < 2)
1617 return -ENOSPC;
a40a90a0 1618 min_stripes = 2;
8790d502 1619 }
321aecc6 1620 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
0ef3e66b 1621 num_stripes = extent_root->fs_info->fs_devices->open_devices;
321aecc6
CM
1622 if (num_stripes < 4)
1623 return -ENOSPC;
1624 num_stripes &= ~(u32)1;
1625 sub_stripes = 2;
a40a90a0 1626 min_stripes = 4;
321aecc6 1627 }
9b3f68b9
CM
1628
1629 if (type & BTRFS_BLOCK_GROUP_DATA) {
1630 max_chunk_size = 10 * calc_size;
a40a90a0 1631 min_stripe_size = 64 * 1024 * 1024;
9b3f68b9
CM
1632 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
1633 max_chunk_size = 4 * calc_size;
a40a90a0
CM
1634 min_stripe_size = 32 * 1024 * 1024;
1635 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1636 calc_size = 8 * 1024 * 1024;
1637 max_chunk_size = calc_size * 2;
1638 min_stripe_size = 1 * 1024 * 1024;
9b3f68b9
CM
1639 }
1640
8f18cf13
CM
1641 path = btrfs_alloc_path();
1642 if (!path)
1643 return -ENOMEM;
1644
9b3f68b9
CM
1645 /* we don't want a chunk larger than 10% of the FS */
1646 percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
1647 max_chunk_size = min(percent_max, max_chunk_size);
1648
a40a90a0 1649again:
9b3f68b9
CM
1650 if (calc_size * num_stripes > max_chunk_size) {
1651 calc_size = max_chunk_size;
1652 do_div(calc_size, num_stripes);
1653 do_div(calc_size, stripe_len);
1654 calc_size *= stripe_len;
1655 }
1656 /* we don't want tiny stripes */
a40a90a0 1657 calc_size = max_t(u64, min_stripe_size, calc_size);
9b3f68b9 1658
9b3f68b9
CM
1659 do_div(calc_size, stripe_len);
1660 calc_size *= stripe_len;
1661
6324fbf3
CM
1662 INIT_LIST_HEAD(&private_devs);
1663 cur = dev_list->next;
1664 index = 0;
611f0e00
CM
1665
1666 if (type & BTRFS_BLOCK_GROUP_DUP)
1667 min_free = calc_size * 2;
9b3f68b9
CM
1668 else
1669 min_free = calc_size;
611f0e00 1670
ad5bd91e
CM
1671 /* we add 1MB because we never use the first 1MB of the device */
1672 min_free += 1024 * 1024;
1673
6324fbf3
CM
1674 /* build a private list of devices we will allocate from */
1675 while(index < num_stripes) {
b3075717 1676 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
611f0e00 1677
dfe25020
CM
1678 if (device->total_bytes > device->bytes_used)
1679 avail = device->total_bytes - device->bytes_used;
1680 else
1681 avail = 0;
6324fbf3 1682 cur = cur->next;
8f18cf13 1683
dfe25020 1684 if (device->in_fs_metadata && avail >= min_free) {
8f18cf13
CM
1685 u64 ignored_start = 0;
1686 ret = find_free_dev_extent(trans, device, path,
1687 min_free,
1688 &ignored_start);
1689 if (ret == 0) {
1690 list_move_tail(&device->dev_alloc_list,
1691 &private_devs);
611f0e00 1692 index++;
8f18cf13
CM
1693 if (type & BTRFS_BLOCK_GROUP_DUP)
1694 index++;
1695 }
dfe25020 1696 } else if (device->in_fs_metadata && avail > max_avail)
a40a90a0 1697 max_avail = avail;
6324fbf3
CM
1698 if (cur == dev_list)
1699 break;
1700 }
1701 if (index < num_stripes) {
1702 list_splice(&private_devs, dev_list);
a40a90a0
CM
1703 if (index >= min_stripes) {
1704 num_stripes = index;
1705 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
1706 num_stripes /= sub_stripes;
1707 num_stripes *= sub_stripes;
1708 }
1709 looped = 1;
1710 goto again;
1711 }
6324fbf3
CM
1712 if (!looped && max_avail > 0) {
1713 looped = 1;
1714 calc_size = max_avail;
1715 goto again;
1716 }
8f18cf13 1717 btrfs_free_path(path);
6324fbf3
CM
1718 return -ENOSPC;
1719 }
e17cade2
CM
1720 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1721 key.type = BTRFS_CHUNK_ITEM_KEY;
1722 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1723 &key.offset);
8f18cf13
CM
1724 if (ret) {
1725 btrfs_free_path(path);
0b86a832 1726 return ret;
8f18cf13 1727 }
0b86a832 1728
0b86a832 1729 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
8f18cf13
CM
1730 if (!chunk) {
1731 btrfs_free_path(path);
0b86a832 1732 return -ENOMEM;
8f18cf13 1733 }
0b86a832 1734
593060d7
CM
1735 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
1736 if (!map) {
1737 kfree(chunk);
8f18cf13 1738 btrfs_free_path(path);
593060d7
CM
1739 return -ENOMEM;
1740 }
8f18cf13
CM
1741 btrfs_free_path(path);
1742 path = NULL;
593060d7 1743
0b86a832 1744 stripes = &chunk->stripe;
9b3f68b9
CM
1745 *num_bytes = chunk_bytes_by_type(type, calc_size,
1746 num_stripes, sub_stripes);
0b86a832 1747
6324fbf3 1748 index = 0;
0b86a832 1749 while(index < num_stripes) {
e17cade2 1750 struct btrfs_stripe *stripe;
6324fbf3
CM
1751 BUG_ON(list_empty(&private_devs));
1752 cur = private_devs.next;
b3075717 1753 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
611f0e00
CM
1754
1755 /* loop over this device again if we're doing a dup group */
1756 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
1757 (index == num_stripes - 1))
b3075717 1758 list_move_tail(&device->dev_alloc_list, dev_list);
0b86a832
CM
1759
1760 ret = btrfs_alloc_dev_extent(trans, device,
e17cade2
CM
1761 info->chunk_root->root_key.objectid,
1762 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1763 calc_size, &dev_offset);
0b86a832 1764 BUG_ON(ret);
0b86a832
CM
1765 device->bytes_used += calc_size;
1766 ret = btrfs_update_device(trans, device);
1767 BUG_ON(ret);
1768
593060d7
CM
1769 map->stripes[index].dev = device;
1770 map->stripes[index].physical = dev_offset;
e17cade2
CM
1771 stripe = stripes + index;
1772 btrfs_set_stack_stripe_devid(stripe, device->devid);
1773 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1774 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
0b86a832
CM
1775 physical = dev_offset;
1776 index++;
1777 }
6324fbf3 1778 BUG_ON(!list_empty(&private_devs));
0b86a832 1779
e17cade2
CM
1780 /* key was set above */
1781 btrfs_set_stack_chunk_length(chunk, *num_bytes);
0b86a832 1782 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
593060d7 1783 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
0b86a832
CM
1784 btrfs_set_stack_chunk_type(chunk, type);
1785 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
593060d7
CM
1786 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1787 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
0b86a832 1788 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
321aecc6 1789 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
593060d7
CM
1790 map->sector_size = extent_root->sectorsize;
1791 map->stripe_len = stripe_len;
1792 map->io_align = stripe_len;
1793 map->io_width = stripe_len;
1794 map->type = type;
1795 map->num_stripes = num_stripes;
321aecc6 1796 map->sub_stripes = sub_stripes;
0b86a832
CM
1797
1798 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1799 btrfs_chunk_item_size(num_stripes));
1800 BUG_ON(ret);
e17cade2 1801 *start = key.offset;;
0b86a832
CM
1802
1803 em = alloc_extent_map(GFP_NOFS);
1804 if (!em)
1805 return -ENOMEM;
0b86a832 1806 em->bdev = (struct block_device *)map;
e17cade2
CM
1807 em->start = key.offset;
1808 em->len = *num_bytes;
0b86a832
CM
1809 em->block_start = 0;
1810
8f18cf13
CM
1811 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1812 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1813 chunk, btrfs_chunk_item_size(num_stripes));
1814 BUG_ON(ret);
1815 }
0b86a832
CM
1816 kfree(chunk);
1817
1818 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
1819 spin_lock(&em_tree->lock);
1820 ret = add_extent_mapping(em_tree, em);
0b86a832 1821 spin_unlock(&em_tree->lock);
b248a415 1822 BUG_ON(ret);
0b86a832
CM
1823 free_extent_map(em);
1824 return ret;
1825}
1826
1827void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
1828{
1829 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
1830}
1831
1832void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
1833{
1834 struct extent_map *em;
1835
1836 while(1) {
1837 spin_lock(&tree->map_tree.lock);
1838 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
1839 if (em)
1840 remove_extent_mapping(&tree->map_tree, em);
1841 spin_unlock(&tree->map_tree.lock);
1842 if (!em)
1843 break;
1844 kfree(em->bdev);
1845 /* once for us */
1846 free_extent_map(em);
1847 /* once for the tree */
1848 free_extent_map(em);
1849 }
1850}
1851
f188591e
CM
1852int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1853{
1854 struct extent_map *em;
1855 struct map_lookup *map;
1856 struct extent_map_tree *em_tree = &map_tree->map_tree;
1857 int ret;
1858
1859 spin_lock(&em_tree->lock);
1860 em = lookup_extent_mapping(em_tree, logical, len);
b248a415 1861 spin_unlock(&em_tree->lock);
f188591e
CM
1862 BUG_ON(!em);
1863
1864 BUG_ON(em->start > logical || em->start + em->len < logical);
1865 map = (struct map_lookup *)em->bdev;
1866 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1867 ret = map->num_stripes;
321aecc6
CM
1868 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1869 ret = map->sub_stripes;
f188591e
CM
1870 else
1871 ret = 1;
1872 free_extent_map(em);
f188591e
CM
1873 return ret;
1874}
1875
dfe25020
CM
1876static int find_live_mirror(struct map_lookup *map, int first, int num,
1877 int optimal)
1878{
1879 int i;
1880 if (map->stripes[optimal].dev->bdev)
1881 return optimal;
1882 for (i = first; i < first + num; i++) {
1883 if (map->stripes[i].dev->bdev)
1884 return i;
1885 }
1886 /* we couldn't find one that doesn't fail. Just return something
1887 * and the io error handling code will clean up eventually
1888 */
1889 return optimal;
1890}
1891
f2d8d74d
CM
1892static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1893 u64 logical, u64 *length,
1894 struct btrfs_multi_bio **multi_ret,
1895 int mirror_num, struct page *unplug_page)
0b86a832
CM
1896{
1897 struct extent_map *em;
1898 struct map_lookup *map;
1899 struct extent_map_tree *em_tree = &map_tree->map_tree;
1900 u64 offset;
593060d7
CM
1901 u64 stripe_offset;
1902 u64 stripe_nr;
cea9e445 1903 int stripes_allocated = 8;
321aecc6 1904 int stripes_required = 1;
593060d7 1905 int stripe_index;
cea9e445 1906 int i;
f2d8d74d 1907 int num_stripes;
a236aed1 1908 int max_errors = 0;
cea9e445 1909 struct btrfs_multi_bio *multi = NULL;
0b86a832 1910
cea9e445
CM
1911 if (multi_ret && !(rw & (1 << BIO_RW))) {
1912 stripes_allocated = 1;
1913 }
1914again:
1915 if (multi_ret) {
1916 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1917 GFP_NOFS);
1918 if (!multi)
1919 return -ENOMEM;
a236aed1
CM
1920
1921 atomic_set(&multi->error, 0);
cea9e445 1922 }
0b86a832
CM
1923
1924 spin_lock(&em_tree->lock);
1925 em = lookup_extent_mapping(em_tree, logical, *length);
b248a415 1926 spin_unlock(&em_tree->lock);
f2d8d74d
CM
1927
1928 if (!em && unplug_page)
1929 return 0;
1930
3b951516 1931 if (!em) {
a061fc8d 1932 printk("unable to find logical %Lu len %Lu\n", logical, *length);
f2d8d74d 1933 BUG();
3b951516 1934 }
0b86a832
CM
1935
1936 BUG_ON(em->start > logical || em->start + em->len < logical);
1937 map = (struct map_lookup *)em->bdev;
1938 offset = logical - em->start;
593060d7 1939
f188591e
CM
1940 if (mirror_num > map->num_stripes)
1941 mirror_num = 0;
1942
cea9e445 1943 /* if our multi bio struct is too small, back off and try again */
321aecc6
CM
1944 if (rw & (1 << BIO_RW)) {
1945 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1946 BTRFS_BLOCK_GROUP_DUP)) {
1947 stripes_required = map->num_stripes;
a236aed1 1948 max_errors = 1;
321aecc6
CM
1949 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1950 stripes_required = map->sub_stripes;
a236aed1 1951 max_errors = 1;
321aecc6
CM
1952 }
1953 }
1954 if (multi_ret && rw == WRITE &&
1955 stripes_allocated < stripes_required) {
cea9e445 1956 stripes_allocated = map->num_stripes;
cea9e445
CM
1957 free_extent_map(em);
1958 kfree(multi);
1959 goto again;
1960 }
593060d7
CM
1961 stripe_nr = offset;
1962 /*
1963 * stripe_nr counts the total number of stripes we have to stride
1964 * to get to this block
1965 */
1966 do_div(stripe_nr, map->stripe_len);
1967
1968 stripe_offset = stripe_nr * map->stripe_len;
1969 BUG_ON(offset < stripe_offset);
1970
1971 /* stripe_offset is the offset of this block in its stripe*/
1972 stripe_offset = offset - stripe_offset;
1973
cea9e445 1974 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
321aecc6 1975 BTRFS_BLOCK_GROUP_RAID10 |
cea9e445
CM
1976 BTRFS_BLOCK_GROUP_DUP)) {
1977 /* we limit the length of each bio to what fits in a stripe */
1978 *length = min_t(u64, em->len - offset,
1979 map->stripe_len - stripe_offset);
1980 } else {
1981 *length = em->len - offset;
1982 }
f2d8d74d
CM
1983
1984 if (!multi_ret && !unplug_page)
cea9e445
CM
1985 goto out;
1986
f2d8d74d 1987 num_stripes = 1;
cea9e445 1988 stripe_index = 0;
8790d502 1989 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
f2d8d74d
CM
1990 if (unplug_page || (rw & (1 << BIO_RW)))
1991 num_stripes = map->num_stripes;
2fff734f 1992 else if (mirror_num)
f188591e 1993 stripe_index = mirror_num - 1;
dfe25020
CM
1994 else {
1995 stripe_index = find_live_mirror(map, 0,
1996 map->num_stripes,
1997 current->pid % map->num_stripes);
1998 }
2fff734f 1999
611f0e00 2000 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
cea9e445 2001 if (rw & (1 << BIO_RW))
f2d8d74d 2002 num_stripes = map->num_stripes;
f188591e
CM
2003 else if (mirror_num)
2004 stripe_index = mirror_num - 1;
2fff734f 2005
321aecc6
CM
2006 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2007 int factor = map->num_stripes / map->sub_stripes;
321aecc6
CM
2008
2009 stripe_index = do_div(stripe_nr, factor);
2010 stripe_index *= map->sub_stripes;
2011
f2d8d74d
CM
2012 if (unplug_page || (rw & (1 << BIO_RW)))
2013 num_stripes = map->sub_stripes;
321aecc6
CM
2014 else if (mirror_num)
2015 stripe_index += mirror_num - 1;
dfe25020
CM
2016 else {
2017 stripe_index = find_live_mirror(map, stripe_index,
2018 map->sub_stripes, stripe_index +
2019 current->pid % map->sub_stripes);
2020 }
8790d502
CM
2021 } else {
2022 /*
2023 * after this do_div call, stripe_nr is the number of stripes
2024 * on this device we have to walk to find the data, and
2025 * stripe_index is the number of our device in the stripe array
2026 */
2027 stripe_index = do_div(stripe_nr, map->num_stripes);
2028 }
593060d7 2029 BUG_ON(stripe_index >= map->num_stripes);
cea9e445 2030
f2d8d74d
CM
2031 for (i = 0; i < num_stripes; i++) {
2032 if (unplug_page) {
2033 struct btrfs_device *device;
2034 struct backing_dev_info *bdi;
2035
2036 device = map->stripes[stripe_index].dev;
dfe25020
CM
2037 if (device->bdev) {
2038 bdi = blk_get_backing_dev_info(device->bdev);
2039 if (bdi->unplug_io_fn) {
2040 bdi->unplug_io_fn(bdi, unplug_page);
2041 }
f2d8d74d
CM
2042 }
2043 } else {
2044 multi->stripes[i].physical =
2045 map->stripes[stripe_index].physical +
2046 stripe_offset + stripe_nr * map->stripe_len;
2047 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2048 }
cea9e445 2049 stripe_index++;
593060d7 2050 }
f2d8d74d
CM
2051 if (multi_ret) {
2052 *multi_ret = multi;
2053 multi->num_stripes = num_stripes;
a236aed1 2054 multi->max_errors = max_errors;
f2d8d74d 2055 }
cea9e445 2056out:
0b86a832 2057 free_extent_map(em);
0b86a832
CM
2058 return 0;
2059}
2060
f2d8d74d
CM
2061int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2062 u64 logical, u64 *length,
2063 struct btrfs_multi_bio **multi_ret, int mirror_num)
2064{
2065 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2066 mirror_num, NULL);
2067}
2068
2069int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2070 u64 logical, struct page *page)
2071{
2072 u64 length = PAGE_CACHE_SIZE;
2073 return __btrfs_map_block(map_tree, READ, logical, &length,
2074 NULL, 0, page);
2075}
2076
2077
8790d502
CM
2078#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2079static void end_bio_multi_stripe(struct bio *bio, int err)
2080#else
2081static int end_bio_multi_stripe(struct bio *bio,
2082 unsigned int bytes_done, int err)
2083#endif
2084{
cea9e445 2085 struct btrfs_multi_bio *multi = bio->bi_private;
7d2b4daa 2086 int is_orig_bio = 0;
8790d502
CM
2087
2088#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2089 if (bio->bi_size)
2090 return 1;
2091#endif
2092 if (err)
a236aed1 2093 atomic_inc(&multi->error);
8790d502 2094
7d2b4daa
CM
2095 if (bio == multi->orig_bio)
2096 is_orig_bio = 1;
2097
cea9e445 2098 if (atomic_dec_and_test(&multi->stripes_pending)) {
7d2b4daa
CM
2099 if (!is_orig_bio) {
2100 bio_put(bio);
2101 bio = multi->orig_bio;
2102 }
8790d502
CM
2103 bio->bi_private = multi->private;
2104 bio->bi_end_io = multi->end_io;
a236aed1
CM
2105 /* only send an error to the higher layers if it is
2106 * beyond the tolerance of the multi-bio
2107 */
1259ab75 2108 if (atomic_read(&multi->error) > multi->max_errors) {
a236aed1 2109 err = -EIO;
1259ab75
CM
2110 } else if (err) {
2111 /*
2112 * this bio is actually up to date, we didn't
2113 * go over the max number of errors
2114 */
2115 set_bit(BIO_UPTODATE, &bio->bi_flags);
a236aed1 2116 err = 0;
1259ab75 2117 }
8790d502
CM
2118 kfree(multi);
2119
73f61b2a
M
2120#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2121 bio_endio(bio, bio->bi_size, err);
2122#else
8790d502 2123 bio_endio(bio, err);
73f61b2a 2124#endif
7d2b4daa 2125 } else if (!is_orig_bio) {
8790d502
CM
2126 bio_put(bio);
2127 }
2128#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2129 return 0;
2130#endif
2131}
2132
8b712842
CM
2133struct async_sched {
2134 struct bio *bio;
2135 int rw;
2136 struct btrfs_fs_info *info;
2137 struct btrfs_work work;
2138};
2139
2140/*
2141 * see run_scheduled_bios for a description of why bios are collected for
2142 * async submit.
2143 *
2144 * This will add one bio to the pending list for a device and make sure
2145 * the work struct is scheduled.
2146 */
a1b32a59
CM
2147static int noinline schedule_bio(struct btrfs_root *root,
2148 struct btrfs_device *device,
2149 int rw, struct bio *bio)
8b712842
CM
2150{
2151 int should_queue = 1;
2152
2153 /* don't bother with additional async steps for reads, right now */
2154 if (!(rw & (1 << BIO_RW))) {
492bb6de 2155 bio_get(bio);
8b712842 2156 submit_bio(rw, bio);
492bb6de 2157 bio_put(bio);
8b712842
CM
2158 return 0;
2159 }
2160
2161 /*
0986fe9e 2162 * nr_async_bios allows us to reliably return congestion to the
8b712842
CM
2163 * higher layers. Otherwise, the async bio makes it appear we have
2164 * made progress against dirty pages when we've really just put it
2165 * on a queue for later
2166 */
0986fe9e 2167 atomic_inc(&root->fs_info->nr_async_bios);
492bb6de 2168 WARN_ON(bio->bi_next);
8b712842
CM
2169 bio->bi_next = NULL;
2170 bio->bi_rw |= rw;
2171
2172 spin_lock(&device->io_lock);
2173
2174 if (device->pending_bio_tail)
2175 device->pending_bio_tail->bi_next = bio;
2176
2177 device->pending_bio_tail = bio;
2178 if (!device->pending_bios)
2179 device->pending_bios = bio;
2180 if (device->running_pending)
2181 should_queue = 0;
2182
2183 spin_unlock(&device->io_lock);
2184
2185 if (should_queue)
1cc127b5
CM
2186 btrfs_queue_worker(&root->fs_info->submit_workers,
2187 &device->work);
8b712842
CM
2188 return 0;
2189}
2190
f188591e 2191int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
8b712842 2192 int mirror_num, int async_submit)
0b86a832
CM
2193{
2194 struct btrfs_mapping_tree *map_tree;
2195 struct btrfs_device *dev;
8790d502 2196 struct bio *first_bio = bio;
0b86a832 2197 u64 logical = bio->bi_sector << 9;
0b86a832
CM
2198 u64 length = 0;
2199 u64 map_length;
cea9e445 2200 struct btrfs_multi_bio *multi = NULL;
0b86a832 2201 int ret;
8790d502
CM
2202 int dev_nr = 0;
2203 int total_devs = 1;
0b86a832 2204
f2d8d74d 2205 length = bio->bi_size;
0b86a832
CM
2206 map_tree = &root->fs_info->mapping_tree;
2207 map_length = length;
cea9e445 2208
f188591e
CM
2209 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2210 mirror_num);
cea9e445
CM
2211 BUG_ON(ret);
2212
2213 total_devs = multi->num_stripes;
2214 if (map_length < length) {
2215 printk("mapping failed logical %Lu bio len %Lu "
2216 "len %Lu\n", logical, length, map_length);
2217 BUG();
2218 }
2219 multi->end_io = first_bio->bi_end_io;
2220 multi->private = first_bio->bi_private;
7d2b4daa 2221 multi->orig_bio = first_bio;
cea9e445
CM
2222 atomic_set(&multi->stripes_pending, multi->num_stripes);
2223
8790d502 2224 while(dev_nr < total_devs) {
8790d502 2225 if (total_devs > 1) {
8790d502
CM
2226 if (dev_nr < total_devs - 1) {
2227 bio = bio_clone(first_bio, GFP_NOFS);
2228 BUG_ON(!bio);
2229 } else {
2230 bio = first_bio;
2231 }
2232 bio->bi_private = multi;
2233 bio->bi_end_io = end_bio_multi_stripe;
2234 }
cea9e445
CM
2235 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2236 dev = multi->stripes[dev_nr].dev;
dfe25020
CM
2237 if (dev && dev->bdev) {
2238 bio->bi_bdev = dev->bdev;
8b712842
CM
2239 if (async_submit)
2240 schedule_bio(root, dev, rw, bio);
2241 else
2242 submit_bio(rw, bio);
dfe25020
CM
2243 } else {
2244 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2245 bio->bi_sector = logical >> 9;
2246#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
2247 bio_endio(bio, bio->bi_size, -EIO);
2248#else
2249 bio_endio(bio, -EIO);
2250#endif
2251 }
8790d502
CM
2252 dev_nr++;
2253 }
cea9e445
CM
2254 if (total_devs == 1)
2255 kfree(multi);
0b86a832
CM
2256 return 0;
2257}
2258
a443755f
CM
2259struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2260 u8 *uuid)
0b86a832 2261{
8a4b83cc 2262 struct list_head *head = &root->fs_info->fs_devices->devices;
0b86a832 2263
a443755f 2264 return __find_device(head, devid, uuid);
0b86a832
CM
2265}
2266
dfe25020
CM
2267static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2268 u64 devid, u8 *dev_uuid)
2269{
2270 struct btrfs_device *device;
2271 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2272
2273 device = kzalloc(sizeof(*device), GFP_NOFS);
2274 list_add(&device->dev_list,
2275 &fs_devices->devices);
2276 list_add(&device->dev_alloc_list,
2277 &fs_devices->alloc_list);
2278 device->barriers = 1;
2279 device->dev_root = root->fs_info->dev_root;
2280 device->devid = devid;
8b712842 2281 device->work.func = pending_bios_fn;
dfe25020
CM
2282 fs_devices->num_devices++;
2283 spin_lock_init(&device->io_lock);
2284 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2285 return device;
2286}
2287
2288
0b86a832
CM
2289static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2290 struct extent_buffer *leaf,
2291 struct btrfs_chunk *chunk)
2292{
2293 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2294 struct map_lookup *map;
2295 struct extent_map *em;
2296 u64 logical;
2297 u64 length;
2298 u64 devid;
a443755f 2299 u8 uuid[BTRFS_UUID_SIZE];
593060d7 2300 int num_stripes;
0b86a832 2301 int ret;
593060d7 2302 int i;
0b86a832 2303
e17cade2
CM
2304 logical = key->offset;
2305 length = btrfs_chunk_length(leaf, chunk);
a061fc8d 2306
0b86a832
CM
2307 spin_lock(&map_tree->map_tree.lock);
2308 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
b248a415 2309 spin_unlock(&map_tree->map_tree.lock);
0b86a832
CM
2310
2311 /* already mapped? */
2312 if (em && em->start <= logical && em->start + em->len > logical) {
2313 free_extent_map(em);
0b86a832
CM
2314 return 0;
2315 } else if (em) {
2316 free_extent_map(em);
2317 }
0b86a832
CM
2318
2319 map = kzalloc(sizeof(*map), GFP_NOFS);
2320 if (!map)
2321 return -ENOMEM;
2322
2323 em = alloc_extent_map(GFP_NOFS);
2324 if (!em)
2325 return -ENOMEM;
593060d7
CM
2326 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2327 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
0b86a832
CM
2328 if (!map) {
2329 free_extent_map(em);
2330 return -ENOMEM;
2331 }
2332
2333 em->bdev = (struct block_device *)map;
2334 em->start = logical;
2335 em->len = length;
2336 em->block_start = 0;
2337
593060d7
CM
2338 map->num_stripes = num_stripes;
2339 map->io_width = btrfs_chunk_io_width(leaf, chunk);
2340 map->io_align = btrfs_chunk_io_align(leaf, chunk);
2341 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
2342 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
2343 map->type = btrfs_chunk_type(leaf, chunk);
321aecc6 2344 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
593060d7
CM
2345 for (i = 0; i < num_stripes; i++) {
2346 map->stripes[i].physical =
2347 btrfs_stripe_offset_nr(leaf, chunk, i);
2348 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
a443755f
CM
2349 read_extent_buffer(leaf, uuid, (unsigned long)
2350 btrfs_stripe_dev_uuid_nr(chunk, i),
2351 BTRFS_UUID_SIZE);
2352 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
dfe25020
CM
2353
2354 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
593060d7
CM
2355 kfree(map);
2356 free_extent_map(em);
2357 return -EIO;
2358 }
dfe25020
CM
2359 if (!map->stripes[i].dev) {
2360 map->stripes[i].dev =
2361 add_missing_dev(root, devid, uuid);
2362 if (!map->stripes[i].dev) {
2363 kfree(map);
2364 free_extent_map(em);
2365 return -EIO;
2366 }
2367 }
2368 map->stripes[i].dev->in_fs_metadata = 1;
0b86a832
CM
2369 }
2370
2371 spin_lock(&map_tree->map_tree.lock);
2372 ret = add_extent_mapping(&map_tree->map_tree, em);
0b86a832 2373 spin_unlock(&map_tree->map_tree.lock);
b248a415 2374 BUG_ON(ret);
0b86a832
CM
2375 free_extent_map(em);
2376
2377 return 0;
2378}
2379
2380static int fill_device_from_item(struct extent_buffer *leaf,
2381 struct btrfs_dev_item *dev_item,
2382 struct btrfs_device *device)
2383{
2384 unsigned long ptr;
0b86a832
CM
2385
2386 device->devid = btrfs_device_id(leaf, dev_item);
2387 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2388 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2389 device->type = btrfs_device_type(leaf, dev_item);
2390 device->io_align = btrfs_device_io_align(leaf, dev_item);
2391 device->io_width = btrfs_device_io_width(leaf, dev_item);
2392 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
0b86a832
CM
2393
2394 ptr = (unsigned long)btrfs_device_uuid(dev_item);
e17cade2 2395 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
0b86a832 2396
0b86a832
CM
2397 return 0;
2398}
2399
0d81ba5d 2400static int read_one_dev(struct btrfs_root *root,
0b86a832
CM
2401 struct extent_buffer *leaf,
2402 struct btrfs_dev_item *dev_item)
2403{
2404 struct btrfs_device *device;
2405 u64 devid;
2406 int ret;
a443755f
CM
2407 u8 dev_uuid[BTRFS_UUID_SIZE];
2408
0b86a832 2409 devid = btrfs_device_id(leaf, dev_item);
a443755f
CM
2410 read_extent_buffer(leaf, dev_uuid,
2411 (unsigned long)btrfs_device_uuid(dev_item),
2412 BTRFS_UUID_SIZE);
2413 device = btrfs_find_device(root, devid, dev_uuid);
6324fbf3 2414 if (!device) {
dfe25020
CM
2415 printk("warning devid %Lu missing\n", devid);
2416 device = add_missing_dev(root, devid, dev_uuid);
6324fbf3
CM
2417 if (!device)
2418 return -ENOMEM;
6324fbf3 2419 }
0b86a832
CM
2420
2421 fill_device_from_item(leaf, dev_item, device);
2422 device->dev_root = root->fs_info->dev_root;
dfe25020 2423 device->in_fs_metadata = 1;
0b86a832
CM
2424 ret = 0;
2425#if 0
2426 ret = btrfs_open_device(device);
2427 if (ret) {
2428 kfree(device);
2429 }
2430#endif
2431 return ret;
2432}
2433
0d81ba5d
CM
2434int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
2435{
2436 struct btrfs_dev_item *dev_item;
2437
2438 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
2439 dev_item);
2440 return read_one_dev(root, buf, dev_item);
2441}
2442
0b86a832
CM
2443int btrfs_read_sys_array(struct btrfs_root *root)
2444{
2445 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
a061fc8d 2446 struct extent_buffer *sb;
0b86a832 2447 struct btrfs_disk_key *disk_key;
0b86a832 2448 struct btrfs_chunk *chunk;
84eed90f
CM
2449 u8 *ptr;
2450 unsigned long sb_ptr;
2451 int ret = 0;
0b86a832
CM
2452 u32 num_stripes;
2453 u32 array_size;
2454 u32 len = 0;
0b86a832 2455 u32 cur;
84eed90f 2456 struct btrfs_key key;
0b86a832 2457
a061fc8d
CM
2458 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
2459 BTRFS_SUPER_INFO_SIZE);
2460 if (!sb)
2461 return -ENOMEM;
2462 btrfs_set_buffer_uptodate(sb);
2463 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
0b86a832
CM
2464 array_size = btrfs_super_sys_array_size(super_copy);
2465
0b86a832
CM
2466 ptr = super_copy->sys_chunk_array;
2467 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
2468 cur = 0;
2469
2470 while (cur < array_size) {
2471 disk_key = (struct btrfs_disk_key *)ptr;
2472 btrfs_disk_key_to_cpu(&key, disk_key);
2473
a061fc8d 2474 len = sizeof(*disk_key); ptr += len;
0b86a832
CM
2475 sb_ptr += len;
2476 cur += len;
2477
0d81ba5d 2478 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
0b86a832 2479 chunk = (struct btrfs_chunk *)sb_ptr;
0d81ba5d 2480 ret = read_one_chunk(root, &key, sb, chunk);
84eed90f
CM
2481 if (ret)
2482 break;
0b86a832
CM
2483 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
2484 len = btrfs_chunk_item_size(num_stripes);
2485 } else {
84eed90f
CM
2486 ret = -EIO;
2487 break;
0b86a832
CM
2488 }
2489 ptr += len;
2490 sb_ptr += len;
2491 cur += len;
2492 }
a061fc8d 2493 free_extent_buffer(sb);
84eed90f 2494 return ret;
0b86a832
CM
2495}
2496
2497int btrfs_read_chunk_tree(struct btrfs_root *root)
2498{
2499 struct btrfs_path *path;
2500 struct extent_buffer *leaf;
2501 struct btrfs_key key;
2502 struct btrfs_key found_key;
2503 int ret;
2504 int slot;
2505
2506 root = root->fs_info->chunk_root;
2507
2508 path = btrfs_alloc_path();
2509 if (!path)
2510 return -ENOMEM;
2511
2512 /* first we search for all of the device items, and then we
2513 * read in all of the chunk items. This way we can create chunk
2514 * mappings that reference all of the devices that are afound
2515 */
2516 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2517 key.offset = 0;
2518 key.type = 0;
2519again:
2520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2521 while(1) {
2522 leaf = path->nodes[0];
2523 slot = path->slots[0];
2524 if (slot >= btrfs_header_nritems(leaf)) {
2525 ret = btrfs_next_leaf(root, path);
2526 if (ret == 0)
2527 continue;
2528 if (ret < 0)
2529 goto error;
2530 break;
2531 }
2532 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2533 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2534 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
2535 break;
2536 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
2537 struct btrfs_dev_item *dev_item;
2538 dev_item = btrfs_item_ptr(leaf, slot,
2539 struct btrfs_dev_item);
0d81ba5d 2540 ret = read_one_dev(root, leaf, dev_item);
0b86a832
CM
2541 BUG_ON(ret);
2542 }
2543 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
2544 struct btrfs_chunk *chunk;
2545 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2546 ret = read_one_chunk(root, &found_key, leaf, chunk);
2547 }
2548 path->slots[0]++;
2549 }
2550 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
2551 key.objectid = 0;
2552 btrfs_release_path(root, path);
2553 goto again;
2554 }
2555
2556 btrfs_free_path(path);
2557 ret = 0;
2558error:
2559 return ret;
2560}