Whamcloud - gitweb
LU-12396 utils: lfs should not output 'nul' char
[fs/lustre-release.git] / lustre / utils / lfs.c
index 02bad77..4f21ced 100644 (file)
@@ -128,6 +128,7 @@ static inline int lfs_mirror_read(int argc, char **argv);
 static inline int lfs_mirror_write(int argc, char **argv);
 static inline int lfs_mirror_copy(int argc, char **argv);
 static int lfs_pcc_attach(int argc, char **argv);
+static int lfs_pcc_attach_fid(int argc, char **argv);
 static int lfs_pcc_detach(int argc, char **argv);
 static int lfs_pcc_detach_fid(int argc, char **argv);
 static int lfs_pcc_state(int argc, char **argv);
@@ -339,6 +340,12 @@ command_t pcc_cmdlist[] = {
          .pc_help = "Attach given files to the Persistent Client Cache.\n"
                "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
                "\t-i: archive id for RW-PCC\n" },
+       { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
+         .pc_help = "Attach given files into PCC by FID(s).\n"
+               "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
+               "<fid> ...\n"
+               "\t-i: archive id for RW-PCC\n"
+               "\t-m: Lustre mount point\n" },
        { .pc_name = "state", .pc_func = lfs_pcc_state,
          .pc_help = "Display the PCC state for given files.\n"
                "usage: lfs pcc state <file> ...\n" },
