]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
bcacheadm: list-cachesets --list-devs option
authorJacob Malevich <jam@daterainc.com>
Fri, 14 Nov 2014 01:08:20 +0000 (17:08 -0800)
committerJacob Malevich <jam@daterainc.com>
Fri, 14 Nov 2014 01:08:20 +0000 (17:08 -0800)
lists the devices in each cacheset

Change-Id: Ie3beb38203eefc3ab64b2a3d37652964f7021e54
Signed-off-by: Jacob Malevich <jam@daterainc.com>
bcache.c
bcache.h
bcacheadm.c

index 5d1162f12d3a5957dbc53d40b6c847ed939198f4..fc20e6408123722dba63eae3637dc096167331a1 100644 (file)
--- 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);
+                       }
                }
        }
 
index 873fdb74642b124c2a3d97543db7a86f16a7505f..0dff61397c1d58cc5e56c7d7c3961d97f6c8bbbf 100644 (file)
--- 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);
index 64ebb6b2d766793e40f2e397569a398d141720a1..572643079e90ab0d224f6d908911295c147cc7bd 100644 (file)
@@ -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);
        }