Whamcloud - gitweb
LU-5710 all: second batch of corrected typos and grammar errors
[fs/lustre-release.git] / lustre / utils / lfs.c
index 3923ea0..edb5676 100644 (file)
@@ -115,29 +115,43 @@ 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);
 
-#define SETSTRIPE_USAGE(_cmd, _tgt) \
-       "usage: "_cmd" [--stripe-count|-c <stripe_count>]\n"\
-       "                 [--stripe-index|-i <start_ost_idx>]\n"\
-       "                 [--stripe-size|-S <stripe_size>]\n"\
-       "                 [--pool|-p <pool_name>]\n"\
-       "                 [--block|-b] "_tgt"\n"\
-       "                 [--ost-list|-o <ost_indices>]\n"\
-       "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n"\
-       "\t              Can be specified with k, m or g (in KB, MB and GB\n"\
-       "\t              respectively)\n"\
-       "\tstart_ost_idx: OST index of first stripe (-1 default)\n"\
-       "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"\
-       "\tpool_name:    Name of OST pool to use (default none)\n"\
-       "\tblock:        Block file access during data migration\n"\
+/* Setstripe and migrate share mostly the same parameters */
+#define SSM_CMD_COMMON(cmd) \
+       "usage: "cmd" [--stripe-count|-c <stripe_count>]\n"             \
+       "                 [--stripe-index|-i <start_ost_idx>]\n"        \
+       "                 [--stripe-size|-S <stripe_size>]\n"           \
+       "                 [--pool|-p <pool_name>]\n"                    \
+       "                 [--ost-list|-o <ost_indices>]\n"
+
+#define SSM_HELP_COMMON \
+       "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n" \
+       "\t              Can be specified with k, m or g (in KB, MB and GB\n" \
+       "\t              respectively)\n"                               \
+       "\tstart_ost_idx: OST index of first stripe (-1 default)\n"     \
+       "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n" \
+       "\tpool_name:    Name of OST pool to use (default none)\n"      \
        "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
-       "\t              Indices be specified in a format of:\n"\
-       "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"\
-       "\t              Or:\n"\
-       "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"\
-       "\t              If --pool is set with --ost-list, then the OSTs\n"\
+       "\t              Indices be specified in a format of:\n"        \
+       "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"        \
+       "\t              Or:\n"                                         \
+       "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"  \
+       "\t              If --pool is set with --ost-list, then the OSTs\n" \
        "\t              must be the members of the pool."
 
