Skip to content

Commit 3d17609

Browse files
committed
btrfs-progs: list-chunks: add sorting by length
Signed-off-by: David Sterba <[email protected]>
1 parent 73c5ad0 commit 3d17609

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

cmds/inspect.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,14 @@ static const char * const cmd_inspect_list_chunks_usage[] = {
699699
"Show chunks (block groups) layout for all devices",
700700
"",
701701
HELPINFO_UNITS_LONG,
702-
OPTLINE("--sort MODE", "sort by a column ascending: pstart, lstart, usage (default: pstart)"),
702+
OPTLINE("--sort MODE", "sort by a column ascending (default: pstart), "
703+
"MODE can be one of: "
704+
"pstart - physical offset, grouped by device, "
705+
"lstart - logical offset, "
706+
"usage - by chunk usage (implies --usage), "
707+
"length_p - by chunk length, secondary by physical offset, "
708+
"length_l - by chunk length, secondary by logical offset"
709+
),
703710
OPTLINE("--usage", "show usage per block group (note: this can be slow)"),
704711
OPTLINE("--no-usage", "don't show usage per block group"),
705712
OPTLINE("--empty", "show empty space between block groups"),
@@ -711,6 +718,10 @@ enum {
711718
CHUNK_SORT_PSTART,
712719
CHUNK_SORT_LSTART,
713720
CHUNK_SORT_USAGE,
721+
/* Length, secondary physical start */
722+
CHUNK_SORT_LENGTH_P,
723+
/* Length, secondary logical start */
724+
CHUNK_SORT_LENGTH_L,
714725
CHUNK_SORT_DEFAULT = CHUNK_SORT_PSTART
715726
};
716727

@@ -789,6 +800,40 @@ static int cmp_cse_devid_usage(const void *va, const void *vb)
789800
return 0;
790801
}
791802

803+
static int cmp_cse_length_physical(const void *va, const void *vb)
804+
{
805+
const struct list_chunks_entry *a = va;
806+
const struct list_chunks_entry *b = vb;
807+
808+
if (a->length < b->length)
809+
return -1;
810+
if (a->length > b->length)
811+
return 1;
812+
813+
if (a->start < b->start)
814+
return -1;
815+
if (a->start > b->start)
816+
return 1;
817+
return 0;
818+
}
819+
820+
static int cmp_cse_length_logical(const void *va, const void *vb)
821+
{
822+
const struct list_chunks_entry *a = va;
823+
const struct list_chunks_entry *b = vb;
824+
825+
if (a->length < b->length)
826+
return -1;
827+
if (a->length > b->length)
828+
return 1;
829+
830+
if (a->lstart < b->lstart)
831+
return -1;
832+
if (a->lstart > b->lstart)
833+
return 1;
834+
return 0;
835+
}
836+
792837
static int print_list_chunks(struct list_chunks_ctx *ctx, unsigned sort_mode,
793838
unsigned unit_mode, bool with_usage, bool with_empty)
794839
{
@@ -830,6 +875,12 @@ static int print_list_chunks(struct list_chunks_ctx *ctx, unsigned sort_mode,
830875
else if (sort_mode == CHUNK_SORT_USAGE)
831876
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]),
832877
cmp_cse_devid_usage);
878+
else if (sort_mode == CHUNK_SORT_LENGTH_P)
879+
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]),
880+
cmp_cse_length_physical);
881+
else if (sort_mode == CHUNK_SORT_LENGTH_L)
882+
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]),
883+
cmp_cse_length_logical);
833884

834885
/* Optional usage, two rows for header and separator, gaps */
835886
table = table_create(7 + (int)with_usage, 2 + ctx->length + gaps);
@@ -992,6 +1043,10 @@ static int cmd_inspect_list_chunks(const struct cmd_struct *cmd,
9921043
} else if (strcmp(optarg, "usage") == 0) {
9931044
sort_mode = CHUNK_SORT_USAGE;
9941045
with_usage = true;
1046+
} else if (strcmp(optarg, "length_p") == 0) {
1047+
sort_mode = CHUNK_SORT_LENGTH_P;
1048+
} else if (strcmp(optarg, "length_l") == 0) {
1049+
sort_mode = CHUNK_SORT_LENGTH_L;
9951050
} else {
9961051
error("unknown sort mode: %s", optarg);
9971052
exit(1);

0 commit comments

Comments
 (0)