Whamcloud - gitweb
LU-5868 nodemap: Add nodemap_info to lctl
[fs/lustre-release.git] / lustre / utils / lustre_cfg.c
index a7af694..cc9763d 100644 (file)
@@ -523,6 +523,7 @@ struct param_opts {
        unsigned int po_recursive:1;
        unsigned int po_params2:1;
        unsigned int po_delete:1;
+       unsigned int po_only_dir:1;
 };
 
 /* Param set to single log file, used by all clients and servers.
@@ -664,22 +665,27 @@ int jt_lcfg_mgsparam(int argc, char **argv)
 /* Display the path in the same format as sysctl
  * For eg. obdfilter.lustre-OST0000.stats */
 static char *
-display_name(char *filename, size_t filename_size, bool po_show_type)
+display_name(char *filename, size_t filename_size, struct param_opts *popt)
 {
        struct stat st;
        char *tmp;
        char *suffix = NULL;
 
-       if (po_show_type) {
+       if (popt->po_show_type || popt->po_only_dir) {
                if (lstat(filename, &st) == -1)
                        return NULL;
 
-               if (S_ISDIR(st.st_mode))
-                       suffix = "/";
-               else if (S_ISLNK(st.st_mode))
-                       suffix = "@";
-               else if (st.st_mode & S_IWUSR)
-                       suffix = "=";
+               if (popt->po_show_type) {
+                       if (S_ISDIR(st.st_mode))
+                               suffix = "/";
+                       else if (S_ISLNK(st.st_mode))
+                               suffix = "@";
+                       else if (st.st_mode & S_IWUSR)
+                               suffix = "=";
+               } else if (popt->po_only_dir) {
+                       if (!S_ISDIR(st.st_mode))
+                               return NULL;
+               }
        }
 
        filename += strlen("/proc/");
@@ -700,7 +706,7 @@ display_name(char *filename, size_t filename_size, bool po_show_type)
 
        /* Append the indicator to entries.  We know there is enough space
         * for the suffix, since the path prefix was deleted. */
-       if (po_show_type && suffix != NULL)
+       if (popt->po_show_type && suffix != NULL)
                strncat(filename, suffix, filename_size);
 
        return filename;
@@ -780,27 +786,31 @@ static int lprocfs_param_pattern(const char *pattern, char *buf, size_t bufsize)
 
 static int listparam_cmdline(int argc, char **argv, struct param_opts *popt)
 {
-        int ch;
+       int ch;
 
-        popt->po_show_path = 1;
-        popt->po_only_path = 1;
-        popt->po_show_type = 0;
-        popt->po_recursive = 0;
+       popt->po_show_path = 1;
+       popt->po_only_path = 1;
+       popt->po_show_type = 0;
+       popt->po_recursive = 0;
+       popt->po_only_dir = 0;
 
-        while ((ch = getopt(argc, argv, "FR")) != -1) {
-                switch (ch) {
-                case 'F':
-                        popt->po_show_type = 1;
-                        break;
-                case 'R':
-                        popt->po_recursive = 1;
-                        break;
-                default:
-                        return -1;
-                }
-        }
+       while ((ch = getopt(argc, argv, "FRD")) != -1) {
+               switch (ch) {
+               case 'F':
+                       popt->po_show_type = 1;
+                       break;
+               case 'R':
+                       popt->po_recursive = 1;
+                       break;
+               case 'D':
+                       popt->po_only_dir = 1;
+                       break;
+               default:
+                       return -1;
+               }
+       }
 
-        return optind;
+       return optind;
 }
 
 static int listparam_display(struct param_opts *popt, char *pattern)
@@ -814,7 +824,11 @@ static int listparam_display(struct param_opts *popt, char *pattern)
        if (rc < 0)
                return rc;
 
-       rc = glob(filename, GLOB_BRACE | (popt->po_recursive ? GLOB_MARK : 0),
+       rc = glob(filename,
+                 GLOB_BRACE |
+                       (popt->po_recursive ? GLOB_MARK : 0) |
+                       /* GLOB_ONLYDIR doesn't guarantee, only a hint */
+                       (popt->po_only_dir ? GLOB_ONLYDIR : 0),
                  NULL, &glob_info);
        if (rc) {
                fprintf(stderr, "error: list_param: %s: %s\n",
@@ -835,7 +849,7 @@ static int listparam_display(struct param_opts *popt, char *pattern)
                else
                        last = 0;
                strlcpy(filename, glob_info.gl_pathv[i], len);
-               valuename = display_name(filename, len, popt->po_show_type);
+               valuename = display_name(filename, len, popt);
                if (valuename)
                        printf("%s\n", valuename);
                if (last) {
@@ -944,7 +958,7 @@ static int getparam_display(struct param_opts *popt, char *pattern)
                        strncpy(filename, glob_info.gl_pathv[i],
                                sizeof(filename));
                        valuename = display_name(filename, sizeof(filename),
-                                                false);
+                                                popt);
                }
 
                 /* Write the contents of file to stdout */
@@ -1025,6 +1039,54 @@ int jt_lcfg_getparam(int argc, char **argv)
        return rc;
 }
 
+/**
+ * Output information about nodemaps.
+ * \param      argc            number of args
+ * \param      argv[]          variable string arguments
+ *
+ * [list|nodemap_name|all]     \a list will list all nodemaps (default).
+ *                             Specifying a \a nodemap_name will
+ *                             display info about that specific nodemap.
+ *                             \a all will display info for all nodemaps.
+ * \retval                     0 on success
+ */
+int jt_nodemap_info(int argc, char **argv)
+{
+       const char              usage_str[] = "usage: nodemap_info "
+                                             "[list|nodemap_name|all]\n";
+       struct param_opts       popt = {
+               .po_only_path = 0,
+               .po_show_path = 1,
+               .po_show_type = 0,
+               .po_recursive = 0,
+               .po_only_dir = 0
+       };
+       int                     rc = 0;
+
+       if (argc > 2) {
+               fprintf(stderr, usage_str);
+               return -1;
+       }
+
+       if (argc == 1 || strcmp("list", argv[1]) == 0) {
+               popt.po_only_path = 1;
+               popt.po_only_dir = 1;
+               rc = listparam_display(&popt, "nodemap/*");
+       } else if (strcmp("all", argv[1]) == 0) {
+               rc = getparam_display(&popt, "nodemap/*/*");
+       } else {
+               char    pattern[PATH_MAX];
+
+               snprintf(pattern, sizeof(pattern), "nodemap/%s/*", argv[1]);
+               rc = getparam_display(&popt, pattern);
+               if (rc == -ESRCH)
+                       fprintf(stderr, "error: nodemap_info: cannot find"
+                                       "nodemap %s\n", argv[1]);
+       }
+       return rc;
+}
+
+
 static int setparam_cmdline(int argc, char **argv, struct param_opts *popt)
 {
         int ch;
@@ -1081,7 +1143,7 @@ static int setparam_display(struct param_opts *popt, char *pattern, char *value)
                        strncpy(filename, glob_info.gl_pathv[i],
                                sizeof(filename));
                        valuename = display_name(filename, sizeof(filename),
-                                                false);
+                                                popt);
                        if (valuename)
                                printf("%s=%s\n", valuename, value);
                }