diff mbox

[1/2] btrfs-progs: mkfs: output device list in sorted order

Message ID 0e4f0ae269fdaf041e1f53d2460e92ac3f926b88.1446548572.git.zhaolei@cn.fujitsu.com (mailing list archive)
State Accepted
Headers show

Commit Message

Zhaolei Nov. 3, 2015, 11:03 a.m. UTC
list_for_each_entry_reverse() in current code can not output
devices in sorted order, because the sequence are broken in
btrfs_alloc_chunk().

We can use list_sort() instead.

Before patch:
 # mkfs.btrfs -f /dev/vdd /dev/vde /dev/vdf
 ...
 Number of devices:  3
 Devices:
    ID        SIZE  PATH
     3     2.60GiB  /dev/vdf
     1     2.60GiB  /dev/vdd
     2     2.60GiB  /dev/vde

After patch:
 # mkfs.btrfs -f /dev/vdd /dev/vde /dev/vdf
 ...
 Number of devices:  3
 Devices:
    ID        SIZE  PATH
     1     2.60GiB  /dev/vdd
     2     2.60GiB  /dev/vde
     3     2.60GiB  /dev/vdf

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 mkfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Comments

David Sterba Nov. 3, 2015, noon UTC | #1
On Tue, Nov 03, 2015 at 07:03:00PM +0800, Zhao Lei wrote:
> list_for_each_entry_reverse() in current code can not output
> devices in sorted order, because the sequence are broken in
> btrfs_alloc_chunk().
> 
> We can use list_sort() instead.
> 
> Before patch:
>  # mkfs.btrfs -f /dev/vdd /dev/vde /dev/vdf
>  ...
>  Number of devices:  3
>  Devices:
>     ID        SIZE  PATH
>      3     2.60GiB  /dev/vdf
>      1     2.60GiB  /dev/vdd
>      2     2.60GiB  /dev/vde
> 
> After patch:
>  # mkfs.btrfs -f /dev/vdd /dev/vde /dev/vdf
>  ...
>  Number of devices:  3
>  Devices:
>     ID        SIZE  PATH
>      1     2.60GiB  /dev/vdd
>      2     2.60GiB  /dev/vde
>      3     2.60GiB  /dev/vdf
> 
> Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>

Nice, thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/mkfs.c b/mkfs.c
index 14e7eb4..75fc086 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -42,6 +42,7 @@ 
 #include "volumes.h"
 #include "transaction.h"
 #include "utils.h"
+#include "list_sort.h"
 
 static u64 index_cnt = 2;
 static int verbose = 1;
@@ -1148,6 +1149,13 @@  static int is_ssd(const char *file)
 	return !atoi((const char *)&rotational);
 }
 
+static int _cmp_device_by_id(void *priv, struct list_head *a,
+			     struct list_head *b)
+{
+	return list_entry(a, struct btrfs_device, dev_list)->devid -
+	       list_entry(b, struct btrfs_device, dev_list)->devid;
+}
+
 static void list_all_devices(struct btrfs_root *root)
 {
 	struct btrfs_fs_devices *fs_devices;
@@ -1160,12 +1168,14 @@  static void list_all_devices(struct btrfs_root *root)
 	list_for_each_entry(device, &fs_devices->devices, dev_list)
 		number_of_devices++;
 
+	list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
+
 	printf("Number of devices:  %d\n", number_of_devices);
 	/* printf("Total devices size: %10s\n", */
 		/* pretty_size(total_block_count)); */
 	printf("Devices:\n");
 	printf("   ID        SIZE  PATH\n");
-	list_for_each_entry_reverse(device, &fs_devices->devices, dev_list) {
+	list_for_each_entry(device, &fs_devices->devices, dev_list) {
 		char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
 
 		uuid_unparse(device->uuid, dev_uuid);