Whamcloud - gitweb
LU-7813 utils: improve lfs setstripe pool error message
[fs/lustre-release.git] / lustre / utils / lfs.c
index 9059890..60bdab6 100644 (file)
 #include <lustre_ver.h>
 #include <lustre_param.h>
 
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
+#endif /* !ARRAY_SIZE */
+
 /* all functions */
 static int lfs_setstripe(int argc, char **argv);
 static int lfs_find(int argc, char **argv);
@@ -116,6 +120,7 @@ static int lfs_hsm_remove(int argc, char **argv);
 static int lfs_hsm_cancel(int argc, char **argv);
 static int lfs_swap_layouts(int argc, char **argv);
 static int lfs_mv(int argc, char **argv);
+static int lfs_ladvise(int argc, char **argv);
 
 /* Setstripe and migrate share mostly the same parameters */
 #define SSM_CMD_COMMON(cmd) \
@@ -359,19 +364,19 @@ command_t cmdlist[] = {
         "migrate a directory between MDTs.\n"
         "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
         "<directory>\n"
-        "\tmdt_idx:      index of the destination MDT\n"},
-       {"migrate", lfs_setstripe, 0,
+        "\tmdt_idx:      index of the destination MDT\n"
+        "\n"
         "migrate file objects from one OST "
         "layout\nto another (may be not safe with concurent writes).\n"
-        "usage: migrate "
+        "usage: migrate  "
         "[--stripe-count|-c] <stripe_count>\n"
-        "[--stripe-index|-i] <start_ost_index>\n"
-        "[--stripe-size|-S] <stripe_size>\n"
-        "[--pool|-p] <pool_name>\n"
-        "[--ost-list|-o] <ost_indices>\n"
-        "[--block|-b]\n"
-        "[--non-block|-n]\n"
-        "<file|directory>\n"
+        "              [--stripe-index|-i] <start_ost_index>\n"
+        "              [--stripe-size|-S] <stripe_size>\n"
+        "              [--pool|-p] <pool_name>\n"
+        "              [--ost-list|-o] <ost_indices>\n"
+        "              [--block|-b]\n"
+        "              [--non-block|-n]\n"
+        "              <file|directory>\n"
         "\tstripe_count:     number of OSTs to stripe a file over\n"
         "\tstripe_ost_index: index of the first OST to stripe a file over\n"
         "\tstripe_size:      number of bytes to store before moving to the next OST\n"
@@ -384,6 +389,12 @@ command_t cmdlist[] = {
         "use \"migrate\" instead.\n"
         "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
         "[--verbose|-v]\n"},
+       {"ladvise", lfs_ladvise, 0,
+        "Provide servers with advice about access patterns for a file.\n"
+        "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
+        "               [--background|-b]\n"
+        "               {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
+        "               <file> ..."},
        {"help", Parser_help, 0, "help"},
        {"exit", Parser_quit, 0, "quit"},
        {"quit", Parser_quit, 0, "quit"},
@@ -1213,10 +1224,11 @@ static int lfs_setstripe(int argc, char **argv)
                        /* Save the first error encountered. */
                        if (result2 == 0)
                                result2 = result;
-                       fprintf(stderr,
-                               "error: %s: %s file '%s' failed\n",
+                       fprintf(stderr, "error: %s: %s file '%s' failed: %s\n",
                                argv[0], migrate_mode ? "migrate" : "create",
-                               fname);
+                               fname,
+                               pool_name_arg != NULL && result == EINVAL ?
+                               "OST not in pool?" : strerror(result));
                        continue;
                }
        }
