public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format
@ 2025-02-14 16:47 Racz Zoltan
  2025-02-14 16:47 ` [PATCH 1/6] btrfs-progs: Added rowspec struct and neccesary include file for " Racz Zoltan
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

This patchset is a follow-up to the json output implementation sent
earlier this week. It uses the new fmt_print format types (duration
and date-time).

The patchset also relies on the following patches:
[PATCH] btrfs-progs: add duration format to fmt_print
[PATCH] btrfs-progs: change print format for btrfs_scrub_progress struct keys in print_scrub_full()
[PATCH] btrfs-progs: Simplified unit_mode check in print_scrub_summary

Example usages:

1. ./btrfs --format json scrub status /
Result:
{
  "__header": {
    "version": "1"
  },
  "scrub-status": {
    "uuid": "1a7d1bc4-c212-42bf-b05c-73bd313d3ecd",
    "info": {
      "started-at": "2025-02-11 00:27:01 +0200",
      "status": "finished",
      "duration": "00:00:15"
    },
    "scrub": {
      "total-bytes-to-scrub": 84444946432,
      "rate": 4726353100
    }
  }
}

2. ./btrfs --format json scrub status / -R
Result:
{
  "__header": {
    "version": "1"
  },
  "scrub-status": {
    "uuid": "1a7d1bc4-c212-42bf-b05c-73bd313d3ecd",
    "info": {
      "started-at": "2025-02-11 00:27:01 +0200",
      "status": "finished",
      "duration": "00:00:15"
    },
    "scrub": {
      "data-extents-scrubbed": 1507393,
      "tree-extents-scrubbed": 108172,
      "data-bytes-scrubbed": 69123006464,
      "tree-bytes-scrubbed": 1772290048,
      "read-errors": 0,
      "csum-errors": 0,
      "verify-errors": 0,
      "no-csum": 107693,
      "csum-discards": 0,
      "super-errors": 0,
      "malloc-errors": 0,
      "uncorrectable-errors": 0,
      "unverified-errors": 0,
      "corrected-errors": 0,
      "last-physical": 159691177984
    }
  }
}

3. ./btrfs --format json scrub status / -d
Result:
{
  "__header": {
    "version": "1"
  },
  "scrub-status": {
    "uuid": "1a7d1bc4-c212-42bf-b05c-73bd313d3ecd",
    "devices": [
      {
        "device": {
          "dev": "/dev/nvme0n1p3",
          "id": 1,
          "info": {
            "started-at": "2025-02-11 00:27:01 +0200",
            "status": "finished",
            "duration": "00:00:15"
          },
          "scrub": {
            "total-bytes-to-scrub": 70895296512,
            "rate": 4726353100
          }
        }
      }
    ]
  }
}


Racz Zoltan (6):
  Added rowspec struct and neccesary include file for json output format
  Added json output format for print_scrub_full
  Added json output format for print_scrub_summary
  Added json output format for _print_scrub_ss and print_scrub_dev
  Added json output format for cmd_scrub_status
  Corrected a minor JSON string error in print_scrub_dev

 cmds/scrub.c | 307 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 235 insertions(+), 72 deletions(-)

-- 
2.48.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/6] btrfs-progs: Added rowspec struct and neccesary include file for json output format
  2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
@ 2025-02-14 16:47 ` Racz Zoltan
  2025-02-14 16:47 ` [PATCH 2/6] btrfs-progs: Added json output format for print_scrub_full Racz Zoltan
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

---
 cmds/scrub.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index f769188a..f049e1ed 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -53,6 +53,7 @@
 #include "common/sysfs-utils.h"
 #include "common/string-table.h"
 #include "common/string-utils.h"
+#include "common/format-output.h"
 #include "common/help.h"
 #include "cmds/commands.h"
 
@@ -124,6 +125,41 @@ struct scrub_fs_stat {
 	int i;
 };
 
+struct format_ctx fctx;
+
+static const struct rowspec scrub_status_rowspec[] = {
+	{ .key = "uuid", .fmt = "%s", .out_json = "uuid"},
+	{ .key = "dev", .fmt = "%s", .out_json = "dev"},
+	{ .key = "id", .fmt = "%llu", .out_json = "id"},
+	{ .key = "status", .fmt = "%s", .out_json = "status"},
+	{ .key = "duration", .fmt = "duration", .out_json = "duration"},
+	{ .key = "started-at", .fmt = "date-time", .out_json = "started-at"},
+	{ .key = "resumed-at", .fmt = "date-time", .out_json = "resumed-at"},
+	{ .key = "data-extents-scrubbed", .fmt = "%llu", .out_json = "data-extents-scrubbed"},
+	{ .key = "tree-extents-scrubbed", .fmt = "%llu", .out_json = "tree-extents-scrubbed"},
+	{ .key = "data-bytes-scrubbed", .fmt = "%llu", .out_json = "data-bytes-scrubbed"},
+	{ .key = "tree-bytes-scrubbed", .fmt = "%llu", .out_json = "tree-bytes-scrubbed"},
+	{ .key = "read-errors", .fmt = "%llu", .out_json = "read-errors"},
+	{ .key = "csum-errors", .fmt = "%llu", .out_json = "csum-errors"},
+	{ .key = "verify-errors", .fmt = "%llu", .out_json = "verify-errors"},
+	{ .key = "no-csum", .fmt = "%llu", .out_json = "no-csum"},
+	{ .key = "csum-discards", .fmt = "%llu", .out_json = "csum-discards"},
+	{ .key = "super-errors", .fmt = "%llu", .out_json = "super-errors"},
+	{ .key = "malloc-errors", .fmt = "%llu", .out_json = "malloc-errors"},
+	{ .key = "uncorrectable-errors", .fmt = "%llu", .out_json = "uncorrectable-errors"},
+	{ .key = "unverified-errors", .fmt = "%llu", .out_json = "unverified-errors"},
+	{ .key = "corrected-errors", .fmt = "%llu", .out_json = "corrected-errors"},
+	{ .key = "last-physical", .fmt = "%llu", .out_json = "last-physical"},
+	{ .key = "time-left", .fmt = "duration", .out_json = "time-left"},
+	{ .key = "eta", .fmt = "date-time", .out_json = "eta"},
+	{ .key = "total-bytes-to-scrub", .fmt = "%llu", .out_json = "total-bytes-to-scrub"},
+	{ .key = "bytes-scrubbed", .fmt = "%llu", .out_json = "bytes-scrubbed"},
+	{ .key = "rate", .fmt = "%llu", .out_json = "rate"},
+	{ .key = "limit", .fmt = "%llu", .out_json = "limit"},
+
+	ROWSPEC_END
+};
+
 static void print_scrub_full(struct btrfs_scrub_progress *sp)
 {
 	pr_verbose(LOG_DEFAULT, "\tdata_extents_scrubbed: %llu\n", sp->data_extents_scrubbed);
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/6] btrfs-progs: Added json output format for print_scrub_full
  2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
  2025-02-14 16:47 ` [PATCH 1/6] btrfs-progs: Added rowspec struct and neccesary include file for " Racz Zoltan