@@ -664,6 +671,7 @@ command_t cmdlist[] = {
        {"pcc", lfs_pcc, pcc_cmdlist,
         "lfs commands used to interact with PCC features:\n"
         "lfs pcc attach - attach given files to Persistent Client Cache\n"
+        "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
         "lfs pcc state  - display the PCC state for given files\n"
         "lfs pcc detach - detach given files from Persistent Client Cache\n"
         "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
@@ -4934,9 +4942,6 @@ static char obd_statfs_state_names[] = {
 
 static char obd_statfs_state2char(int s)
 {
-       /* Not an error state, do not print here  */
-       if (s == OS_STATE_NONROT)
-               return '\0';
        /* Unknown name */
        if (s > ARRAY_SIZE(obd_statfs_state_names)/sizeof(char) ||
            obd_statfs_state_names[s] == 0)
@@ -5024,7 +5029,7 @@ static int showdf(char *mntdir, struct obd_statfs *stat,
                        printf(" ");
                        for (i = 0, state = stat->os_state; state != 0; i++) {
                                uint32_t mask = 1 << i;
-                               if (!(state & mask))
+                               if (!(state & mask) || mask == OS_STATE_NONROT)
                                        continue;
                                printf("%c", obd_statfs_state2char(mask));
                                state &= ~mask;
@@ -5361,7 +5366,6 @@ static int lfs_setdirstripe(int argc, char **argv)
                                        "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
                                        progname, argv[0]);
 #endif
-                       lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
                        lsa.lsa_nr_tgts = parse_targets(mdts,
                                                sizeof(mdts) / sizeof(__u32),
                                                lsa.lsa_nr_tgts, optarg, NULL);
@@ -5371,7 +5375,6 @@ static int lfs_setdirstripe(int argc, char **argv)
                                        progname, argv[0], optarg);
                                return CMD_HELP;
                        }
-                       lsa.lsa_pattern = 0;
 
                        lsa.lsa_tgts = mdts;
                        if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
@@ -10433,20 +10436,107 @@ static int lfs_pcc_attach(int argc, char **argv)
        return rc;
 }
 
-static int lfs_pcc_detach(int argc, char **argv)
+static int lfs_pcc_attach_fid(int argc, char **argv)
 {
+       struct option long_opts[] = {
+       { .val = 'i',   .name = "id",   .has_arg = required_argument },
+       { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
+       { .name = NULL } };
+       char                     short_opts[] = "i:m:";
+       int                      c;
        int                      rc = 0;
-       const char              *path;
-       char                     fullpath[PATH_MAX];
+       __u32                    archive_id = 0;
+       char                    *end;
+       const char              *mntpath = NULL;
+       const char              *fidstr;
+       enum lu_pcc_type         type = LU_PCC_READWRITE;
 
-       optind = 1;
+       optind = 0;
+       while ((c = getopt_long(argc, argv, short_opts,
+                               long_opts, NULL)) != -1) {
+               switch (c) {
+               case 'i':
+                       archive_id = strtoul(optarg, &end, 0);
+                       if (*end != '\0') {
+                               fprintf(stderr, "error: %s: bad archive ID "
+                                       "'%s'\n", argv[0], optarg);
+                               return CMD_HELP;
+                       }
+                       break;
+               case 'm':
+                       mntpath = optarg;
+                       break;
+               case '?':
+                       return CMD_HELP;
+               default:
+                       fprintf(stderr, "%s: option '%s' unrecognized\n",
+                               argv[0], argv[optind - 1]);
+                       return CMD_HELP;
+               }
+       }
 
-       if (argc <= 1) {
-               fprintf(stderr, "%s: must specify one or more file names\n",
+       if (archive_id == 0) {
+               fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
+               return CMD_HELP;
+       }
+
+       if (mntpath == NULL) {
+               fprintf(stderr, "%s: must specify Lustre mount point\n",
                        argv[0]);
                return CMD_HELP;
        }
 
+       if (argc <= optind) {
+               fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
+               return CMD_HELP;
+       }
+
+       while (optind < argc) {
+               int rc2;
+
+               fidstr = argv[optind++];
+
+               rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
+                                              archive_id, type);
+               if (rc2 < 0) {
+                       fprintf(stderr, "%s: cannot attach '%s' on '%s' to PCC "
+                               "with archive ID '%u': %s\n", argv[0],
+                               fidstr, mntpath, archive_id, strerror(rc2));
+               }
+               if (rc == 0 && rc2 < 0)
+                       rc = rc2;
+       }
+       return rc;
+}
+
+static int lfs_pcc_detach(int argc, char **argv)
+{
+       struct option long_opts[] = {
+       { .val = 'k',   .name = "keep", .has_arg = no_argument },
+       { .name = NULL } };
+       char                     short_opts[] = "k";
+       int                      c;
+       int                      rc = 0;
+       const char              *path;
+       char                     fullpath[PATH_MAX];
+       __u32                    detach_opt = PCC_DETACH_OPT_UNCACHE;
+
+       optind = 0;
+       while ((c = getopt_long(argc, argv, short_opts,
+                               long_opts, NULL)) != -1) {
+               switch (c) {
+               case 'k':
+                       detach_opt = PCC_DETACH_OPT_NONE;
+                       break;
+               case '?':
+                       return CMD_HELP;
+               default:
+                       fprintf(stderr, "%s: option '%s' unrecognized\n",
+                               argv[0], argv[optind - 1]);
+                       return CMD_HELP;
+               }
+       }
+
        while (optind < argc) {
                int rc2;
 
@@ -10459,10 +10549,11 @@ static int lfs_pcc_detach(int argc, char **argv)
                        continue;
                }
 
-               rc2 = llapi_pcc_detach_file(fullpath);
+               rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
                if (rc2 < 0) {
+                       rc2 = -errno;
                        fprintf(stderr, "%s: cannot detach '%s' from PCC: "
-                               "%s\n", argv[0], path, strerror(-rc2));
+                               "%s\n", argv[0], path, strerror(errno));
                        if (rc == 0)
                                rc = rc2;
                }
@@ -10472,16 +10563,30 @@ static int lfs_pcc_detach(int argc, char **argv)
 
 static int lfs_pcc_detach_fid(int argc, char **argv)
 {
+       struct option long_opts[] = {
+       { .val = 'k',   .name = "keep", .has_arg = no_argument },
+       { .name = NULL } };
+       char             short_opts[] = "k";
+       int              c;
        int              rc = 0;
        const char      *fid;
        const char      *mntpath;
+       __u32            detach_opt = PCC_DETACH_OPT_UNCACHE;
 
-       optind = 1;
-
-       if (argc <= 2) {
-               fprintf(stderr, "%s: not enough argument\n",
-                       argv[0]);
-               return CMD_HELP;
+       optind = 0;
+       while ((c = getopt_long(argc, argv, short_opts,
+                               long_opts, NULL)) != -1) {
+               switch (c) {
+               case 'k':
+                       detach_opt = PCC_DETACH_OPT_NONE;
+                       break;
+               case '?':
+                       return CMD_HELP;
+               default:
+                       fprintf(stderr, "%s: option '%s' unrecognized\n",
+                               argv[0], argv[optind - 1]);
+                       return CMD_HELP;
+               }
        }
 
        mntpath = argv[optind++];
@@ -10491,7 +10596,7 @@ static int lfs_pcc_detach_fid(int argc, char **argv)
 
                fid = argv[optind++];
 
-               rc2 = llapi_pcc_detach_fid_str(mntpath, fid);
+               rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
                if (rc2 < 0) {
                        fprintf(stderr, "%s: cannot detach '%s' on '%s' "
                                "from PCC: %s\n", argv[0], fid, mntpath,
@@ -10549,9 +10654,7 @@ static int lfs_pcc_state(int argc, char **argv)
 
                printf(", PCC file: %s", state.pccs_path);
                printf(", user number: %u", state.pccs_open_count);
-               printf(", attr cached: %s",
-                      state.pccs_flags &  PCC_STATE_FLAG_ATTR_VALID ?
-                      "true" : "false");
+               printf(", flags: %x", state.pccs_flags);
                printf("\n");
        }
        return rc;