@@ -2947,7 +2959,7 @@ static void print_quota_title(char *name, struct if_quotactl *qctl,
 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
 {
        if (!h) {
-               snprintf(buf, buflen, LPU64, num);
+               snprintf(buf, buflen, "%ju", (uintmax_t)num);
        } else {
                if (num >> 40)
                        snprintf(buf, buflen, "%5.4gP",
@@ -2962,7 +2974,7 @@ static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
                        snprintf(buf, buflen, "%5.4gM",
                                 (double)num / (1 << 10));
                else
-                       snprintf(buf, buflen, LPU64"%s", num, "k");
+                       snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
        }
 }
 
@@ -3039,16 +3051,17 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
                        diff2str(dqb->dqb_itime, timebuf, now);
 
                sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
-                       LPU64 : "["LPU64"]", dqb->dqb_curinodes);
+                       "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
 
                if (type == QC_GENERAL)
                        sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
-                               LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
+                               "%ju" : "[%ju]",
+                               (uintmax_t)dqb->dqb_isoftlimit);
                else
                        sprintf(numbuf[1], "%s", "-");
 
                sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
-                       LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
+                       "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
 
                if (type != QC_OSTIDX)
                        printf(" %7s%c %6s %7s %7s",
@@ -3260,8 +3273,9 @@ ug_output:
                                      &total_balloc);
                kbytes2str(total_balloc, strbuf, sizeof(strbuf),
                           human_readable);
-               printf("Total allocated inode limit: "LPU64", total "
-                      "allocated block limit: %s\n", total_ialloc, strbuf);
+               printf("Total allocated inode limit: %ju, total "
+                      "allocated block limit: %s\n", (uintmax_t)total_ialloc,
+                      strbuf);
        }
 
         if (rc1 || rc2 || rc3 || inacc)
@@ -3441,8 +3455,8 @@ static int lfs_changelog(int argc, char **argv)
 
                secs = rec->cr_time >> 30;
                gmtime_r(&secs, &ts);
-               printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
-                      "0x%x t="DFID, rec->cr_index, rec->cr_type,
+               printf("%ju %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
+                      "0x%x t="DFID, (uintmax_t) rec->cr_index, rec->cr_type,
                       changelog_type2str(rec->cr_type),
                       ts.tm_hour, ts.tm_min, ts.tm_sec,
                       (int)(rec->cr_time & ((1<<30) - 1)),
@@ -3706,7 +3720,7 @@ static int lfs_data_version(int argc, char **argv)
        if (rc < 0)
                err(errno, "cannot get version for %s", path);
        else
-               printf(LPU64 "\n", data_version);
+               printf("%ju" "\n", (uintmax_t)data_version);
 
        close(fd);
        return rc;
@@ -4219,6 +4233,178 @@ static int lfs_swap_layouts(int argc, char **argv)
                                  SWAP_LAYOUTS_KEEP_ATIME);
 }
 