@ 2025-02-14 16:47 ` Racz Zoltan
  2025-02-14 16:47 ` [PATCH 3/6] btrfs-progs: Added json output format for print_scrub_summary Racz Zoltan
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

---
 cmds/scrub.c | 52 +++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index f049e1ed..caada704 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -162,21 +162,43 @@ static const struct rowspec scrub_status_rowspec[] = {
 
 static void print_scrub_full(struct btrfs_scrub_progress *sp)
 {
-	pr_verbose(LOG_DEFAULT, "\tdata_extents_scrubbed: %llu\n", sp->data_extents_scrubbed);
-	pr_verbose(LOG_DEFAULT, "\ttree_extents_scrubbed: %llu\n", sp->tree_extents_scrubbed);
-	pr_verbose(LOG_DEFAULT, "\tdata_bytes_scrubbed: %llu\n", sp->data_bytes_scrubbed);
-	pr_verbose(LOG_DEFAULT, "\ttree_bytes_scrubbed: %llu\n", sp->tree_bytes_scrubbed);
-	pr_verbose(LOG_DEFAULT, "\tread_errors: %llu\n", sp->read_errors);
-	pr_verbose(LOG_DEFAULT, "\tcsum_errors: %llu\n", sp->csum_errors);
-	pr_verbose(LOG_DEFAULT, "\tverify_errors: %llu\n", sp->verify_errors);
-	pr_verbose(LOG_DEFAULT, "\tno_csum: %llu\n", sp->no_csum);
-	pr_verbose(LOG_DEFAULT, "\tcsum_discards: %llu\n", sp->csum_discards);
-	pr_verbose(LOG_DEFAULT, "\tsuper_errors: %llu\n", sp->super_errors);
-	pr_verbose(LOG_DEFAULT, "\tmalloc_errors: %llu\n", sp->malloc_errors);
-	pr_verbose(LOG_DEFAULT, "\tuncorrectable_errors: %llu\n", sp->uncorrectable_errors);
-	pr_verbose(LOG_DEFAULT, "\tunverified_errors: %llu\n", sp->unverified_errors);
-	pr_verbose(LOG_DEFAULT, "\tcorrected_errors: %llu\n", sp->corrected_errors);
-	pr_verbose(LOG_DEFAULT, "\tlast_physical: %llu\n", sp->last_physical);
+	if (bconf.output_format == CMD_FORMAT_JSON) {
+		fmt_print_start_group(&fctx, "scrub", JSON_TYPE_MAP);
+
+		fmt_print(&fctx, "data-extents-scrubbed", sp->data_extents_scrubbed);
+		fmt_print(&fctx, "tree-extents-scrubbed", sp->tree_extents_scrubbed);
+		fmt_print(&fctx, "data-bytes-scrubbed", sp->data_bytes_scrubbed);
+		fmt_print(&fctx, "tree-bytes-scrubbed", sp->tree_bytes_scrubbed);
+		fmt_print(&fctx, "read-errors", sp->read_errors);
+		fmt_print(&fctx, "csum-errors", sp->csum_errors);
+		fmt_print(&fctx, "verify-errors", sp->verify_errors);
+		fmt_print(&fctx, "no-csum", sp->no_csum);
+		fmt_print(&fctx, "csum-discards", sp->csum_discards);
+		fmt_print(&fctx, "super-errors", sp->super_errors);
+		fmt_print(&fctx, "malloc-errors", sp->malloc_errors);
+		fmt_print(&fctx, "uncorrectable-errors", sp->uncorrectable_errors);
+		fmt_print(&fctx, "unverified-errors", sp->unverified_errors);
+		fmt_print(&fctx, "corrected-errors", sp->corrected_errors);
+		fmt_print(&fctx, "last-physical", sp->last_physical);
+
+		fmt_print_end_group(&fctx, "scrub");
+	} else {
+		pr_verbose(LOG_DEFAULT, "\tdata_extents_scrubbed: %llu\n", sp->data_extents_scrubbed);
+		pr_verbose(LOG_DEFAULT, "\ttree_extents_scrubbed: %llu\n", sp->tree_extents_scrubbed);
+		pr_verbose(LOG_DEFAULT, "\tdata_bytes_scrubbed: %llu\n", sp->data_bytes_scrubbed);
+		pr_verbose(LOG_DEFAULT, "\ttree_bytes_scrubbed: %llu\n", sp->tree_bytes_scrubbed);
+		pr_verbose(LOG_DEFAULT, "\tread_errors: %llu\n", sp->read_errors);
+		pr_verbose(LOG_DEFAULT, "\tcsum_errors: %llu\n", sp->csum_errors);
+		pr_verbose(LOG_DEFAULT, "\tverify_errors: %llu\n", sp->verify_errors);
+		pr_verbose(LOG_DEFAULT, "\tno_csum: %llu\n", sp->no_csum);
+		pr_verbose(LOG_DEFAULT, "\tcsum_discards: %llu\n", sp->csum_discards);
+		pr_verbose(LOG_DEFAULT, "\tsuper_errors: %llu\n", sp->super_errors);
+		pr_verbose(LOG_DEFAULT, "\tmalloc_errors: %llu\n", sp->malloc_errors);
+		pr_verbose(LOG_DEFAULT, "\tuncorrectable_errors: %llu\n", sp->uncorrectable_errors);
+		pr_verbose(LOG_DEFAULT, "\tunverified_errors: %llu\n", sp->unverified_errors);
+		pr_verbose(LOG_DEFAULT, "\tcorrected_errors: %llu\n", sp->corrected_errors);
+		pr_verbose(LOG_DEFAULT, "\tlast_physical: %llu\n", sp->last_physical);
+	}
 }
 
 #define PRINT_SCRUB_ERROR(test, desc) do {	\
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/6] btrfs-progs: Added json output format for print_scrub_summary
  2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
  2025-02-14 16:47 ` [PATCH 1/6] btrfs-progs: Added rowspec struct and neccesary include file for " Racz Zoltan
  2025-02-14 16:47 ` [PATCH 2/6] btrfs-progs: Added json output format for print_scrub_full Racz Zoltan
