Whamcloud - gitweb
LU-6179 llite: Implement ladvise lockahead
[fs/lustre-release.git] / lustre / utils / lfs.c
index 35c908e..dc07fb9 100644 (file)
@@ -65,8 +65,8 @@
 #include <libcfs/util/ioctl.h>
 #include <libcfs/util/parser.h>
 #include <lustre/lustreapi.h>
-#include <lustre_ver.h>
-#include <linux/lustre_param.h>
+#include <linux/lustre/lustre_ver.h>
+#include <linux/lustre/lustre_param.h>
 
 #ifndef ARRAY_SIZE
 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
@@ -400,8 +400,9 @@ command_t cmdlist[] = {
        {"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"
+        "               [--background|-b] [--unset|-u]\n\n"
         "               {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
+        "               {[--mode|-m [READ,WRITE]}\n"
         "               <file> ...\n"},
        {"help", Parser_help, 0, "help"},
        {"exit", Parser_quit, 0, "quit"},
@@ -1009,23 +1010,6 @@ static int parse_targets(__u32 *osts, int size, int offset, char *arg)
        return rc < 0 ? rc : nr;
 }
 
-static int verify_pool_name(char *prog_name, char *pool_name)
-{
-       char *ptr;
-
-       if (pool_name == NULL)
-               return 0;
-
-       ptr = strchr(pool_name, '.');
-       if (ptr != NULL && ptr == pool_name) {
-               fprintf(stderr, "error: %s: fsname is empty in pool name '%s'\n",
-                       prog_name, pool_name);
-               return -EINVAL;
-       }
-
-       return 0;
-}
-
 struct lfs_setstripe_args {
        unsigned long long       lsa_comp_end;
        unsigned long long       lsa_stripe_size;
@@ -1549,8 +1533,7 @@ static int lfs_setstripe(int argc, char **argv)
                                lsa.lsa_stripe_off = osts[0];
                        break;
                case 'p':
-                       result = verify_pool_name(argv[0], optarg);
-                       if (result)
+                       if (optarg == NULL)
                                goto error;
                        lsa.lsa_pool_name = optarg;
                        break;
@@ -2767,20 +2750,19 @@ static int lfs_getdirstripe(int argc, char **argv)
        struct find_param param = { 0 };
        struct option long_opts[] = {
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
-               {"mdt-count",   no_argument,            0, 'c'},
+       { .val = 'c',   .name = "mdt-count",    .has_arg = no_argument },
 #endif
-               {"mdt-hash",    no_argument,            0, 'H'},
-               {"mdt-index",   no_argument,            0, 'i'},
-               {"recursive",   no_argument,            0, 'r'},
+       { .val = 'D',   .name = "default",      .has_arg = no_argument },
+       { .val = 'H',   .name = "mdt-hash",     .has_arg = no_argument },
+       { .val = 'i',   .name = "mdt-index",    .has_arg = no_argument },
+       { .val = 'O',   .name = "obd",          .has_arg = required_argument },
+       { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
-               {"mdt-hash",    no_argument,            0, 't'},
+       { .val = 't',   .name = "mdt-hash",     .has_arg = no_argument },
 #endif
-               {"default",     no_argument,            0, 'D'},
-               {"obd",         required_argument,      0, 'O'},
-               {"mdt-count",   no_argument,            0, 'T'},
-               {"yaml",        no_argument,            0, 'y'},
-               {0, 0, 0, 0}
-       };
+       { .val = 'T',   .name = "mdt-count",    .has_arg = no_argument },
+       { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
+       { .name = NULL } };
        int c, rc;
 
        param.fp_get_lmv = 1;
@@ -2901,8 +2883,9 @@ static int lfs_setdirstripe(int argc, char **argv)
                case 'c':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
                        if (strcmp(argv[optind - 1], "--count") == 0)
-                               fprintf(stderr, "warning: '--count' deprecated"
-                                       ", use '--mdt-count' instead\n");
+                               fprintf(stderr,
+                                       "%s %s: warning: '--count' deprecated, use '--mdt-count' instead\n",
+                                       progname, argv[0]);
 #endif
                        stripe_count_opt = optarg;
                        break;
@@ -2916,8 +2899,9 @@ static int lfs_setdirstripe(int argc, char **argv)
                case 'i':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
                        if (strcmp(argv[optind - 1], "--index") == 0)
-                               fprintf(stderr, "warning: '--index' deprecated"
-                                       ", use '--mdt-index' instead\n");
+                               fprintf(stderr,
+                                       "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
+                                       progname, argv[0]);
 #endif
                        stripe_offset_opt = optarg;
                        break;
@@ -2930,29 +2914,29 @@ static int lfs_setdirstripe(int argc, char **argv)
                case 'H':
 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 11, 53, 0)
                        if (strcmp(argv[optind - 1], "--hash-type") == 0)
-                               fprintf(stderr, "warning: '--hash-type' "
-                                       "deprecated, use '--mdt-hash' "
-                                       "instead\n");
+                               fprintf(stderr,
+                                       "%s %s: warning: '--hash-type' deprecated, use '--mdt-hash' instead\n",
+                                       progname, argv[0]);
 #endif
                        stripe_hash_opt = optarg;
                        break;
                default:
-                       fprintf(stderr, "error: %s: option '%s' "
-                                       "unrecognized\n",
-                                       argv[0], argv[optind - 1]);
+                       fprintf(stderr, "%s %s: unrecognized option '%s'\n",
+                               progname, argv[0], argv[optind - 1]);
                        return CMD_HELP;
                }
        }
 
        if (optind == argc) {
-               fprintf(stderr, "error: %s: missing dirname\n",
-                       argv[0]);
+               fprintf(stderr, "%s %s: DIR must be specified\n",
+                       progname, argv[0]);
                return CMD_HELP;
        }
 
        if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
