aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/volumes.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-09-12 12:58:51 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-09-12 12:58:51 -0400
commitb7c09ad4014e3678e8cc01fdf663c9f43b272dc6 (patch)
tree1edb073b0a76ce1530cb31c113f9e741e33ece0e /fs/btrfs/volumes.c
parent1812997720ab90d029548778c55d7315555e1fef (diff)
parentd7396f07358a7c6e22c238d36d1d85f9d652a414 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs
Pull btrfs updates from Chris Mason: "This is against 3.11-rc7, but was pulled and tested against your tree as of yesterday. We do have two small incrementals queued up, but I wanted to get this bunch out the door before I hop on an airplane. This is a fairly large batch of fixes, performance improvements, and cleanups from the usual Btrfs suspects. We've included Stefan Behren's work to index subvolume UUIDs, which is targeted at speeding up send/receive with many subvolumes or snapshots in place. It closes a long standing performance issue that was built in to the disk format. Mark Fasheh's offline dedup work is also here. In this case offline means the FS is mounted and active, but the dedup work is not done inline during file IO. This is a building block where utilities are able to ask the FS to dedup a series of extents. The kernel takes care of verifying the data involved really is the same. Today this involves reading both extents, but we'll continue to evolve the patches" * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: (118 commits) Btrfs: optimize key searches in btrfs_search_slot Btrfs: don't use an async starter for most of our workers Btrfs: only update disk_i_size as we remove extents Btrfs: fix deadlock in uuid scan kthread Btrfs: stop refusing the relocation of chunk 0 Btrfs: fix memory leak of uuid_root in free_fs_info btrfs: reuse kbasename helper btrfs: return btrfs error code for dev excl ops err Btrfs: allow partial ordered extent completion Btrfs: convert all bug_ons in free-space-cache.c Btrfs: add support for asserts Btrfs: adjust the fs_devices->missing count on unmount Btrf: cleanup: don't check for root_refs == 0 twice Btrfs: fix for patch "cleanup: don't check the same thing twice" Btrfs: get rid of one BUG() in write_all_supers() Btrfs: allocate prelim_ref with a slab allocater Btrfs: pass gfp_t to __add_prelim_ref() to avoid always using GFP_ATOMIC Btrfs: fix race conditions in BTRFS_IOC_FS_INFO ioctl Btrfs: fix race between removing a dev and writing sbs Btrfs: remove ourselves from the cluster list under lock ...
Diffstat (limited to 'fs/btrfs/volumes.c')
-rw-r--r--fs/btrfs/volumes.c613
1 files changed, 464 insertions, 149 deletions
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 67a085381845..0052ca8264d9 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -26,6 +26,7 @@
26#include <linux/ratelimit.h> 26#include <linux/ratelimit.h>
27#include <linux/kthread.h> 27#include <linux/kthread.h>
28#include <linux/raid/pq.h> 28#include <linux/raid/pq.h>
29#include <linux/semaphore.h>
29#include <asm/div64.h> 30#include <asm/div64.h>
30#include "compat.h" 31#include "compat.h"
31#include "ctree.h" 32#include "ctree.h"
@@ -62,6 +63,48 @@ static void unlock_chunks(struct btrfs_root *root)
62 mutex_unlock(&root->fs_info->chunk_mutex); 63 mutex_unlock(&root->fs_info->chunk_mutex);
63} 64}
64 65
66static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
77 INIT_LIST_HEAD(&fs_devs->alloc_list);
78 INIT_LIST_HEAD(&fs_devs->list);
79
80 return fs_devs;
81}
82
83/**
84 * alloc_fs_devices - allocate struct btrfs_fs_devices
85 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
86 * generated.
87 *
88 * Return: a pointer to a new &struct btrfs_fs_devices on success;
89 * ERR_PTR() on error. Returned struct is not linked onto any lists and
90 * can be destroyed with kfree() right away.
91 */
92static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
93{
94 struct btrfs_fs_devices *fs_devs;
95
96 fs_devs = __alloc_fs_devices();
97 if (IS_ERR(fs_devs))
98 return fs_devs;
99
100 if (fsid)
101 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
102 else
103 generate_random_uuid(fs_devs->fsid);
104
105 return fs_devs;
106}
107
65static void free_fs_devices(struct btrfs_fs_devices *fs_devices) 108static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
66{ 109{
67 struct btrfs_device *device; 110 struct btrfs_device *device;
@@ -101,6 +144,27 @@ void btrfs_cleanup_fs_uuids(void)
101 } 144 }
102} 145}
103 146
147static struct btrfs_device *__alloc_device(void)
148{
149 struct btrfs_device *dev;
150
151 dev = kzalloc(sizeof(*dev), GFP_NOFS);
152 if (!dev)
153 return ERR_PTR(-ENOMEM);
154
155 INIT_LIST_HEAD(&dev->dev_list);
156 INIT_LIST_HEAD(&dev->dev_alloc_list);
157
158 spin_lock_init(&dev->io_lock);
159
160 spin_lock_init(&dev->reada_lock);
161 atomic_set(&dev->reada_in_flight, 0);
162 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
163 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
164
165 return dev;
166}
167
104static noinline struct btrfs_device *__find_device(struct list_head *head, 168static noinline struct btrfs_device *__find_device(struct list_head *head,
105 u64 devid, u8 *uuid) 169 u64 devid, u8 *uuid)
106{ 170{
@@ -395,16 +459,14 @@ static noinline int device_list_add(const char *path,
395 459
396 fs_devices = find_fsid(disk_super->fsid); 460 fs_devices = find_fsid(disk_super->fsid);
397 if (!fs_devices) { 461 if (!fs_devices) {
398 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); 462 fs_devices = alloc_fs_devices(disk_super->fsid);
399 if (!fs_devices) 463 if (IS_ERR(fs_devices))
400 return -ENOMEM; 464 return PTR_ERR(fs_devices);
401 INIT_LIST_HEAD(&fs_devices->devices); 465
402 INIT_LIST_HEAD(&fs_devices->alloc_list);
403 list_add(&fs_devices->list, &fs_uuids); 466 list_add(&fs_devices->list, &fs_uuids);
404 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
405 fs_devices->latest_devid = devid; 467 fs_devices->latest_devid = devid;
406 fs_devices->latest_trans = found_transid; 468 fs_devices->latest_trans = found_transid;
407 mutex_init(&fs_devices->device_list_mutex); 469
408 device = NULL; 470 device = NULL;
409 } else { 471 } else {
410 device = __find_device(&fs_devices->devices, devid, 472 device = __find_device(&fs_devices->devices, devid,
@@ -414,17 +476,12 @@ static noinline int device_list_add(const char *path,
414 if (fs_devices->opened) 476 if (fs_devices->opened)
415 return -EBUSY; 477 return -EBUSY;
416 478
417 device = kzalloc(sizeof(*device), GFP_NOFS); 479 device = btrfs_alloc_device(NULL, &devid,
418 if (!device) { 480 disk_super->dev_item.uuid);
481 if (IS_ERR(device)) {
419 /* we can safely leave the fs_devices entry around */ 482 /* we can safely leave the fs_devices entry around */
420 return -ENOMEM; 483 return PTR_ERR(device);
421 } 484 }
422 device->devid = devid;
423 device->dev_stats_valid = 0;
424 device->work.func = pending_bios_fn;
425 memcpy(device->uuid, disk_super->dev_item.uuid,
426 BTRFS_UUID_SIZE);
427 spin_lock_init(&device->io_lock);
428 485
429 name = rcu_string_strdup(path, GFP_NOFS); 486 name = rcu_string_strdup(path, GFP_NOFS);
430 if (!name) { 487 if (!name) {
@@ -432,22 +489,13 @@ static noinline int device_list_add(const char *path,
432 return -ENOMEM; 489 return -ENOMEM;
433 } 490 }
434 rcu_assign_pointer(device->name, name); 491 rcu_assign_pointer(device->name, name);
435 INIT_LIST_HEAD(&device->dev_alloc_list);
436
437 /* init readahead state */
438 spin_lock_init(&device->reada_lock);
439 device->reada_curr_zone = NULL;
440 atomic_set(&device->reada_in_flight, 0);
441 device->reada_next = 0;
442 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
443 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
444 492
445 mutex_lock(&fs_devices->device_list_mutex); 493 mutex_lock(&fs_devices->device_list_mutex);
446 list_add_rcu(&device->dev_list, &fs_devices->devices); 494 list_add_rcu(&device->dev_list, &fs_devices->devices);
495 fs_devices->num_devices++;
447 mutex_unlock(&fs_devices->device_list_mutex); 496 mutex_unlock(&fs_devices->device_list_mutex);
448 497
449 device->fs_devices = fs_devices; 498 device->fs_devices = fs_devices;
450 fs_devices->num_devices++;
451 } else if (!device->name || strcmp(device->name->str, path)) { 499 } else if (!device->name || strcmp(device->name->str, path)) {
452 name = rcu_string_strdup(path, GFP_NOFS); 500 name = rcu_string_strdup(path, GFP_NOFS);
453 if (!name) 501 if (!name)
@@ -474,25 +522,21 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
474 struct btrfs_device *device; 522 struct btrfs_device *device;
475 struct btrfs_device *orig_dev; 523 struct btrfs_device *orig_dev;
476 524
477 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); 525 fs_devices = alloc_fs_devices(orig->fsid);
478 if (!fs_devices) 526 if (IS_ERR(fs_devices))
479 return ERR_PTR(-ENOMEM); 527 return fs_devices;
480 528
481 INIT_LIST_HEAD(&fs_devices->devices);
482 INIT_LIST_HEAD(&fs_devices->alloc_list);
483 INIT_LIST_HEAD(&fs_devices->list);
484 mutex_init(&fs_devices->device_list_mutex);
485 fs_devices->latest_devid = orig->latest_devid; 529 fs_devices->latest_devid = orig->latest_devid;
486 fs_devices->latest_trans = orig->latest_trans; 530 fs_devices->latest_trans = orig->latest_trans;
487 fs_devices->total_devices = orig->total_devices; 531 fs_devices->total_devices = orig->total_devices;
488 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
489 532
490 /* We have held the volume lock, it is safe to get the devices. */ 533 /* We have held the volume lock, it is safe to get the devices. */
491 list_for_each_entry(orig_dev, &orig->devices, dev_list) { 534 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
492 struct rcu_string *name; 535 struct rcu_string *name;
493 536
494 device = kzalloc(sizeof(*device), GFP_NOFS); 537 device = btrfs_alloc_device(NULL, &orig_dev->devid,
495 if (!device) 538 orig_dev->uuid);
539 if (IS_ERR(device))
496 goto error; 540 goto error;
497 541
498 /* 542 /*
@@ -506,13 +550,6 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
506 } 550 }
507 rcu_assign_pointer(device->name, name); 551 rcu_assign_pointer(device->name, name);
508 552
509 device->devid = orig_dev->devid;
510 device->work.func = pending_bios_fn;
511 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
512 spin_lock_init(&device->io_lock);
513 INIT_LIST_HEAD(&device->dev_list);
514 INIT_LIST_HEAD(&device->dev_alloc_list);
515
516 list_add(&device->dev_list, &fs_devices->devices); 553 list_add(&device->dev_list, &fs_devices->devices);
517 device->fs_devices = fs_devices; 554 device->fs_devices = fs_devices;
518 fs_devices->num_devices++; 555 fs_devices->num_devices++;
@@ -636,23 +673,22 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
636 673
637 if (device->can_discard) 674 if (device->can_discard)
638 fs_devices->num_can_discard--; 675 fs_devices->num_can_discard--;
676 if (device->missing)
677 fs_devices->missing_devices--;
639 678
640 new_device = kmalloc(sizeof(*new_device), GFP_NOFS); 679 new_device = btrfs_alloc_device(NULL, &device->devid,
641 BUG_ON(!new_device); /* -ENOMEM */ 680 device->uuid);
642 memcpy(new_device, device, sizeof(*new_device)); 681 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
643 682
644 /* Safe because we are under uuid_mutex */ 683 /* Safe because we are under uuid_mutex */
645 if (device->name) { 684 if (device->name) {
646 name = rcu_string_strdup(device->name->str, GFP_NOFS); 685 name = rcu_string_strdup(device->name->str, GFP_NOFS);
647 BUG_ON(device->name && !name); /* -ENOMEM */ 686 BUG_ON(!name); /* -ENOMEM */
648 rcu_assign_pointer(new_device->name, name); 687 rcu_assign_pointer(new_device->name, name);
649 } 688 }
650 new_device->bdev = NULL; 689
651 new_device->writeable = 0;
652 new_device->in_fs_metadata = 0;
653 new_device->can_discard = 0;
654 spin_lock_init(&new_device->io_lock);
655 list_replace_rcu(&device->dev_list, &new_device->dev_list); 690 list_replace_rcu(&device->dev_list, &new_device->dev_list);
691 new_device->fs_devices = device->fs_devices;
656 692
657 call_rcu(&device->rcu, free_device); 693 call_rcu(&device->rcu, free_device);
658 } 694 }
@@ -865,7 +901,7 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
865 disk_super = p + (bytenr & ~PAGE_CACHE_MASK); 901 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
866 902
867 if (btrfs_super_bytenr(disk_super) != bytenr || 903 if (btrfs_super_bytenr(disk_super) != bytenr ||
868 disk_super->magic != cpu_to_le64(BTRFS_MAGIC)) 904 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
869 goto error_unmap; 905 goto error_unmap;
870 906
871 devid = btrfs_stack_device_id(&disk_super->dev_item); 907 devid = btrfs_stack_device_id(&disk_super->dev_item);
@@ -880,8 +916,7 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
880 printk(KERN_INFO "device fsid %pU ", disk_super->fsid); 916 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
881 } 917 }
882 918
883 printk(KERN_CONT "devid %llu transid %llu %s\n", 919 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
884 (unsigned long long)devid, (unsigned long long)transid, path);
885 920
886 ret = device_list_add(path, disk_super, devid, fs_devices_ret); 921 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
887 if (!ret && fs_devices_ret) 922 if (!ret && fs_devices_ret)
@@ -1278,8 +1313,7 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1278 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); 1313 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1279 1314
1280 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 1315 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1281 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), 1316 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
1282 BTRFS_UUID_SIZE);
1283 1317
1284 btrfs_set_dev_extent_length(leaf, extent, num_bytes); 1318 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1285 btrfs_mark_buffer_dirty(leaf); 1319 btrfs_mark_buffer_dirty(leaf);
@@ -1307,15 +1341,14 @@ static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1307 return ret; 1341 return ret;
1308} 1342}
1309 1343
1310static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) 1344static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1345 u64 *devid_ret)
1311{ 1346{
1312 int ret; 1347 int ret;
1313 struct btrfs_key key; 1348 struct btrfs_key key;
1314 struct btrfs_key found_key; 1349 struct btrfs_key found_key;
1315 struct btrfs_path *path; 1350 struct btrfs_path *path;
1316 1351
1317 root = root->fs_info->chunk_root;
1318
1319 path = btrfs_alloc_path(); 1352 path = btrfs_alloc_path();
1320 if (!path) 1353 if (!path)
1321 return -ENOMEM; 1354 return -ENOMEM;
@@ -1324,20 +1357,21 @@ static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1324 key.type = BTRFS_DEV_ITEM_KEY; 1357 key.type = BTRFS_DEV_ITEM_KEY;
1325 key.offset = (u64)-1; 1358 key.offset = (u64)-1;
1326 1359
1327 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1360 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1328 if (ret < 0) 1361 if (ret < 0)
1329 goto error; 1362 goto error;
1330 1363
1331 BUG_ON(ret == 0); /* Corruption */ 1364 BUG_ON(ret == 0); /* Corruption */
1332 1365
1333 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, 1366 ret = btrfs_previous_item(fs_info->chunk_root, path,
1367 BTRFS_DEV_ITEMS_OBJECTID,
1334 BTRFS_DEV_ITEM_KEY); 1368 BTRFS_DEV_ITEM_KEY);
1335 if (ret) { 1369 if (ret) {
1336 *objectid = 1; 1370 *devid_ret = 1;
1337 } else { 1371 } else {
1338 btrfs_item_key_to_cpu(path->nodes[0], &found_key, 1372 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1339 path->slots[0]); 1373 path->slots[0]);
1340 *objectid = found_key.offset + 1; 1374 *devid_ret = found_key.offset + 1;
1341 } 1375 }
1342 ret = 0; 1376 ret = 0;
1343error: 1377error:
@@ -1391,9 +1425,9 @@ static int btrfs_add_device(struct btrfs_trans_handle *trans,
1391 btrfs_set_device_bandwidth(leaf, dev_item, 0); 1425 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1392 btrfs_set_device_start_offset(leaf, dev_item, 0); 1426 btrfs_set_device_start_offset(leaf, dev_item, 0);
1393 1427
1394 ptr = (unsigned long)btrfs_device_uuid(dev_item); 1428 ptr = btrfs_device_uuid(dev_item);
1395 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); 1429 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1396 ptr = (unsigned long)btrfs_device_fsid(dev_item); 1430 ptr = btrfs_device_fsid(dev_item);
1397 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); 1431 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1398 btrfs_mark_buffer_dirty(leaf); 1432 btrfs_mark_buffer_dirty(leaf);
1399 1433
@@ -1562,7 +1596,9 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1562 clear_super = true; 1596 clear_super = true;
1563 } 1597 }
1564 1598
1599 mutex_unlock(&uuid_mutex);
1565 ret = btrfs_shrink_device(device, 0); 1600 ret = btrfs_shrink_device(device, 0);
1601 mutex_lock(&uuid_mutex);
1566 if (ret) 1602 if (ret)
1567 goto error_undo; 1603 goto error_undo;
1568 1604
@@ -1586,7 +1622,11 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1586 /* 1622 /*
1587 * the device list mutex makes sure that we don't change 1623 * the device list mutex makes sure that we don't change
1588 * the device list while someone else is writing out all 1624 * the device list while someone else is writing out all
1589 * the device supers. 1625 * the device supers. Whoever is writing all supers, should
1626 * lock the device list mutex before getting the number of
1627 * devices in the super block (super_copy). Conversely,
1628 * whoever updates the number of devices in the super block
1629 * (super_copy) should hold the device list mutex.
1590 */ 1630 */
1591 1631
1592 cur_devices = device->fs_devices; 1632 cur_devices = device->fs_devices;
@@ -1610,10 +1650,10 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1610 device->fs_devices->open_devices--; 1650 device->fs_devices->open_devices--;
1611 1651
1612 call_rcu(&device->rcu, free_device); 1652 call_rcu(&device->rcu, free_device);
1613 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1614 1653
1615 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 1654 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1616 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); 1655 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1656 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1617 1657
1618 if (cur_devices->open_devices == 0) { 1658 if (cur_devices->open_devices == 0) {
1619 struct btrfs_fs_devices *fs_devices; 1659 struct btrfs_fs_devices *fs_devices;
@@ -1793,9 +1833,9 @@ static int btrfs_prepare_sprout(struct btrfs_root *root)
1793 if (!fs_devices->seeding) 1833 if (!fs_devices->seeding)
1794 return -EINVAL; 1834 return -EINVAL;
1795 1835
1796 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); 1836 seed_devices = __alloc_fs_devices();
1797 if (!seed_devices) 1837 if (IS_ERR(seed_devices))
1798 return -ENOMEM; 1838 return PTR_ERR(seed_devices);
1799 1839
1800 old_devices = clone_fs_devices(fs_devices); 1840 old_devices = clone_fs_devices(fs_devices);
1801 if (IS_ERR(old_devices)) { 1841 if (IS_ERR(old_devices)) {
@@ -1814,7 +1854,6 @@ static int btrfs_prepare_sprout(struct btrfs_root *root)
1814 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 1854 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1815 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, 1855 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1816 synchronize_rcu); 1856 synchronize_rcu);
1817 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1818 1857
1819 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); 1858 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1820 list_for_each_entry(device, &seed_devices->devices, dev_list) { 1859 list_for_each_entry(device, &seed_devices->devices, dev_list) {
@@ -1830,6 +1869,8 @@ static int btrfs_prepare_sprout(struct btrfs_root *root)
1830 generate_random_uuid(fs_devices->fsid); 1869 generate_random_uuid(fs_devices->fsid);
1831 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); 1870 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1832 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); 1871 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1872 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1873
1833 super_flags = btrfs_super_flags(disk_super) & 1874 super_flags = btrfs_super_flags(disk_super) &
1834 ~BTRFS_SUPER_FLAG_SEEDING; 1875 ~BTRFS_SUPER_FLAG_SEEDING;
1835 btrfs_set_super_flags(disk_super, super_flags); 1876 btrfs_set_super_flags(disk_super, super_flags);
@@ -1889,11 +1930,9 @@ next_slot:
1889 dev_item = btrfs_item_ptr(leaf, path->slots[0], 1930 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1890 struct btrfs_dev_item); 1931 struct btrfs_dev_item);
1891 devid = btrfs_device_id(leaf, dev_item); 1932 devid = btrfs_device_id(leaf, dev_item);
1892 read_extent_buffer(leaf, dev_uuid, 1933 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
1893 (unsigned long)btrfs_device_uuid(dev_item),
1894 BTRFS_UUID_SIZE); 1934 BTRFS_UUID_SIZE);
1895 read_extent_buffer(leaf, fs_uuid, 1935 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
1896 (unsigned long)btrfs_device_fsid(dev_item),
1897 BTRFS_UUID_SIZE); 1936 BTRFS_UUID_SIZE);
1898 device = btrfs_find_device(root->fs_info, devid, dev_uuid, 1937 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1899 fs_uuid); 1938 fs_uuid);
@@ -1956,10 +1995,10 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1956 } 1995 }
1957 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 1996 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1958 1997
1959 device = kzalloc(sizeof(*device), GFP_NOFS); 1998 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
1960 if (!device) { 1999 if (IS_ERR(device)) {
1961 /* we can safely leave the fs_devices entry around */ 2000 /* we can safely leave the fs_devices entry around */
1962 ret = -ENOMEM; 2001 ret = PTR_ERR(device);
1963 goto error; 2002 goto error;
1964 } 2003 }
1965 2004
@@ -1971,13 +2010,6 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1971 } 2010 }
1972 rcu_assign_pointer(device->name, name); 2011 rcu_assign_pointer(device->name, name);
1973 2012
1974 ret = find_next_devid(root, &device->devid);
1975 if (ret) {
1976 rcu_string_free(device->name);
1977 kfree(device);
1978 goto error;
1979 }
1980
1981 trans = btrfs_start_transaction(root, 0); 2013 trans = btrfs_start_transaction(root, 0);
1982 if (IS_ERR(trans)) { 2014 if (IS_ERR(trans)) {
1983 rcu_string_free(device->name); 2015 rcu_string_free(device->name);
@@ -1992,9 +2024,6 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1992 if (blk_queue_discard(q)) 2024 if (blk_queue_discard(q))
1993 device->can_discard = 1; 2025 device->can_discard = 1;
1994 device->writeable = 1; 2026 device->writeable = 1;
1995 device->work.func = pending_bios_fn;
1996 generate_random_uuid(device->uuid);
1997 spin_lock_init(&device->io_lock);
1998 device->generation = trans->transid; 2027 device->generation = trans->transid;
1999 device->io_width = root->sectorsize; 2028 device->io_width = root->sectorsize;
2000 device->io_align = root->sectorsize; 2029 device->io_align = root->sectorsize;
@@ -2121,6 +2150,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2121 struct btrfs_fs_info *fs_info = root->fs_info; 2150 struct btrfs_fs_info *fs_info = root->fs_info;
2122 struct list_head *devices; 2151 struct list_head *devices;
2123 struct rcu_string *name; 2152 struct rcu_string *name;
2153 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2124 int ret = 0; 2154 int ret = 0;
2125 2155
2126 *device_out = NULL; 2156 *device_out = NULL;
@@ -2142,9 +2172,9 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2142 } 2172 }
2143 } 2173 }
2144 2174
2145 device = kzalloc(sizeof(*device), GFP_NOFS); 2175 device = btrfs_alloc_device(NULL, &devid, NULL);
2146 if (!device) { 2176 if (IS_ERR(device)) {
2147 ret = -ENOMEM; 2177 ret = PTR_ERR(device);
2148 goto error; 2178 goto error;
2149 } 2179 }
2150 2180
@@ -2161,10 +2191,6 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2161 device->can_discard = 1; 2191 device->can_discard = 1;
2162 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2192 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2163 device->writeable = 1; 2193 device->writeable = 1;
2164 device->work.func = pending_bios_fn;
2165 generate_random_uuid(device->uuid);
2166 device->devid = BTRFS_DEV_REPLACE_DEVID;
2167 spin_lock_init(&device->io_lock);
2168 device->generation = 0; 2194 device->generation = 0;
2169 device->io_width = root->sectorsize; 2195 device->io_width = root->sectorsize;
2170 device->io_align = root->sectorsize; 2196 device->io_align = root->sectorsize;
@@ -2971,10 +2997,6 @@ again:
2971 if (found_key.objectid != key.objectid) 2997 if (found_key.objectid != key.objectid)
2972 break; 2998 break;
2973 2999
2974 /* chunk zero is special */
2975 if (found_key.offset == 0)
2976 break;
2977
2978 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); 3000 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2979 3001
2980 if (!counting) { 3002 if (!counting) {
@@ -3010,6 +3032,8 @@ again:
3010 spin_unlock(&fs_info->balance_lock); 3032 spin_unlock(&fs_info->balance_lock);
3011 } 3033 }
3012loop: 3034loop:
3035 if (found_key.offset == 0)
3036 break;
3013 key.offset = found_key.offset - 1; 3037 key.offset = found_key.offset - 1;
3014 } 3038 }
3015 3039
@@ -3074,9 +3098,6 @@ static void __cancel_balance(struct btrfs_fs_info *fs_info)
3074 atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3098 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3075} 3099}
3076 3100
3077void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3078 struct btrfs_ioctl_balance_args *bargs);
3079
3080/* 3101/*
3081 * Should be called with both balance and volume mutexes held 3102 * Should be called with both balance and volume mutexes held
3082 */ 3103 */
@@ -3139,7 +3160,7 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
3139 (bctl->data.target & ~allowed))) { 3160 (bctl->data.target & ~allowed))) {
3140 printk(KERN_ERR "btrfs: unable to start balance with target " 3161 printk(KERN_ERR "btrfs: unable to start balance with target "
3141 "data profile %llu\n", 3162 "data profile %llu\n",
3142 (unsigned long long)bctl->data.target); 3163 bctl->data.target);
3143 ret = -EINVAL; 3164 ret = -EINVAL;
3144 goto out; 3165 goto out;
3145 } 3166 }
@@ -3148,7 +3169,7 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
3148 (bctl->meta.target & ~allowed))) { 3169 (bctl->meta.target & ~allowed))) {
3149 printk(KERN_ERR "btrfs: unable to start balance with target " 3170 printk(KERN_ERR "btrfs: unable to start balance with target "
3150 "metadata profile %llu\n", 3171 "metadata profile %llu\n",
3151 (unsigned long long)bctl->meta.target); 3172 bctl->meta.target);
3152 ret = -EINVAL; 3173 ret = -EINVAL;
3153 goto out; 3174 goto out;
3154 } 3175 }
@@ -3157,7 +3178,7 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
3157 (bctl->sys.target & ~allowed))) { 3178 (bctl->sys.target & ~allowed))) {
3158 printk(KERN_ERR "btrfs: unable to start balance with target " 3179 printk(KERN_ERR "btrfs: unable to start balance with target "
3159 "system profile %llu\n", 3180 "system profile %llu\n",
3160 (unsigned long long)bctl->sys.target); 3181 bctl->sys.target);
3161 ret = -EINVAL; 3182 ret = -EINVAL;
3162 goto out; 3183 goto out;
3163 } 3184 }
@@ -3430,6 +3451,264 @@ int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3430 return 0; 3451 return 0;
3431} 3452}
3432 3453
3454static int btrfs_uuid_scan_kthread(void *data)
3455{
3456 struct btrfs_fs_info *fs_info = data;
3457 struct btrfs_root *root = fs_info->tree_root;
3458 struct btrfs_key key;
3459 struct btrfs_key max_key;
3460 struct btrfs_path *path = NULL;
3461 int ret = 0;
3462 struct extent_buffer *eb;
3463 int slot;
3464 struct btrfs_root_item root_item;
3465 u32 item_size;
3466 struct btrfs_trans_handle *trans = NULL;
3467
3468 path = btrfs_alloc_path();
3469 if (!path) {
3470 ret = -ENOMEM;
3471 goto out;
3472 }
3473
3474 key.objectid = 0;
3475 key.type = BTRFS_ROOT_ITEM_KEY;
3476 key.offset = 0;
3477
3478 max_key.objectid = (u64)-1;
3479 max_key.type = BTRFS_ROOT_ITEM_KEY;
3480 max_key.offset = (u64)-1;
3481
3482 path->keep_locks = 1;
3483
3484 while (1) {
3485 ret = btrfs_search_forward(root, &key, &max_key, path, 0);
3486 if (ret) {
3487 if (ret > 0)
3488 ret = 0;
3489 break;
3490 }
3491
3492 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3493 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3494 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3495 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3496 goto skip;
3497
3498 eb = path->nodes[0];
3499 slot = path->slots[0];
3500 item_size = btrfs_item_size_nr(eb, slot);
3501 if (item_size < sizeof(root_item))
3502 goto skip;
3503
3504 read_extent_buffer(eb, &root_item,
3505 btrfs_item_ptr_offset(eb, slot),
3506 (int)sizeof(root_item));
3507 if (btrfs_root_refs(&root_item) == 0)
3508 goto skip;
3509
3510 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3511 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3512 if (trans)
3513 goto update_tree;
3514
3515 btrfs_release_path(path);
3516 /*
3517 * 1 - subvol uuid item
3518 * 1 - received_subvol uuid item
3519 */
3520 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3521 if (IS_ERR(trans)) {
3522 ret = PTR_ERR(trans);
3523 break;
3524 }
3525 continue;
3526 } else {
3527 goto skip;
3528 }
3529update_tree:
3530 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3531 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3532 root_item.uuid,
3533 BTRFS_UUID_KEY_SUBVOL,
3534 key.objectid);
3535 if (ret < 0) {
3536 pr_warn("btrfs: uuid_tree_add failed %d\n",
3537 ret);
3538 break;
3539 }
3540 }
3541
3542 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3543 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3544 root_item.received_uuid,
3545 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3546 key.objectid);
3547 if (ret < 0) {
3548 pr_warn("btrfs: uuid_tree_add failed %d\n",
3549 ret);
3550 break;
3551 }
3552 }
3553
3554skip:
3555 if (trans) {
3556 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3557 trans = NULL;
3558 if (ret)
3559 break;
3560 }
3561
3562 btrfs_release_path(path);
3563 if (key.offset < (u64)-1) {
3564 key.offset++;
3565 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3566 key.offset = 0;
3567 key.type = BTRFS_ROOT_ITEM_KEY;
3568 } else if (key.objectid < (u64)-1) {
3569 key.offset = 0;
3570 key.type = BTRFS_ROOT_ITEM_KEY;
3571 key.objectid++;
3572 } else {
3573 break;
3574 }
3575 cond_resched();
3576 }
3577
3578out:
3579 btrfs_free_path(path);
3580 if (trans && !IS_ERR(trans))
3581 btrfs_end_transaction(trans, fs_info->uuid_root);
3582 if (ret)
3583 pr_warn("btrfs: btrfs_uuid_scan_kthread failed %d\n", ret);
3584 else
3585 fs_info->update_uuid_tree_gen = 1;
3586 up(&fs_info->uuid_tree_rescan_sem);
3587 return 0;
3588}
3589
3590/*
3591 * Callback for btrfs_uuid_tree_iterate().
3592 * returns:
3593 * 0 check succeeded, the entry is not outdated.
3594 * < 0 if an error occured.
3595 * > 0 if the check failed, which means the caller shall remove the entry.
3596 */
3597static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3598 u8 *uuid, u8 type, u64 subid)
3599{
3600 struct btrfs_key key;
3601 int ret = 0;
3602 struct btrfs_root *subvol_root;
3603
3604 if (type != BTRFS_UUID_KEY_SUBVOL &&
3605 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3606 goto out;
3607
3608 key.objectid = subid;
3609 key.type = BTRFS_ROOT_ITEM_KEY;
3610 key.offset = (u64)-1;
3611 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3612 if (IS_ERR(subvol_root)) {
3613 ret = PTR_ERR(subvol_root);
3614 if (ret == -ENOENT)
3615 ret = 1;
3616 goto out;
3617 }
3618
3619 switch (type) {
3620 case BTRFS_UUID_KEY_SUBVOL:
3621 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3622 ret = 1;
3623 break;
3624 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3625 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3626 BTRFS_UUID_SIZE))
3627 ret = 1;
3628 break;
3629 }
3630
3631out:
3632 return ret;
3633}
3634
3635static int btrfs_uuid_rescan_kthread(void *data)
3636{
3637 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3638 int ret;
3639
3640 /*
3641 * 1st step is to iterate through the existing UUID tree and
3642 * to delete all entries that contain outdated data.
3643 * 2nd step is to add all missing entries to the UUID tree.
3644 */
3645 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3646 if (ret < 0) {
3647 pr_warn("btrfs: iterating uuid_tree failed %d\n", ret);
3648 up(&fs_info->uuid_tree_rescan_sem);
3649 return ret;
3650 }
3651 return btrfs_uuid_scan_kthread(data);
3652}
3653
3654int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3655{
3656 struct btrfs_trans_handle *trans;
3657 struct btrfs_root *tree_root = fs_info->tree_root;
3658 struct btrfs_root *uuid_root;
3659 struct task_struct *task;
3660 int ret;
3661
3662 /*
3663 * 1 - root node
3664 * 1 - root item
3665 */
3666 trans = btrfs_start_transaction(tree_root, 2);
3667 if (IS_ERR(trans))
3668 return PTR_ERR(trans);
3669
3670 uuid_root = btrfs_create_tree(trans, fs_info,
3671 BTRFS_UUID_TREE_OBJECTID);
3672 if (IS_ERR(uuid_root)) {
3673 btrfs_abort_transaction(trans, tree_root,
3674 PTR_ERR(uuid_root));
3675 return PTR_ERR(uuid_root);
3676 }
3677
3678 fs_info->uuid_root = uuid_root;
3679
3680 ret = btrfs_commit_transaction(trans, tree_root);
3681 if (ret)
3682 return ret;
3683
3684 down(&fs_info->uuid_tree_rescan_sem);
3685 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3686 if (IS_ERR(task)) {
3687 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3688 pr_warn("btrfs: failed to start uuid_scan task\n");
3689 up(&fs_info->uuid_tree_rescan_sem);
3690 return PTR_ERR(task);
3691 }
3692
3693 return 0;
3694}
3695
3696int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3697{
3698 struct task_struct *task;
3699
3700 down(&fs_info->uuid_tree_rescan_sem);
3701 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3702 if (IS_ERR(task)) {
3703 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3704 pr_warn("btrfs: failed to start uuid_rescan task\n");
3705 up(&fs_info->uuid_tree_rescan_sem);
3706 return PTR_ERR(task);
3707 }
3708
3709 return 0;
3710}
3711
3433/* 3712/*
3434 * shrinking a device means finding all of the device extents past 3713 * shrinking a device means finding all of the device extents past
3435 * the new size, and then following the back refs to the chunks. 3714 * the new size, and then following the back refs to the chunks.
@@ -4194,13 +4473,13 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4194 * and exit, so return 1 so the callers don't try to use other copies. 4473 * and exit, so return 1 so the callers don't try to use other copies.
4195 */ 4474 */
4196 if (!em) { 4475 if (!em) {
4197 btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical, 4476 btrfs_crit(fs_info, "No mapping for %Lu-%Lu\n", logical,
4198 logical+len); 4477 logical+len);
4199 return 1; 4478 return 1;
4200 } 4479 }
4201 4480
4202 if (em->start > logical || em->start + em->len < logical) { 4481 if (em->start > logical || em->start + em->len < logical) {
4203 btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got " 4482 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
4204 "%Lu-%Lu\n", logical, logical+len, em->start, 4483 "%Lu-%Lu\n", logical, logical+len, em->start,
4205 em->start + em->len); 4484 em->start + em->len);
4206 return 1; 4485 return 1;
@@ -4375,8 +4654,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4375 4654
4376 if (!em) { 4655 if (!em) {
4377 btrfs_crit(fs_info, "unable to find logical %llu len %llu", 4656 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4378 (unsigned long long)logical, 4657 logical, *length);
4379 (unsigned long long)*length);
4380 return -EINVAL; 4658 return -EINVAL;
4381 } 4659 }
4382 4660
@@ -4671,6 +4949,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4671 } 4949 }
4672 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); 4950 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
4673 if (!bbio) { 4951 if (!bbio) {
4952 kfree(raid_map);
4674 ret = -ENOMEM; 4953 ret = -ENOMEM;
4675 goto out; 4954 goto out;
4676 } 4955 }
@@ -5246,9 +5525,7 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5246 5525
5247 if (map_length < length) { 5526 if (map_length < length) {
5248 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu", 5527 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5249 (unsigned long long)logical, 5528 logical, length, map_length);
5250 (unsigned long long)length,
5251 (unsigned long long)map_length);
5252 BUG(); 5529 BUG();
5253 } 5530 }
5254 5531
@@ -5314,23 +5591,72 @@ static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5314 struct btrfs_device *device; 5591 struct btrfs_device *device;
5315 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 5592 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5316 5593
5317 device = kzalloc(sizeof(*device), GFP_NOFS); 5594 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5318 if (!device) 5595 if (IS_ERR(device))
5319 return NULL; 5596 return NULL;
5320 list_add(&device->dev_list, 5597
5321 &fs_devices->devices); 5598 list_add(&device->dev_list, &fs_devices->devices);
5322 device->devid = devid;
5323 device->work.func = pending_bios_fn;
5324 device->fs_devices = fs_devices; 5599 device->fs_devices = fs_devices;
5325 device->missing = 1;
5326 fs_devices->num_devices++; 5600 fs_devices->num_devices++;
5601
5602 device->missing = 1;
5327 fs_devices->missing_devices++; 5603 fs_devices->missing_devices++;
5328 spin_lock_init(&device->io_lock); 5604
5329 INIT_LIST_HEAD(&device->dev_alloc_list);
5330 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5331 return device; 5605 return device;
5332} 5606}
5333 5607
5608/**
5609 * btrfs_alloc_device - allocate struct btrfs_device
5610 * @fs_info: used only for generating a new devid, can be NULL if
5611 * devid is provided (i.e. @devid != NULL).
5612 * @devid: a pointer to devid for this device. If NULL a new devid
5613 * is generated.
5614 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5615 * is generated.
5616 *
5617 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5618 * on error. Returned struct is not linked onto any lists and can be
5619 * destroyed with kfree() right away.
5620 */
5621struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5622 const u64 *devid,
5623 const u8 *uuid)
5624{
5625 struct btrfs_device *dev;
5626 u64 tmp;
5627
5628 if (!devid && !fs_info) {
5629 WARN_ON(1);
5630 return ERR_PTR(-EINVAL);
5631 }
5632
5633 dev = __alloc_device();
5634 if (IS_ERR(dev))
5635 return dev;
5636
5637 if (devid)
5638 tmp = *devid;
5639 else {
5640 int ret;
5641
5642 ret = find_next_devid(fs_info, &tmp);
5643 if (ret) {
5644 kfree(dev);
5645 return ERR_PTR(ret);
5646 }
5647 }
5648 dev->devid = tmp;
5649
5650 if (uuid)
5651 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5652 else
5653 generate_random_uuid(dev->uuid);
5654
5655 dev->work.func = pending_bios_fn;
5656
5657 return dev;
5658}
5659
5334static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, 5660static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5335 struct extent_buffer *leaf, 5661 struct extent_buffer *leaf,
5336 struct btrfs_chunk *chunk) 5662 struct btrfs_chunk *chunk)
@@ -5437,7 +5763,7 @@ static void fill_device_from_item(struct extent_buffer *leaf,
5437 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); 5763 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
5438 device->is_tgtdev_for_dev_replace = 0; 5764 device->is_tgtdev_for_dev_replace = 0;
5439 5765
5440 ptr = (unsigned long)btrfs_device_uuid(dev_item); 5766 ptr = btrfs_device_uuid(dev_item);
5441 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); 5767 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
5442} 5768}
5443 5769
@@ -5500,11 +5826,9 @@ static int read_one_dev(struct btrfs_root *root,
5500 u8 dev_uuid[BTRFS_UUID_SIZE]; 5826 u8 dev_uuid[BTRFS_UUID_SIZE];
5501 5827
5502 devid = btrfs_device_id(leaf, dev_item); 5828 devid = btrfs_device_id(leaf, dev_item);
5503 read_extent_buffer(leaf, dev_uuid, 5829 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
5504 (unsigned long)btrfs_device_uuid(dev_item),
5505 BTRFS_UUID_SIZE); 5830 BTRFS_UUID_SIZE);
5506 read_extent_buffer(leaf, fs_uuid, 5831 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
5507 (unsigned long)btrfs_device_fsid(dev_item),
5508 BTRFS_UUID_SIZE); 5832 BTRFS_UUID_SIZE);
5509 5833
5510 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { 5834 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
@@ -5519,8 +5843,7 @@ static int read_one_dev(struct btrfs_root *root,
5519 return -EIO; 5843 return -EIO;
5520 5844
5521 if (!device) { 5845 if (!device) {
5522 btrfs_warn(root->fs_info, "devid %llu missing", 5846 btrfs_warn(root->fs_info, "devid %llu missing", devid);
5523 (unsigned long long)devid);
5524 device = add_missing_dev(root, devid, dev_uuid); 5847 device = add_missing_dev(root, devid, dev_uuid);
5525 if (!device) 5848 if (!device)
5526 return -ENOMEM; 5849 return -ENOMEM;
@@ -5644,14 +5967,15 @@ int btrfs_read_chunk_tree(struct btrfs_root *root)
5644 mutex_lock(&uuid_mutex); 5967 mutex_lock(&uuid_mutex);
5645 lock_chunks(root); 5968 lock_chunks(root);
5646 5969
5647 /* first we search for all of the device items, and then we 5970 /*
5648 * read in all of the chunk items. This way we can create chunk 5971 * Read all device items, and then all the chunk items. All
5649 * mappings that reference all of the devices that are afound 5972 * device items are found before any chunk item (their object id
5973 * is smaller than the lowest possible object id for a chunk
5974 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
5650 */ 5975 */
5651 key.objectid = BTRFS_DEV_ITEMS_OBJECTID; 5976 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5652 key.offset = 0; 5977 key.offset = 0;
5653 key.type = 0; 5978 key.type = 0;
5654again:
5655 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5979 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5656 if (ret < 0) 5980 if (ret < 0)
5657 goto error; 5981 goto error;
@@ -5667,17 +5991,13 @@ again:
5667 break; 5991 break;
5668 } 5992 }
5669 btrfs_item_key_to_cpu(leaf, &found_key, slot); 5993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5670 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { 5994 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5671 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) 5995 struct btrfs_dev_item *dev_item;
5672 break; 5996 dev_item = btrfs_item_ptr(leaf, slot,
5673 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5674 struct btrfs_dev_item *dev_item;
5675 dev_item = btrfs_item_ptr(leaf, slot,
5676 struct btrfs_dev_item); 5997 struct btrfs_dev_item);
5677 ret = read_one_dev(root, leaf, dev_item); 5998 ret = read_one_dev(root, leaf, dev_item);
5678 if (ret) 5999 if (ret)
5679 goto error; 6000 goto error;
5680 }
5681 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { 6001 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5682 struct btrfs_chunk *chunk; 6002 struct btrfs_chunk *chunk;
5683 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); 6003 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
@@ -5687,11 +6007,6 @@ again:
5687 } 6007 }
5688 path->slots[0]++; 6008 path->slots[0]++;
5689 } 6009 }
5690 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5691 key.objectid = 0;
5692 btrfs_release_path(path);
5693 goto again;
5694 }
5695 ret = 0; 6010 ret = 0;
5696error: 6011error:
5697 unlock_chunks(root); 6012 unlock_chunks(root);