+static const char *const ladvise_names[] = LU_LADVISE_NAMES;
+
+static enum lu_ladvise_type lfs_get_ladvice(const char *string)
+{
+       enum lu_ladvise_type advice;
+
+       for (advice = 0;
+            advice < ARRAY_SIZE(ladvise_names); advice++) {
+               if (ladvise_names[advice] == NULL)
+                       continue;
+               if (strcmp(string, ladvise_names[advice]) == 0)
+                       return advice;
+       }
+
+       return LU_LADVISE_INVALID;
+}
+
+static int lfs_ladvise(int argc, char **argv)
+{
+       struct option            long_opts[] = {
+               {"advice",      required_argument,      0, 'a'},
+               {"background",  no_argument,            0, 'b'},
+               {"end",         required_argument,      0, 'e'},
+               {"start",       required_argument,      0, 's'},
+               {"length",      required_argument,      0, 'l'},
+               {0, 0, 0, 0}
+       };
+       char                     short_opts[] = "a:be:l:s:";
+       int                      c;
+       int                      rc = 0;
+       const char              *path;
+       int                      fd;
+       struct lu_ladvise        advice;
+       enum lu_ladvise_type     advice_type = LU_LADVISE_INVALID;
+       unsigned long long       start = 0;
+       unsigned long long       end = LUSTRE_EOF;
+       unsigned long long       length = 0;
+       unsigned long long       size_units;
+       unsigned long long       flags = 0;
+
+       optind = 0;
+       while ((c = getopt_long(argc, argv, short_opts,
+                               long_opts, NULL)) != -1) {
+               switch (c) {
+               case 'a':
+                       advice_type = lfs_get_ladvice(optarg);
+                       if (advice_type == LU_LADVISE_INVALID) {
+                               fprintf(stderr, "%s: invalid advice type "
+                                       "'%s'\n", argv[0], optarg);
+                               fprintf(stderr, "Valid types:");
+
+                               for (advice_type = 0;
+                                    advice_type < ARRAY_SIZE(ladvise_names);
+                                    advice_type++) {
+                                       if (ladvise_names[advice_type] == NULL)
+                                               continue;
+                                       fprintf(stderr, " %s",
+                                               ladvise_names[advice_type]);
+                               }
+                               fprintf(stderr, "\n");
+
+                               return CMD_HELP;
+                       }
+                       break;
+               case 'b':
+                       flags |= LF_ASYNC;
+                       break;
+               case 'e':
+                       size_units = 1;
+                       rc = llapi_parse_size(optarg, &end,
+                                             &size_units, 0);
+                       if (rc) {
+                               fprintf(stderr, "%s: bad end offset '%s'\n",
+                                       argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case 's':
+                       size_units = 1;
+                       rc = llapi_parse_size(optarg, &start,
+                                             &size_units, 0);
+                       if (rc) {
+                               fprintf(stderr, "%s: bad start offset "
+                                       "'%s'\n", argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case 'l':
+                       size_units = 1;
+                       rc = llapi_parse_size(optarg, &length,
+                                             &size_units, 0);
+                       if (rc) {
+                               fprintf(stderr, "%s: bad length '%s'\n",
+                                       argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case '?':
+                       return CMD_HELP;
+               default:
+                       fprintf(stderr, "%s: option '%s' unrecognized\n",
+                               argv[0], argv[optind - 1]);
+                       return CMD_HELP;
+               }
+       }
+
+       if (advice_type == LU_LADVISE_INVALID) {
+               fprintf(stderr, "%s: please give an advice type\n", argv[0]);
+               fprintf(stderr, "Valid types:");
+               for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
+                    advice_type++) {
+                       if (ladvise_names[advice_type] == NULL)
+                               continue;
+                       fprintf(stderr, " %s", ladvise_names[advice_type]);
+               }
+               fprintf(stderr, "\n");
+               return CMD_HELP;
+       }
+
+       if (argc <= optind) {
+               fprintf(stderr, "%s: please give one or more file names\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
+       if (end != LUSTRE_EOF && length != 0 && end != start + length) {
+               fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
+       if (end == LUSTRE_EOF && length != 0)
+               end = start + length;
+
+       if (end <= start) {
+               fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
+                       argv[0], start, end);
+               return CMD_HELP;
+       }
+
+       while (optind < argc) {
+               int rc2;
+
+               path = argv[optind++];
+
+               fd = open(path, O_RDONLY);
+               if (fd < 0) {
+                       fprintf(stderr, "%s: cannot open file '%s': %s\n",
+                               argv[0], path, strerror(errno));
+                       rc2 = -errno;
+                       goto next;
+               }
+
+               advice.lla_start = start;
+               advice.lla_end = end;
+               advice.lla_advice = advice_type;
+               advice.lla_padding = 0;
+               rc2 = llapi_ladvise(fd, flags, 1, &advice);
+               close(fd);
+               if (rc2 < 0) {
+                       fprintf(stderr, "%s: cannot give advice '%s' to file "
+                               "'%s': %s\n", argv[0],
+                               ladvise_names[advice_type],
+                               path, strerror(errno));
+               }
+next:
+               if (rc == 0 && rc2 < 0)
+                       rc = rc2;
+       }
+       return rc;
+}
+
 int main(int argc, char **argv)
 {
         int rc;