-               fprintf(stderr, "error: %s: missing stripe offset and count.\n",
-                       argv[0]);
+               fprintf(stderr,
+                       "%s %s: stripe offset and count must be specified\n",
+                       progname, argv[0]);
                return CMD_HELP;
        }
 
@@ -2960,16 +2944,18 @@ static int lfs_setdirstripe(int argc, char **argv)
                /* get the stripe offset */
                stripe_offset = strtoul(stripe_offset_opt, &end, 0);
                if (*end != '\0') {
-                       fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
-                               argv[0], stripe_offset_opt);
+                       fprintf(stderr,
+                               "%s %s: bad stripe offset '%s'\n",
+                               progname, argv[0], stripe_offset_opt);
                        return CMD_HELP;
                }
        }
 
        if (delete) {
                if (stripe_offset_opt != NULL || stripe_count_opt != NULL) {
-                       fprintf(stderr, "error: %s: cannot specify -d with -s,"
-                               " or -i options.\n", argv[0]);
+                       fprintf(stderr,
+                               "%s %s: cannot specify -d with -c or -i options\n",
+                               progname, argv[0]);
                        return CMD_HELP;
                } else {
                        stripe_count = 0;
@@ -2980,8 +2966,9 @@ static int lfs_setdirstripe(int argc, char **argv)
        if (mode_opt != NULL) {
                mode = strtoul(mode_opt, &end, 8);
                if (*end != '\0') {
-                       fprintf(stderr, "error: %s: bad mode '%s'\n",
-                               argv[0], mode_opt);
+                       fprintf(stderr,
+                               "%s %s: bad MODE '%s'\n",
+                               progname, argv[0], mode_opt);
                        return CMD_HELP;
                }
                previous_mode = umask(0);
@@ -2992,9 +2979,8 @@ static int lfs_setdirstripe(int argc, char **argv)
        } else {
                hash_type = check_hashtype(stripe_hash_opt);
                if (hash_type == 0) {
-                       fprintf(stderr,
-                               "error: %s: bad stripe hash type '%s'\n",
-                               argv[0], stripe_hash_opt);
+                       fprintf(stderr, "%s %s: bad stripe hash type '%s'\n",
+                               progname, argv[0], stripe_hash_opt);
                        return CMD_HELP;
                }
        }
@@ -3003,8 +2989,9 @@ static int lfs_setdirstripe(int argc, char **argv)
        if (stripe_count_opt != NULL) {
                stripe_count = strtoul(stripe_count_opt, &end, 0);
                if (*end != '\0') {
-                       fprintf(stderr, "error: %s: bad stripe count '%s'\n",
-                               argv[0], stripe_count_opt);
+                       fprintf(stderr,
+                               "%s %s: bad stripe count '%s'\n",
+                               progname, argv[0], stripe_count_opt);
                        return CMD_HELP;
                }
        }
@@ -3023,8 +3010,9 @@ static int lfs_setdirstripe(int argc, char **argv)
                }
 
                if (result) {
-                       fprintf(stderr, "error: %s: create stripe dir '%s' "
-                               "failed\n", argv[0], dname);
+                       fprintf(stderr,
+                               "%s setdirstripe: cannot create stripe dir '%s': %s\n",
+                               progname, dname, strerror(-result));
                        break;
                }
                dname = argv[++optind];
@@ -4385,19 +4373,18 @@ static int lfs_ls(int argc, char **argv)
 
 static int lfs_changelog(int argc, char **argv)
 {
-        void *changelog_priv;
+       void *changelog_priv;
        struct changelog_rec *rec;
-        long long startrec = 0, endrec = 0;
-        char *mdd;
-        struct option long_opts[] = {
-                {"follow", no_argument, 0, 'f'},
-                {0, 0, 0, 0}
-        };
-        char short_opts[] = "f";
-        int rc, follow = 0;
-
-        while ((rc = getopt_long(argc, argv, short_opts,
-                                long_opts, NULL)) != -1) {
+       long long startrec = 0, endrec = 0;
+       char *mdd;
+       struct option long_opts[] = {
+               { .val = 'f', .name = "follow", .has_arg = no_argument },
+               { .name = NULL } };
+       char short_opts[] = "f";
+       int rc, follow = 0;
+
+       while ((rc = getopt_long(argc, argv, short_opts,
+               long_opts, NULL)) != -1) {
                 switch (rc) {
                 case 'f':
                         follow++;
@@ -5232,6 +5219,28 @@ static int lfs_swap_layouts(int argc, char **argv)
 
 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
 
+static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
+
+static const char *const lockahead_results[] = {
+       [LLA_RESULT_SENT] = "Lock request sent",
+       [LLA_RESULT_DIFFERENT] = "Different matching lock found",
+       [LLA_RESULT_SAME] = "Matching lock on identical extent found",
+};
+
+int lfs_get_mode(const char *string)
+{
+       enum lock_mode_user mode;
+
+       for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
+               if (lock_mode_names[mode] == NULL)
+                       continue;
+               if (strcmp(string, lock_mode_names[mode]) == 0)
+                       return mode;
+       }
+
+       return -EINVAL;
+}
+
 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
 {
        enum lu_ladvise_type advice;
@@ -5254,9 +5263,11 @@ static int lfs_ladvise(int argc, char **argv)
        { .val = 'b',   .name = "background",   .has_arg = no_argument },
        { .val = 'e',   .name = "end",          .has_arg = required_argument },
        { .val = 'l',   .name = "length",       .has_arg = required_argument },
+       { .val = 'm',   .name = "mode",         .has_arg = required_argument },
        { .val = 's',   .name = "start",        .has_arg = required_argument },
+       { .val = 'u',   .name = "unset",        .has_arg = no_argument },
        { .name = NULL } };
-       char                     short_opts[] = "a:be:l:s:";
+       char                     short_opts[] = "a:be:l:m:s:u";
        int                      c;
        int                      rc = 0;
        const char              *path;
@@ -5268,6 +5279,7 @@ static int lfs_ladvise(int argc, char **argv)
        unsigned long long       length = 0;
        unsigned long long       size_units;
        unsigned long long       flags = 0;
+       int                      mode = 0;
 
        optind = 0;
        while ((c = getopt_long(argc, argv, short_opts,
@@ -5296,6 +5308,9 @@ static int lfs_ladvise(int argc, char **argv)
                case 'b':
                        flags |= LF_ASYNC;
                        break;
+               case 'u':
+                       flags |= LF_UNSET;
+                       break;
                case 'e':
                        size_units = 1;
                        rc = llapi_parse_size(optarg, &end,
@@ -5326,6 +5341,15 @@ static int lfs_ladvise(int argc, char **argv)
                                return CMD_HELP;
                        }
                        break;
+               case 'm':
+                       mode = lfs_get_mode(optarg);
+                       if (mode < 0) {
+                               fprintf(stderr, "%s: bad mode '%s', valid "
+                                                "modes are READ or WRITE\n",
+                                       argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
                case '?':
                        return CMD_HELP;
                default:
@@ -5348,6 +5372,13 @@ static int lfs_ladvise(int argc, char **argv)
                return CMD_HELP;
        }
 
+       if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
+               fprintf(stderr, "%s: Lock no expand advice is a per file "
+                                "descriptor advice, so when called from lfs, "
+                                "it does nothing.\n", argv[0]);
+               return CMD_HELP;
+       }
+
        if (argc <= optind) {
                fprintf(stderr, "%s: please give one or more file names\n",
                        argv[0]);
@@ -5369,6 +5400,18 @@ static int lfs_ladvise(int argc, char **argv)
                return CMD_HELP;
        }
 
+       if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
+               fprintf(stderr, "%s: mode is only valid with lockahead\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
+       if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
+               fprintf(stderr, "%s: mode is required with lockahead\n",
+                       argv[0]);
+               return CMD_HELP;
+       }
+
        while (optind < argc) {
                int rc2;
 
@@ -5389,6 +5432,11 @@ static int lfs_ladvise(int argc, char **argv)
                advice.lla_value2 = 0;
                advice.lla_value3 = 0;
                advice.lla_value4 = 0;
+               if (advice_type == LU_LADVISE_LOCKAHEAD) {
+                       advice.lla_lockahead_mode = mode;
+                       advice.lla_peradvice_flags = flags;
+               }
+
                rc2 = llapi_ladvise(fd, flags, 1, &advice);
                close(fd);
                if (rc2 < 0) {
@@ -5396,7 +5444,10 @@ static int lfs_ladvise(int argc, char **argv)
                                "'%s': %s\n", argv[0],
                                ladvise_names[advice_type],
                                path, strerror(errno));
+
+                       goto next;
                }
+
 next:
                if (rc == 0 && rc2 < 0)
                        rc = rc2;