From 3fda6adc197fd35abf9e762112d451076355d217 Mon Sep 17 00:00:00 2001 From: Jacob Malevich Date: Thu, 13 Nov 2014 17:08:20 -0800 Subject: [PATCH] bcacheadm: list-cachesets --list-devs option lists the devices in each cacheset Change-Id: Ie3beb38203eefc3ab64b2a3d37652964f7021e54 Signed-off-by: Jacob Malevich --- bcache.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- bcache.h | 2 +- bcacheadm.c | 6 ++++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/bcache.c b/bcache.c index 5d1162f1..fc20e640 100644 --- a/bcache.c +++ b/bcache.c @@ -901,7 +901,43 @@ void print_dev_info(struct cache_sb *sb, bool force_csum) show_super_backingdev(sb, force_csum); } -int list_cachesets(char *cset_dir) +static void list_cacheset_devs(char *cset_dir, char *cset_name) { + int i = 0; + DIR *cachedir; + struct stat cache_stat; + char intbuf[4]; + char entry[256]; + + strcpy(entry, cset_dir); + strcat(entry, "/"); + strcat(entry, cset_name); + strcat(entry, "/cache0"); + snprintf(intbuf, 4, "%d", i); + + while(true) { + char buf[256]; + int len; + + if((cachedir = opendir(entry)) == NULL) + break; + + if(stat(entry, &cache_stat)) + break; + + if((len = readlink(entry, buf, sizeof(buf) - 1)) != -1) { + buf[len] = '\0'; + printf("\t%s\n", buf); + } + + /* remove i from end and append i++ */ + entry[strlen(entry)-strlen(intbuf)] = 0; + i++; + snprintf(intbuf, 4, "%d", i); + strcat(entry, intbuf); + } +} + +int list_cachesets(char *cset_dir, bool list_devs) { struct dirent *ent; DIR *dir = opendir(cset_dir); @@ -912,7 +948,8 @@ int list_cachesets(char *cset_dir) while ((ent = readdir(dir)) != NULL) { struct stat statbuf; - char entry[100]; + char entry[256]; + struct dirent *cache_ent; if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue; @@ -920,12 +957,18 @@ int list_cachesets(char *cset_dir) strcpy(entry, cset_dir); strcat(entry, "/"); strcat(entry, ent->d_name); + if(stat(entry, &statbuf) == -1) { fprintf(stderr, "Failed to stat %s\n", entry); return 1; } + if (S_ISDIR(statbuf.st_mode)) { printf("%s\n", ent->d_name); + + if(list_devs) { + list_cacheset_devs(cset_dir, ent->d_name); + } } } diff --git a/bcache.h b/bcache.h index 873fdb74..0dff6139 100644 --- a/bcache.h +++ b/bcache.h @@ -58,7 +58,7 @@ void show_super_backingdev(struct cache_sb *, bool); void show_super_cache(struct cache_sb *, bool); struct cache_sb *query_dev(char *, bool); -int list_cachesets(char *); +int list_cachesets(char *, bool); char *parse_array_to_list(char *const *); int register_bcache(); int probe(char *, int); diff --git a/bcacheadm.c b/bcacheadm.c index 64ebb6b2..57264307 100644 --- a/bcacheadm.c +++ b/bcacheadm.c @@ -85,6 +85,7 @@ bool udev = false; /* list globals */ char *cset_dir = "/sys/fs/bcache"; +bool list_devs = false; /* status globals */ bool status_all = false; @@ -203,6 +204,7 @@ static NihOption query_devs_options[] = { static NihOption list_cachesets_options[] = { {'d', "dir", N_("directory"), NULL, NULL, &cset_dir, NULL}, + {0, "list-devs", N_("list all devices in the cache sets as well"), NULL, NULL, &list_devs, NULL}, NIH_OPTION_LAST }; @@ -352,7 +354,7 @@ int bcache_register (NihCommand *command, char *const *args) int bcache_list_cachesets (NihCommand *command, char *const *args) { - return list_cachesets(cset_dir); + return list_cachesets(cset_dir, list_devs); } int bcache_query_devs (NihCommand *command, char *const *args) @@ -361,7 +363,7 @@ int bcache_query_devs (NihCommand *command, char *const *args) for (i = 0; args[i] != NULL; i++) { - printf("query-devs on :%s\n", args[i]); + printf("query-devs on: %s\n", args[i]); struct cache_sb *sb = query_dev(args[i], false); print_dev_info(sb, force_csum); }