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