From 9f4ed5ce05888b62d1e4417323c553e5d06f4abf Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Mon, 1 Apr 2024 19:55:03 -0400 Subject: [PATCH] cmd_show_super: Also print device model Signed-off-by: Kent Overstreet --- c_src/cmd_format.c | 9 +++++++++ c_src/tools-util.c | 43 +++++++++++++++++++++++++++++++++++++++++++ c_src/tools-util.h | 1 + 3 files changed, 53 insertions(+) diff --git a/c_src/cmd_format.c b/c_src/cmd_format.c index 4d6c2dc1..e2468350 100644 --- a/c_src/cmd_format.c +++ b/c_src/cmd_format.c @@ -384,6 +384,15 @@ int cmd_show_super(int argc, char *argv[]) if (f) __bch2_sb_field_to_text(&buf, sb.sb, f); } else { + printbuf_tabstop_push(&buf, 44); + + char *model = fd_to_dev_model(sb.bdev->bd_fd); + prt_str(&buf, "Device:"); + prt_tab(&buf); + prt_str(&buf, model); + prt_newline(&buf); + free(model); + bch2_sb_to_text(&buf, sb.sb, print_layout, fields); } printf("%s", buf.buf); diff --git a/c_src/tools-util.c b/c_src/tools-util.c index 3b9f149d..d8371c24 100644 --- a/c_src/tools-util.c +++ b/c_src/tools-util.c @@ -579,6 +579,49 @@ int dev_mounted(char *dev) return 2; } +static char *dev_to_sysfs_path(dev_t dev) +{ + return mprintf("/sys/dev/block/%u:%u", major(dev), minor(dev)); +} + +char *fd_to_dev_model(int fd) +{ + struct stat stat = xfstat(fd); + + if (S_ISBLK(stat.st_mode)) { + char *sysfs_path = dev_to_sysfs_path(stat.st_rdev); + + char *model_path = mprintf("%s/device/model", sysfs_path); + if (!access(model_path, R_OK)) + goto got_model; + free(model_path); + + /* partition? try parent */ + + char buf[1024]; + if (readlink(sysfs_path, buf, sizeof(buf)) < 0) + die("readlink error on %s: %m", sysfs_path); + + free(sysfs_path); + sysfs_path = strdup(buf); + + *strrchr(sysfs_path, '/') = 0; + model_path = mprintf("%s/device/model", sysfs_path); + if (!access(model_path, R_OK)) + goto got_model; + + return strdup("(unknown device)"); + char *model; +got_model: + model = read_file_str(AT_FDCWD, model_path); + free(model_path); + free(sysfs_path); + return model; + } else { + return strdup("(reg file)"); + } +} + static int kstrtoull_symbolic(const char *s, unsigned int base, unsigned long long *res) { if (!strcmp(s, "U64_MAX")) { diff --git a/c_src/tools-util.h b/c_src/tools-util.h index bff3bc65..269d589b 100644 --- a/c_src/tools-util.h +++ b/c_src/tools-util.h @@ -180,6 +180,7 @@ char *dev_to_name(dev_t); char *dev_to_path(dev_t); struct mntent *dev_to_mount(char *); int dev_mounted(char *); +char *fd_to_dev_model(int); #define args_shift(_nr) \ do { \