aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/volumes.c
diff options
context:
space:
mode:
authorIlya Dryomov <idryomov@gmail.com>2013-08-12 07:33:03 -0400
committerChris Mason <chris.mason@fusionio.com>2013-09-01 08:16:05 -0400
commit2208a378f35fea7a1b778bf856edb971fb7ea9e8 (patch)
treef99434dcc1f923d579290b4fb997b983707c93fd /fs/btrfs/volumes.c
parent12bd2fc0d2f589f9605b8f497eee2e7724f3af24 (diff)
Btrfs: add alloc_fs_devices and switch to it
In the spirit of btrfs_alloc_device, add a helper for allocating and doing some common initialization of btrfs_fs_devices struct. Signed-off-by: Ilya Dryomov <idryomov@gmail.com> Signed-off-by: Josef Bacik <jbacik@fusionio.com> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
Diffstat (limited to 'fs/btrfs/volumes.c')
-rw-r--r--fs/btrfs/volumes.c71
1 files changed, 53 insertions, 18 deletions
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 4bc07d910e54..d38065eca7c9 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -63,6 +63,48 @@ static void unlock_chunks(struct btrfs_root *root)
63 mutex_unlock(&root->fs_info->chunk_mutex); 63 mutex_unlock(&root->fs_info->chunk_mutex);
64} 64}
65 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
66static void free_fs_devices(struct btrfs_fs_devices *fs_devices) 108static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
67{ 109{
68 struct btrfs_device *device; 110 struct btrfs_device *device;
@@ -417,16 +459,14 @@ static noinline int device_list_add(const char *path,
417 459
418 fs_devices = find_fsid(disk_super->fsid); 460 fs_devices = find_fsid(disk_super->fsid);
419 if (!fs_devices) { 461 if (!fs_devices) {
420 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); 462 fs_devices = alloc_fs_devices(disk_super->fsid);
421 if (!fs_devices) 463 if (IS_ERR(fs_devices))
422 return -ENOMEM; 464 return PTR_ERR(fs_devices);
423 INIT_LIST_HEAD(&fs_devices->devices); 465
424 INIT_LIST_HEAD(&fs_devices->alloc_list);
425 list_add(&fs_devices->list, &fs_uuids); 466 list_add(&fs_devices->list, &fs_uuids);
426 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
427 fs_devices->latest_devid = devid; 467 fs_devices->latest_devid = devid;
428 fs_devices->latest_trans = found_transid; 468 fs_devices->latest_trans = found_transid;
429 mutex_init(&fs_devices->device_list_mutex); 469
430 device = NULL; 470 device = NULL;
431 } else { 471 } else {
432 device = __find_device(&fs_devices->devices, devid, 472 device = __find_device(&fs_devices->devices, devid,
@@ -482,18 +522,13 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
482 struct btrfs_device *device; 522 struct btrfs_device *device;
483 struct btrfs_device *orig_dev; 523 struct btrfs_device *orig_dev;
484 524
485 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); 525 fs_devices = alloc_fs_devices(orig->fsid);
486 if (!fs_devices) 526 if (IS_ERR(fs_devices))
487 return ERR_PTR(-ENOMEM); 527 return fs_devices;
488 528
489 INIT_LIST_HEAD(&fs_devices->devices);
490 INIT_LIST_HEAD(&fs_devices->alloc_list);
491 INIT_LIST_HEAD(&fs_devices->list);
492 mutex_init(&fs_devices->device_list_mutex);
493 fs_devices->latest_devid = orig->latest_devid; 529 fs_devices->latest_devid = orig->latest_devid;
494 fs_devices->latest_trans = orig->latest_trans; 530 fs_devices->latest_trans = orig->latest_trans;
495 fs_devices->total_devices = orig->total_devices; 531 fs_devices->total_devices = orig->total_devices;
496 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
497 532
498 /* 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. */
499 list_for_each_entry(orig_dev, &orig->devices, dev_list) { 534 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
@@ -1797,9 +1832,9 @@ static int btrfs_prepare_sprout(struct btrfs_root *root)
1797 if (!fs_devices->seeding) 1832 if (!fs_devices->seeding)
1798 return -EINVAL; 1833 return -EINVAL;
1799 1834
1800 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); 1835 seed_devices = __alloc_fs_devices();
1801 if (!seed_devices) 1836 if (IS_ERR(seed_devices))
1802 return -ENOMEM; 1837 return PTR_ERR(seed_devices);
1803 1838
1804 old_devices = clone_fs_devices(fs_devices); 1839 old_devices = clone_fs_devices(fs_devices);
1805 if (IS_ERR(old_devices)) { 1840 if (IS_ERR(old_devices)) {