@ 2025-02-14 16:47 ` Racz Zoltan
  2025-02-14 16:47 ` [PATCH 4/6] btrfs-progs: Added json output format for _print_scrub_ss and print_scrub_dev Racz Zoltan
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

---
 cmds/scrub.c | 110 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 76 insertions(+), 34 deletions(-)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index caada704..5c05d00f 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -216,6 +216,8 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 	u64 sec_left = 0;
 	time_t sec_eta;
 
+	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
+
 	bytes_scrubbed = p->data_bytes_scrubbed + p->tree_bytes_scrubbed;
 	/*
 	 * If duration is zero seconds (rounded down), then the Rate metric
@@ -236,8 +238,12 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 
 	err_cnt2 = p->corrected_errors + p->uncorrectable_errors;
 
+	if (json_output) 
+		fmt_print_start_group(&fctx, "scrub", JSON_TYPE_MAP);
+
 	if (p->malloc_errors)
-		pr_verbose(LOG_DEFAULT, "*** WARNING: memory allocation failed while scrubbing. "
+		if (!json_output)
+			pr_verbose(LOG_DEFAULT, "*** WARNING: memory allocation failed while scrubbing. "
 		       "results may be inaccurate\n");
 
 	if (s->in_progress) {
@@ -246,21 +252,33 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 
 		sec_eta = time(NULL);
 		sec_eta += sec_left;
-		localtime_r(&sec_eta, &tm);
-		t[sizeof(t) - 1] = '\0';
-		strftime(t, sizeof(t), "%c", &tm);
 
-		pr_verbose(LOG_DEFAULT, "Time left:        %llu:%02llu:%02llu\n",
-			sec_left / 3600, (sec_left / 60) % 60, sec_left % 60);
-		pr_verbose(LOG_DEFAULT, "ETA:              %s\n", t);
-		pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
-			pretty_size_mode(bytes_total, unit_mode));
-		pr_verbose(LOG_DEFAULT, "Bytes scrubbed:   %s  (%.2f%%)\n",
-			pretty_size_mode(bytes_scrubbed, unit_mode),
-			100.0 * bytes_scrubbed / bytes_total);
+		if (json_output) {
+			fmt_print(&fctx, "time-left", sec_left);
+			fmt_print(&fctx, "eta", sec_eta);
+			fmt_print(&fctx, "total-bytes-to-scrub", bytes_total);
+			fmt_print(&fctx, "bytes-scrubbed", bytes_scrubbed);
+		}
+		else {
+			localtime_r(&sec_eta, &tm);
+			t[sizeof(t) - 1] = '\0';
+			strftime(t, sizeof(t), "%c", &tm);
+
+			pr_verbose(LOG_DEFAULT, "Time left:        %llu:%02llu:%02llu\n",
+				sec_left / 3600, (sec_left / 60) % 60, sec_left % 60);
+			pr_verbose(LOG_DEFAULT, "ETA:              %s\n", t);
+			pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
+				pretty_size_mode(bytes_total, unit_mode));
+			pr_verbose(LOG_DEFAULT, "Bytes scrubbed:   %s  (%.2f%%)\n",
+				pretty_size_mode(bytes_scrubbed, unit_mode),
+				100.0 * bytes_scrubbed / bytes_total);
+		}
 	} else {
-		pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
-			pretty_size_mode(bytes_total, unit_mode));
+		if (json_output) 
+			fmt_print(&fctx, "total-bytes-to-scrub", bytes_total);
+		else
+			pr_verbose(LOG_DEFAULT, "Total to scrub:   %s\n",
+				pretty_size_mode(bytes_total, unit_mode));
 	}
 
 	/*
@@ -271,28 +289,52 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p, struct scrub_sta
 	if (unit_mode != UNITS_RAW) 
 		mode = unit_mode & UNITS_BINARY ? UNITS_HUMAN_BINARY : UNITS_HUMAN_DECIMAL;
 
-	pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
-		pretty_size_mode(bytes_per_sec, mode));
-	if (limit > 1)
-		pr_verbose(LOG_DEFAULT, " (limit %s/s)",
-				pretty_size_mode(limit, mode));
-	else if (limit == 1)
-		pr_verbose(LOG_DEFAULT, " (some device limits set)");
-	pr_verbose(LOG_DEFAULT, "\n");
-
-	pr_verbose(LOG_DEFAULT, "Error summary:   ");
-	if (err_cnt || err_cnt2) {
-		PRINT_SCRUB_ERROR(p->read_errors, "read");
-		PRINT_SCRUB_ERROR(p->super_errors, "super");
-		PRINT_SCRUB_ERROR(p->verify_errors, "verify");
-		PRINT_SCRUB_ERROR(p->csum_errors, "csum");
-		pr_verbose(LOG_DEFAULT, "\n");
-		pr_verbose(LOG_DEFAULT, "  Corrected:      %llu\n", p->corrected_errors);
-		pr_verbose(LOG_DEFAULT, "  Uncorrectable:  %llu\n", p->uncorrectable_errors);
-		pr_verbose(LOG_DEFAULT, "  Unverified:     %llu\n", p->unverified_errors);
+	if (json_output) {
+		fmt_print(&fctx, "rate", bytes_per_sec);
+		if (limit > 1)
+			fmt_print(&fctx, "limit", limit);
 	} else {
-		pr_verbose(LOG_DEFAULT, " no errors found\n");
+		pr_verbose(LOG_DEFAULT, "Rate:             %s/s",
+			pretty_size_mode(bytes_per_sec, mode));
+		if (limit > 1)
+			pr_verbose(LOG_DEFAULT, " (limit %s/s)",
+					pretty_size_mode(limit, mode));
+		else if (limit == 1)
+			pr_verbose(LOG_DEFAULT, " (some device limits set)");
+		pr_verbose(LOG_DEFAULT, "\n");
 	}
+
+	if (json_output) {
+		if (err_cnt || err_cnt2) {
+			fmt_print_start_group(&fctx, "error-summary", JSON_TYPE_MAP);
+			fmt_print(&fctx, "read-errors", p->read_errors);
+			fmt_print(&fctx, "super-errors", p->super_errors);
+			fmt_print(&fctx, "verify-errors", p->verify_errors);
+			fmt_print(&fctx, "csum-errors", p->csum_errors);
+			fmt_print(&fctx, "corrected-errors", p->corrected_errors);
+			fmt_print(&fctx, "uncorrectable-errors", p->uncorrectable_errors);
+			fmt_print(&fctx, "unverified-errors", p->unverified_errors);
+			fmt_print_end_group(&fctx, "error-summary");
+		}
+	}
+	else {
+		pr_verbose(LOG_DEFAULT, "Error summary:   ");
+		if (err_cnt || err_cnt2) {
+			PRINT_SCRUB_ERROR(p->read_errors, "read");
+			PRINT_SCRUB_ERROR(p->super_errors, "super");
+			PRINT_SCRUB_ERROR(p->verify_errors, "verify");
+			PRINT_SCRUB_ERROR(p->csum_errors, "csum");
+			pr_verbose(LOG_DEFAULT, "\n");
+			pr_verbose(LOG_DEFAULT, "  Corrected:      %llu\n", p->corrected_errors);
+			pr_verbose(LOG_DEFAULT, "  Uncorrectable:  %llu\n", p->uncorrectable_errors);
+			pr_verbose(LOG_DEFAULT, "  Unverified:     %llu\n", p->unverified_errors);
+		} else {
+			pr_verbose(LOG_DEFAULT, " no errors found\n");
+		}
+	}
+
+	if (json_output)
+		fmt_print_end_group(&fctx, "scrub");
 }
 
 #define _SCRUB_FS_STAT(p, name, fs_stat) do {	\
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/6] btrfs-progs: Added json output format for _print_scrub_ss and print_scrub_dev
  2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
                   ` (2 preceding siblings ...)
  2025-02-14 16:47 ` [PATCH 3/6] btrfs-progs: Added json output format for print_scrub_summary Racz Zoltan
@ 2025-02-14 16:47 ` Racz Zoltan
  2025-02-14 16:47 ` [PATCH 5/6] btrfs-progs: Added json output format for cmd_scrub_status Racz Zoltan
  2025-02-14 16:47 ` [PATCH 6/6] btrfs-progs: Corrected a minor JSON string error in print_scrub_dev Racz Zoltan
  5 siblings, 0 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

---
 cmds/scrub.c | 79 +++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 59 insertions(+), 20 deletions(-)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index 5c05d00f..1f3f032f 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -403,32 +403,61 @@ static void _print_scrub_ss(struct scrub_stats *ss)
 	struct tm tm;
 	time_t seconds;
 	unsigned hours;
+	char *status;
+
+	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
 
 	if (!ss || !ss->t_start) {
-		pr_verbose(LOG_DEFAULT, "\tno stats available\n");
+		if (!json_output)
+			pr_verbose(LOG_DEFAULT, "\tno stats available\n");
+		
 		return;
 	}
+
+	if (json_output)
+		fmt_print_start_group(&fctx, "info", JSON_TYPE_MAP);
+
 	if (ss->t_resumed) {
-		localtime_r(&ss->t_resumed, &tm);
-		strftime(t, sizeof(t), "%c", &tm);
-		t[sizeof(t) - 1] = '\0';
-		pr_verbose(LOG_DEFAULT, "Scrub resumed:    %s\n", t);
+		if (json_output)
+			fmt_print(&fctx, "resumed-at", ss->t_resumed);
+		else {
+			localtime_r(&ss->t_resumed, &tm);
+			strftime(t, sizeof(t), "%c", &tm);
+			t[sizeof(t) - 1] = '\0';
+
+			pr_verbose(LOG_DEFAULT, "Scrub resumed:    %s\n", t);
+		}
 	} else {
-		localtime_r(&ss->t_start, &tm);
-		strftime(t, sizeof(t), "%c", &tm);
-		t[sizeof(t) - 1] = '\0';
-		pr_verbose(LOG_DEFAULT, "Scrub started:    %s\n", t);
-	}
-
-	seconds = ss->duration;
-	hours = ss->duration / (60 * 60);
-	gmtime_r(&seconds, &tm);
-	strftime(t, sizeof(t), "%M:%S", &tm);
-	pr_verbose(LOG_DEFAULT, "Status:           %s\n",
-			(ss->in_progress ? "running" :
+		if (json_output)
+			fmt_print(&fctx, "started-at", ss->t_start);
+		else {
+			localtime_r(&ss->t_start, &tm);
+			strftime(t, sizeof(t), "%c", &tm);
+			t[sizeof(t) - 1] = '\0';
+
+			pr_verbose(LOG_DEFAULT, "Scrub started:    %s\n", t);
+		}
+	}
+
+	status = (ss->in_progress ? "running" :
 			 (ss->canceled ? "aborted" :
-			  (ss->finished ? "finished" : "interrupted"))));
-	pr_verbose(LOG_DEFAULT, "Duration:         %u:%s\n", hours, t);
+			  (ss->finished ? "finished" : "interrupted")));
+	
+
+	if (json_output) {
+		fmt_print(&fctx, "status", status);
+		fmt_print(&fctx, "duration", ss->duration);
+		fmt_print_end_group(&fctx, "info");
+	} else {
+		seconds = ss->duration;
+		hours = ss->duration / (60 * 60);
+		gmtime_r(&seconds, &tm);
+		strftime(t, sizeof(t), "%M:%S", &tm);
+		
+		pr_verbose(LOG_DEFAULT, "Status:           %s\n", status);
+		pr_verbose(LOG_DEFAULT, "Duration:         %u:%s\n", hours, t);
+	}
+
 }
 
 static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
@@ -436,7 +465,14 @@ static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 				const char *append, struct scrub_stats *ss,
 				u64 limit)
 {
-	pr_verbose(LOG_DEFAULT, "\nScrub device %s (id %llu) %s\n", di->path, di->devid,
+	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
+
+	if (json_output) {
+		fmt_print_start_group(&fctx, "device", JSON_TYPE_MAP);
+		fmt_print(&fctx, "dev", di->path);
+		fmt_print(&fctx, "id", di->devid);
+	} else 	
+		pr_verbose(LOG_DEFAULT, "\nScrub device %s (id %llu) %s\n", di->path, di->devid,
 	       append ? append : "");
 
 	_print_scrub_ss(ss);
@@ -461,6 +497,9 @@ static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 			print_scrub_summary(p, ss, di->bytes_used, limit);
 		}
 	}
+
+	if (json_output) 
+		fmt_print_end_group(&fctx, "device");
 }
 
 /*
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 5/6] btrfs-progs: Added json output format for cmd_scrub_status
  2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
                   ` (3 preceding siblings ...)
  2025-02-14 16:47 ` [PATCH 4/6] btrfs-progs: Added json output format for _print_scrub_ss and print_scrub_dev Racz Zoltan
@ 2025-02-14 16:47 ` Racz Zoltan
  2025-02-14 16:47 ` [PATCH 6/6] btrfs-progs: Corrected a minor JSON string error in print_scrub_dev Racz Zoltan
  5 siblings, 0 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

---
 cmds/scrub.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index 1f3f032f..e47c1869 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -1991,6 +1991,8 @@ static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)
 	int fdres = -1;
 	int err = 0;
 
+	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
+
 	unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
 
 	optind = 0;
@@ -2069,9 +2071,18 @@ static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)
 	}
 	in_progress = is_scrub_running_in_kernel(fdmnt, di_args, fi_args.num_devices);
 
-	pr_verbose(LOG_DEFAULT, "UUID:             %s\n", fsid);
+
+	if (json_output) {
+		fmt_start(&fctx, scrub_status_rowspec, 1, 0);
+		fmt_print_start_group(&fctx, "scrub-status", JSON_TYPE_MAP);
+		fmt_print(&fctx, "uuid", fsid);
+	} else 
+		pr_verbose(LOG_DEFAULT, "UUID:             %s\n", fsid);
 
 	if (do_stats_per_dev) {
+		if (json_output) 
+			fmt_print_start_group(&fctx, "devices", JSON_TYPE_ARRAY);
+
 		for (i = 0; i < fi_args.num_devices; ++i) {
 			u64 limit;
 
@@ -2089,6 +2100,10 @@ static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)
 							"history" : "status",
 					&last_scrub->stats, limit);
 		}
+
+		if (json_output) 
+			fmt_print_end_group(&fctx, "devices");
+
 	} else {
 		u64 total_bytes_used = 0;
 		struct btrfs_ioctl_space_info *sp = si_args->spaces;
@@ -2116,10 +2131,16 @@ static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)
 			/* This is still slightly off for RAID56 */
 			total_bytes_used += sp->used_bytes * factor;
 		}