-/* all avaialable commands */
+#define SETSTRIPE_USAGE                                                \
+       SSM_CMD_COMMON("setstripe")                             \
+       "                 <directory|filename>\n"               \
+       SSM_HELP_COMMON
+
+#define MIGRATE_USAGE                                                  \
+       SSM_CMD_COMMON("migrate  ")                                     \
+       "                 [--block|-b]\n"                               \
+       "                 <filename>\n"                                 \
+       SSM_HELP_COMMON                                                 \
+       "\n"                                                            \
+       "\tblock:        Block file access during data migration\n"     \
+
+/* all available commands */
 command_t cmdlist[] = {
        {"setstripe", lfs_setstripe, 0,
         "Create a new file with a specific striping pattern or\n"
@@ -145,7 +159,7 @@ command_t cmdlist[] = {
         "delete the default striping pattern from an existing directory\n"
         "usage: setstripe -d <directory>   (to delete default striping)\n"\
         " or\n"
-        SETSTRIPE_USAGE("setstripe", "<directory|filename>")},
+        SETSTRIPE_USAGE},
        {"getstripe", lfs_getstripe, 0,
         "To list the striping info for a given file or files in a\n"
         "directory or recursively for all files in a directory tree.\n"
@@ -339,8 +353,8 @@ command_t cmdlist[] = {
        {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
         "usage: swap_layouts <path1> <path2>"},
        {"migrate", lfs_setstripe, 0, "migrate file from one OST layout to "
-        "another (may be not safe with concurent writes).\n"
-        SETSTRIPE_USAGE("migrate  ", "<filename>")},
+        "another (may be not safe with concurrent writes).\n"
+        MIGRATE_USAGE},
        {"mv", lfs_mv, 0,
         "To move directories between MDTs.\n"
         "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
@@ -353,41 +367,6 @@ command_t cmdlist[] = {
        { 0, 0, 0, NULL }
 };
 
-/* Generate a random id for the grouplock */
-static int random_group_id(int *gid)
-{
-       int     fd;
-       int     rc;
-       size_t  sz = sizeof(*gid);
-
-       fd = open("/dev/urandom", O_RDONLY);
-       if (fd < 0) {
-               rc = -errno;
-               fprintf(stderr, "cannot open /dev/urandom: %s\n",
-                       strerror(-rc));
-               goto out;
-       }
-
-retry:
-       rc = read(fd, gid, sz);
-       if (rc < sz) {
-               rc = -errno;
-               fprintf(stderr, "cannot read %zu bytes from /dev/urandom: %s\n",
-                       sz, strerror(-rc));
-               goto out;
-       }
-
-       /* gids must be non-zero */
-       if (*gid == 0)
-               goto retry;
-
-out:
-       if (fd >= 0)
-               close(fd);
-
-       return rc;
-}
-
 #define MIGRATION_BLOCKS 1
 
 static int lfs_migrate(char *name, __u64 migration_flags,
@@ -419,9 +398,9 @@ static int lfs_migrate(char *name, __u64 migration_flags,
        }
 
        rc = llapi_file_get_stripe(name, lum);
-       /* failure can come from may case and some may be not real error
+       /* failure can happen for many reasons and some may be not real errors
         * (eg: no stripe)
-        * in case of a real error, a later call will failed with a better
+        * in case of a real error, a later call will fail with better
         * error management */
        if (rc < 0)
                bufsz = 1024*1024;
@@ -433,15 +412,6 @@ static int lfs_migrate(char *name, __u64 migration_flags,
                goto free;
        }
 
-       if (migration_flags & MIGRATION_BLOCKS) {
-               rc = random_group_id(&gid);
-               if (rc < 0) {
-                       fprintf(stderr, "%s: cannot get random group ID: %s\n",
-                               name, strerror(-rc));
-                       goto free;
-               }
-       }
-
        /* search for file directory pathname */
        if (strlen(name) > sizeof(parent)-1) {
                rc = -E2BIG;
@@ -526,8 +496,11 @@ static int lfs_migrate(char *name, __u64 migration_flags,
                goto error;
        }
 
+       do
+               gid = random();
+       while (gid == 0);
        if (migration_flags & MIGRATION_BLOCKS) {
-               /* take group lock to limit concurent access
+               /* take group lock to limit concurrent access
                 * this will be no more needed when exclusive access will
                 * be implemented (see LU-2919) */
                /* group lock is taken after data version read because it
@@ -774,7 +747,7 @@ static int lfs_setstripe(int argc, char **argv)
        if (strcmp(argv[0], "migrate") == 0)
                migrate_mode = true;
 
-       while ((c = getopt_long(argc, argv, "c:di:o:p:s:S:",
+       while ((c = getopt_long(argc, argv, "bc:di:o:p:s:S:",
                                long_opts, NULL)) >= 0) {
                switch (c) {
                case 0:
@@ -1669,12 +1642,14 @@ static int lfs_setdirstripe(int argc, char **argv)
        char                    *stripe_count_opt = NULL;
        char                    *stripe_hash_opt = NULL;
        char                    *mode_opt = NULL;
-       int                     default_stripe = 0;
+       bool                    default_stripe = false;
        mode_t                  mode = S_IRWXU | S_IRWXG | S_IRWXO;
        mode_t                  previous_mode = 0;
+       bool                    delete = false;
 
        struct option long_opts[] = {
                {"count",       required_argument, 0, 'c'},
+               {"delete",      no_argument, 0, 'd'},
                {"index",       required_argument, 0, 'i'},
                {"mode",        required_argument, 0, 'm'},
                {"hash-type",   required_argument, 0, 't'},
@@ -1682,7 +1657,7 @@ static int lfs_setdirstripe(int argc, char **argv)
                {0, 0, 0, 0}
        };
 
-       while ((c = getopt_long(argc, argv, "c:Di:m:t:", long_opts,
+       while ((c = getopt_long(argc, argv, "c:dDi:m:t:", long_opts,
                                NULL)) >= 0) {
                switch (c) {
                case 0:
@@ -1691,8 +1666,12 @@ static int lfs_setdirstripe(int argc, char **argv)
                case 'c':
                        stripe_count_opt = optarg;
                        break;
+               case 'd':
+                       delete = true;
+                       default_stripe = true;
+                       break;
                case 'D':
-                       default_stripe = 1;
+                       default_stripe = true;
                        break;
                case 'i':
                        stripe_offset_opt = optarg;
@@ -1717,7 +1696,7 @@ static int lfs_setdirstripe(int argc, char **argv)
                return CMD_HELP;
        }
 
-       if (stripe_offset_opt == NULL && stripe_count_opt == NULL) {
+       if (!delete && stripe_offset_opt == NULL && stripe_count_opt == NULL) {
                fprintf(stderr, "error: %s: missing stripe offset and count.\n",
                        argv[0]);
                return CMD_HELP;
@@ -1733,6 +1712,17 @@ static int lfs_setdirstripe(int argc, char **argv)
                }
        }
 
+       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]);
+                       return CMD_HELP;
+               } else {
+                       stripe_count = 0;
+               }
+       }
+
+
        if (mode_opt != NULL) {
                mode = strtoul(mode_opt, &end, 8);
                if (*end != '\0') {
@@ -1766,7 +1756,7 @@ static int lfs_setdirstripe(int argc, char **argv)
 
        dname = argv[optind];
        do {
-               if (default_stripe == 1) {
+               if (default_stripe) {
                        result = llapi_dir_set_default_lmv_stripe(dname,
                                                    stripe_offset, stripe_count,
                                                    hash_type, NULL);
@@ -1828,7 +1818,7 @@ static int lfs_mv(int argc, char **argv)
        int     c;
        int     rc = 0;
        struct option long_opts[] = {
-               {"--mdt-index", required_argument, 0, 'M'},
+               {"mdt-index", required_argument, 0, 'M'},
                {"verbose",     no_argument,       0, 'v'},
                {0, 0, 0, 0}
        };
@@ -1856,20 +1846,21 @@ static int lfs_mv(int argc, char **argv)
        }
 
        if (param.fp_mdt_index == -1) {
-               fprintf(stderr, "%s MDT index must be indicated\n", argv[0]);
+               fprintf(stderr, "%s: MDT index must be specified\n", argv[0]);
                return CMD_HELP;
        }
 
        if (optind >= argc) {
-               fprintf(stderr, "%s missing operand path\n", argv[0]);
+               fprintf(stderr, "%s: missing operand path\n", argv[0]);
                return CMD_HELP;
        }
 
        param.fp_migrate = 1;
        rc = llapi_mv(argv[optind], &param);
        if (rc != 0)
-               fprintf(stderr, "cannot migrate '%s' to MDT%04x: %s\n",
-                       argv[optind], param.fp_mdt_index, strerror(-rc));
+               fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
+                       argv[0], argv[optind], param.fp_mdt_index,
+                       strerror(-rc));
        return rc;
 }
 
@@ -3773,12 +3764,15 @@ static int lfs_hsm_action(int argc, char **argv)
 
                if ((hps == HPS_RUNNING) &&
                    (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
-                       printf("("LPX64 " bytes moved)\n", he.length);
+                       printf("(%llu bytes moved)\n",
+                              (unsigned long long)he.length);
                else if ((he.offset + he.length) == LUSTRE_EOF)
-                       printf("(from "LPX64 " to EOF)\n", he.offset);
+                       printf("(from %llu to EOF)\n",
+                              (unsigned long long)he.offset);
                else
-                       printf("(from "LPX64 " to "LPX64")\n",
-                              he.offset, he.offset + he.length);
+                       printf("(from %llu to %llu)\n",
+                              (unsigned long long)he.offset,
+                              (unsigned long long)(he.offset + he.length));
 
        } while (++i < argc);
 
@@ -3951,8 +3945,8 @@ static int lfs_hsm_request(int argc, char **argv, int action)
                while ((rc = getline(&line, &len, fp)) != -1) {
                        struct hsm_user_item *hui;
 
-                       /* If allocated buffer was too small, gets something
-                        * bigger */
+                       /* If allocated buffer was too small, get something
+                        * larger */
                        if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
                                ssize_t size;
                                nbfile_alloc = nbfile_alloc * 2 + 1;
@@ -4070,6 +4064,10 @@ int main(int argc, char **argv)
 {
         int rc;
 
+       /* Ensure that liblustreapi constructor has run */
+       if (!liblustreapi_initialized)
+               fprintf(stderr, "liblustreapi was not properly initialized\n");
+
         setlinebuf(stdout);
 
        Parser_init("lfs > ", cmdlist);