+
 		print_fs_stat(&fs_stat, print_raw, total_bytes_used,
 			      fi_args.num_devices, limit);
 	}
 
+	if (json_output) {
+		fmt_print_end_group(&fctx, "scrub-status");
+		fmt_end(&fctx);
+	}
+
 out:
 	free_history(past_scrubs);
 	free(di_args);
@@ -2130,7 +2151,7 @@ out:
 
 	return !!err;
 }
-static DEFINE_SIMPLE_COMMAND(scrub_status, "status");
+static DEFINE_COMMAND_WITH_FLAGS(scrub_status, "status", CMD_FORMAT_JSON);
 
 static const char * const cmd_scrub_limit_usage[] = {
 	"btrfs scrub limit [options] <path>",
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 6/6] btrfs-progs: Corrected a minor JSON string error in print_scrub_dev
  2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
                   ` (4 preceding siblings ...)
  2025-02-14 16:47 ` [PATCH 5/6] btrfs-progs: Added json output format for cmd_scrub_status Racz Zoltan
@ 2025-02-14 16:47 ` Racz Zoltan
  5 siblings, 0 replies; 7+ messages in thread
From: Racz Zoltan @ 2025-02-14 16:47 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs, Racz Zoltan

---
 cmds/scrub.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/cmds/scrub.c b/cmds/scrub.c
index e47c1869..88af88b0 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -468,6 +468,7 @@ static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
 
 	if (json_output) {
+		fmt_print_start_group(&fctx, NULL, JSON_TYPE_MAP);
 		fmt_print_start_group(&fctx, "device", JSON_TYPE_MAP);
 		fmt_print(&fctx, "dev", di->path);
 		fmt_print(&fctx, "id", di->devid);
@@ -498,8 +499,10 @@ static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 		}
 	}
 
-	if (json_output) 
+	if (json_output) {
 		fmt_print_end_group(&fctx, "device");
+		fmt_print_end_group(&fctx, NULL);
+	}
 }
 
 /*
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-02-14 16:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-14 16:47 [PATCH 0/6] btrfs-progs: V2 scrub status: add json output format Racz Zoltan
2025-02-14 16:47 ` [PATCH 1/6] btrfs-progs: Added rowspec struct and neccesary include file for " Racz Zoltan
2025-02-14 16:47 ` [PATCH 2/6] btrfs-progs: Added json output format for print_scrub_full Racz Zoltan
2025-02-14 16:47 ` [PATCH 3/6] btrfs-progs: Added json output format for print_scrub_summary Racz Zoltan
2025-02-14 16:47 ` [PATCH 4/6] btrfs-progs: Added json output format for _print_scrub_ss and print_scrub_dev Racz Zoltan
2025-02-14 16:47 ` [PATCH 5/6] btrfs-progs: Added json output format for cmd_scrub_status Racz Zoltan
2025-02-14 16:47 ` [PATCH 6/6] btrfs-progs: Corrected a minor JSON string error in print_scrub_dev Racz Zoltan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox