Whamcloud - gitweb
land b_colibri_devel on HEAD:
[fs/lustre-release.git] / lustre / utils / lfs.c
index 6466cae..8d8d0d8 100644 (file)
@@ -23,7 +23,6 @@
  *
  */
 
-
 #include <stdlib.h>
 #include <stdio.h>
 #include <getopt.h>
@@ -38,6 +37,8 @@
 #include <dirent.h>
 #include <time.h>
 #include <ctype.h>
+/* For dirname() */
+#include <libgen.h>
 
 #include <lnet/api-support.h>
 #include <lnet/lnetctl.h>
@@ -68,7 +69,14 @@ static int lfs_quotaoff(int argc, char **argv);
 static int lfs_setquota(int argc, char **argv);
 static int lfs_quota(int argc, char **argv);
 #endif
+static int lfs_flushctx(int argc, char **argv);
 static int lfs_join(int argc, char **argv);
+static int lfs_lsetfacl(int argc, char **argv);
+static int lfs_lgetfacl(int argc, char **argv);
+static int lfs_rsetfacl(int argc, char **argv);
+static int lfs_rgetfacl(int argc, char **argv);
+static int lfs_cp(int argc, char **argv);
+static int lfs_ls(int argc, char **argv);
 
 /* all avaialable commands */
 command_t cmdlist[] = {
@@ -84,6 +92,7 @@ command_t cmdlist[] = {
          "       or \n"
          "       setstripe -d <dirname>   (to delete default striping)\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 respectively)\n"
          "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
          "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
         {"getstripe", lfs_getstripe, 0,
@@ -94,15 +103,12 @@ command_t cmdlist[] = {
         {"find", lfs_find, 0,
          "To find files that match given parameters recursively in a directory tree.\n"
          "usage: find <dir/file> ... \n"
-         "     [[!] --atime|-A N] [[!] --mtime|-M N] [[!] --ctime|-C N] [--maxdepth|-D N]\n"
-         "     [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
-         "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
-         "\t !: used before --name means find exclude the regular expression pattern\n"
-         "If one of the options below is provided, find works the same as 'getstripe':\n"
-         "To list the striping info for a given filename or files in a directory or\n"
-         "recursively.\n"
-         "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
-         "                     [--recursive | -r] <dir|file> ..."},
+         "     [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
+         "     [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
+         "     [--print|-p] [--obd|-O <uuid>] [[!] --type|-t <filetype>]\n"
+         "\t !: used before an option indicates 'NOT' the requested attribute\n"
+         "\t -: used before an value indicates 'AT MOST' the requested value\n"
+         "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
         {"check", lfs_check, 0,
          "Display the status of MDS or OSTs (as specified in the command)\n"
          "or all the servers (MDS and OSTs).\n"
@@ -137,8 +143,28 @@ command_t cmdlist[] = {
          "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
          "       setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
-         "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
+         "usage: quota [ -o obd_uuid ] [{-u|-g  <name>}|-t] <filesystem>"},
 #endif
+        {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
+         "usage: flushctx [-k] [mountpoint...]"},
+        {"lsetfacl", lfs_lsetfacl, 0,
+         "Remote user setfacl for user/group on the same remote client.\n"
+         "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
+        {"lgetfacl", lfs_lgetfacl, 0,
+         "Remote user getfacl for user/group on the same remote client.\n"
+         "usage: lgetfacl [-dRLPvh] file ..."},
+        {"rsetfacl", lfs_rsetfacl, 0,
+         "Remote user setfacl for user/group on other clients.\n"
+         "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
+        {"rgetfacl", lfs_rgetfacl, 0,
+         "Remote user getfacl for user/group on other clients.\n"
+         "usage: rgetfacl [-dRLPvh] file ..."},
+        {"cp", lfs_cp, 0,
+         "Remote user copy files and directories.\n"
+         "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
+        {"ls", lfs_ls, 0,
+         "Remote user list directory contents.\n"
+         "usage: ls [OPTION]... [FILE]..."},
         {"help", Parser_help, 0, "help"},
         {"exit", Parser_quit, 0, "quit"},
         {"quit", Parser_quit, 0, "quit"},
@@ -150,7 +176,7 @@ static int lfs_setstripe(int argc, char **argv)
 {
         char *fname;
         int result;
-        long st_size;
+        unsigned long long st_size;
         int  st_offset, st_count;
         char *end;
         int c;
@@ -158,6 +184,7 @@ static int lfs_setstripe(int argc, char **argv)
         char *stripe_size_arg = NULL;
         char *stripe_off_arg = NULL;
         char *stripe_count_arg = NULL;
+        unsigned long long size_units;
 
         struct option long_opts[] = {
                 {"size",        required_argument, 0, 's'},
@@ -216,12 +243,12 @@ static int lfs_setstripe(int argc, char **argv)
                                 return CMD_HELP;
                         }
                 }
-
                 if (optind < argc)
                         fname = argv[optind];
                 else
                         return CMD_HELP;
 
+
                 if (delete && 
                     (stripe_size_arg != NULL || stripe_off_arg != NULL || 
                      stripe_count_arg != NULL)) {
@@ -231,21 +258,20 @@ static int lfs_setstripe(int argc, char **argv)
                         return CMD_HELP;
                 }
         }
-
         if (optind != argc - 1) {
                 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
                                 "specified: '%s'\n",
-                                argv[0], argv[argc-1]);
+                                argv[0], argv[argc - 1]);
                 return CMD_HELP;
         }
 
         /* get the stripe size */
         if (stripe_size_arg != NULL) {
-                st_size = strtoul(stripe_size_arg, &end, 0);
-                if (*end != '\0') {
-                        fprintf(stderr, "error: %s: bad stripe size '%s'\n",
-                                        argv[0], stripe_size_arg);
-                        return CMD_HELP;
+                result = parse_size(stripe_size_arg, &st_size, &size_units);
+                if (result) {
+                        fprintf(stderr,"error: bad size '%s'\n",
+                                stripe_size_arg);
+                        return result;
                 }
         }
         /* get the stripe offset */
@@ -253,7 +279,7 @@ static int lfs_setstripe(int argc, char **argv)
                 st_offset = strtoul(stripe_off_arg, &end, 0);
                 if (*end != '\0') {
                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
-                                        argv[0], stripe_off_arg);
+                                argv[0], stripe_off_arg);
                         return CMD_HELP;
                 }
         }
@@ -262,7 +288,7 @@ static int lfs_setstripe(int argc, char **argv)
                 st_count = strtoul(stripe_count_arg, &end, 0);
                 if (*end != '\0') {
                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
-                                        argv[0], stripe_count_arg);
+                                argv[0], stripe_count_arg);
                         return CMD_HELP;
                 }
         }
@@ -304,13 +330,9 @@ static int lfs_find(int argc, char **argv)
 {
         int new_fashion = 1;
         int c, ret;
-        int zeroend;
         time_t t;
-        unsigned int depth;
-        int quiet, verbose, recursive;
-        struct find_param param;
-        struct obd_uuid *obduuid = NULL;
-        char timestr[1024];
+        struct find_param param = { .maxdepth = -1 };
+        char str[1024];
         struct option long_opts[] = {
                 /* New find options. */
                 {"atime",     required_argument, 0, 'A'},
@@ -326,6 +348,8 @@ static int lfs_find(int argc, char **argv)
                 /* Old find options. */
                 {"quiet",     no_argument,       0, 'q'},
                 {"recursive", no_argument,       0, 'r'},
+                {"size",      required_argument, 0, 's'},
+                {"type",      required_argument, 0, 't'},
                 {"verbose",   no_argument,       0, 'v'},
                 {0, 0, 0, 0}
         };
@@ -337,15 +361,9 @@ static int lfs_find(int argc, char **argv)
         int isoption;
 
         time(&t);
-        zeroend = 0;
-        depth = -1;
-        quiet = verbose = recursive = 0;
 
-        memset(&param, 0, sizeof(param));
-
-        while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpOqrv",
-                                     long_opts, NULL)) >= 0)
-        {
+        while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrs:t:v",
+                                     long_opts, NULL)) >= 0) {
                 xtime = NULL;
                 xsign = NULL;
                 if (neg_opt)
@@ -374,7 +392,7 @@ static int lfs_find(int argc, char **argv)
                 case 1:
                         if (strcmp(optarg, "!") == 0)
                                 neg_opt = 2;
-                      break;
+                        break;
                 case 'A':
                         xtime = &param.atime;
                         xsign = &param.asign;
@@ -395,10 +413,10 @@ static int lfs_find(int argc, char **argv)
                                 else if (optarg[0] == '+')
                                         optarg[0] = '-';
                                 else {
-                                        timestr[0] = '-';
-                                        timestr[1] = '\0';
-                                        strcat(timestr, optarg);
-                                        optarg = timestr;
+                                        str[0] = '-';
+                                        str[1] = '\0';
+                                        strcat(str, optarg);
+                                        optarg = str;
                                 }
                         }
                         ret = set_time(&t, xtime, optarg);
@@ -408,43 +426,111 @@ static int lfs_find(int argc, char **argv)
                                 *xsign = ret;
                         break;
                 case 'D':
-                        depth = strtol(optarg, 0, 0);
+                        new_fashion = 1;
+                        param.maxdepth = strtol(optarg, 0, 0);
                         break;
                 case 'n':
                         new_fashion = 1;
                         param.pattern = (char *)optarg;
-                        if (neg_opt)
-                                param.exclude_pattern = 1;
-                        else
-                                param.exclude_pattern = 0;
+                        param.exclude_pattern = !!neg_opt;
                         break;
-                case 'O':
-                        if (obduuid) {
-                                fprintf(stderr,
-                                        "error: %s: only one obduuid allowed",
-                                        argv[0]);
-                                return CMD_HELP;
+                case 'O': {
+                        char *buf, *token, *next, *p;
+                        int len;
+
+                        len = strlen((char *)optarg);
+                        buf = malloc(len+1);
+                        if (buf == NULL)
+                                return -ENOMEM;
+                        strcpy(buf, (char *)optarg);
+
+                        if (param.num_alloc_obds == 0) {
+                                param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
+                                                       sizeof(struct obd_uuid));
+                                if (param.obduuid == NULL)
+                                        return -ENOMEM;
+                                param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
+                        }
+
+                        for (token = buf; token && *token; token = next) {
+                                p = strchr(token, ',');
+                                next = 0;
+                                if (p) {
+                                        *p = 0;
+                                        next = p+1;
+                                }
+                                strcpy((char *)&param.obduuid[param.num_obds++].uuid,
+                                       token);
                         }
-                        obduuid = (struct obd_uuid *)optarg;
+
+                        if (buf)
+                                free(buf);
                         break;
+                }
                 case 'p':
-                        zeroend = 1;
+                        new_fashion = 1;
+                        param.zeroend = 1;
                         break;
                 case 'P':
                         break;
                 case 'q':
                         new_fashion = 0;
-                        quiet++;
-                        verbose = 0;
+                        param.quiet++;
+                        param.verbose = 0;
                         break;
                 case 'r':
                         new_fashion = 0;
-                        recursive = 1;
+                        param.recursive = 1;
+                        break;
+                case 't':
+                        param.exclude_type = !!neg_opt;
+                        switch(optarg[0]) {
+                        case 'b': param.type = S_IFBLK; break;
+                        case 'c': param.type = S_IFCHR; break;
+                        case 'd': param.type = S_IFDIR; break;
+                        case 'f': param.type = S_IFREG; break;
+                        case 'l': param.type = S_IFLNK; break;
+                        case 'p': param.type = S_IFIFO; break;
+                        case 's': param.type = S_IFSOCK; break;
+#ifdef S_IFDOOR /* Solaris only */
+                        case 'D': param.type = S_IFDOOR; break;
+#endif
+                        default: fprintf(stderr, "error: %s: bad type '%s'\n",
+                                         argv[0], optarg);
+                                 return CMD_HELP;
+                        };
+                        break;
+                case 's':
+                        if (neg_opt) {
+                                if (optarg[0] == '-')
+                                        optarg[0] = '+';
+                                else if (optarg[0] == '+')
+                                        optarg[0] = '-';
+                                else {
+                                        str[0] = '-';
+                                        str[1] = '\0';
+                                        strcat(str, optarg);
+                                        optarg = str;
+                                }
+                        }
+                        if (optarg[0] == '+')
+                                param.size_sign = -1;
+                        else if (optarg[0] == '-')
+                                param.size_sign = +1;
+
+                        if (param.size_sign)
+                                optarg++;
+                        ret = parse_size(optarg, &param.size,&param.size_units);
+                        if (ret) {
+                                fprintf(stderr,"error: bad size '%s'\n",
+                                        optarg);
+                                return ret;
+                        }
                         break;
                 case 'v':
                         new_fashion = 0;
-                        verbose++;
-                        quiet = 0;
+                        param.verbose++;
+                        param.quiet = 0;
                         break;
                 case '?':
                         return CMD_HELP;
@@ -454,28 +540,29 @@ static int lfs_find(int argc, char **argv)
                         return CMD_HELP;
                 };
         }
-
+        
         if (pathstart == -1) {
                 fprintf(stderr, "error: %s: no filename|pathname\n",
-                                argv[0]);
+                        argv[0]);
                 return CMD_HELP;
         } else if (pathend == -1) {
                 /* no options */
                 pathend = argc;
         }
 
-        param.obduuid = obduuid;
         if (new_fashion) {
-                param.maxdepth = depth;
-                param.zeroend = zeroend;
                 param.quiet = 1;
         } else {
-                param.recursive = recursive;
-                param.verbose = verbose;
-                param.quiet = quiet;
-                param.maxdepth = recursive ? -1 : 1;
+                static int deprecated_warning;
+                if (!deprecated_warning) {
+                        fprintf(stderr, "lfs find: -q, -r, -v options "
+                                "deprecated.  Use 'lfs getstripe' instead.\n");
+                        deprecated_warning = 1;
+                }
+                if (!param.recursive && param.maxdepth == -1)
+                        param.maxdepth = 1;
         }
-
+        
         do {
                 if (new_fashion)
                         ret = llapi_find(argv[pathstart], &param);
@@ -487,6 +574,9 @@ static int lfs_find(int argc, char **argv)
                 fprintf(stderr, "error: %s failed for %s.\n",
                         argv[0], argv[optind - 1]);
 
+        if (param.obduuid && param.num_alloc_obds)
+                free(param.obduuid);
+
         return ret;
 }
 
@@ -500,34 +590,32 @@ static int lfs_getstripe(int argc, char **argv)
                 {0, 0, 0, 0}
         };
         char short_opts[] = "hO:qrv";
-        int quiet, verbose, recursive, c, rc;
-        struct obd_uuid *obduuid = NULL;
-        struct find_param param;
+        int c, rc;
+        struct find_param param = { 0 };
 
         optind = 0;
-        quiet = verbose = recursive = 0;
         while ((c = getopt_long(argc, argv, short_opts,
                                 long_opts, NULL)) != -1) {
                 switch (c) {
                 case 'O':
-                        if (obduuid) {
+                        if (param.obduuid) {
                                 fprintf(stderr,
                                         "error: %s: only one obduuid allowed",
                                         argv[0]);
                                 return CMD_HELP;
                         }
-                        obduuid = (struct obd_uuid *)optarg;
+                        param.obduuid = (struct obd_uuid *)optarg;
                         break;
                 case 'q':
-                        quiet++;
-                        verbose = 0;
+                        param.quiet++;
+                        param.verbose = 0;
                         break;
                 case 'r':
-                        recursive = 1;
+                        param.recursive = 1;
                         break;
                 case 'v':
-                        verbose++;
-                        quiet = 0;
+                        param.verbose++;
+                        param.quiet = 0;
                         break;
                 case '?':
                         return CMD_HELP;
@@ -541,12 +629,7 @@ static int lfs_getstripe(int argc, char **argv)
         if (optind >= argc)
                 return CMD_HELP;
 
-        memset(&param, 0, sizeof(param));
-        param.recursive = recursive;
-        param.verbose = verbose;
-        param.quiet = quiet;
-        param.obduuid = obduuid;
-        param.maxdepth = recursive ? -1 : 1;
+        param.maxdepth = param.recursive ? -1 : 1;
 
         do {
                 rc = llapi_getstripe(argv[optind], &param);
@@ -668,8 +751,8 @@ static int showdf(char *mntdir, struct obd_statfs *stat,
                         int shift = cooked ? 0 : 10;
 
                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
-                        used = stat->os_blocks - stat->os_bavail;
-                        used = (used * stat->os_bsize) >> shift;
+                        used  = ((stat->os_blocks - stat->os_bfree) *
+                                 stat->os_bsize) >> shift;
                         total = (stat->os_blocks * stat->os_bsize) >> shift;
                 }
 
@@ -755,12 +838,12 @@ static int mntdf(char *mntdir, int ishow, int cooked)
 
                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
                     rc == -ENODATA || rc == 0) {
-                        showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
-                               ishow, cooked, "MDT", index, rc);
+                        showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
+                               "MDT", index, rc);
                 } else {
                         fprintf(stderr,
                                 "error: llapi_obd_statfs(%s): %s (%d)\n",
-                                obd_uuid2str(&uuid_buf), strerror(-rc), rc);
+                                uuid_buf.uuid, strerror(-rc), rc);
                         return rc;
                 }
                 if (rc == 0) {
@@ -782,8 +865,8 @@ static int mntdf(char *mntdir, int ishow, int cooked)
 
                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
                     rc == -ENODATA || rc == 0) {
-                        showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
-                               ishow, cooked, "OST", index, rc);
+                        showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
+                               "OST", index, rc);
                 } else {
                         fprintf(stderr,
                                 "error: llapi_obd_statfs failed: %s (%d)\n",
@@ -1026,7 +1109,8 @@ static int lfs_quotacheck(int argc, char **argv)
         char *mnt;
         struct if_quotacheck qchk;
         struct if_quotactl qctl;
-        char *obd_type = (char *)qchk.obd_type;
+        char *obd_type = qchk.obd_type;
+        char *obd_uuid = qchk.obd_uuid.uuid;
         int rc;
 
         memset(&qchk, 0, sizeof(qchk));
@@ -1076,8 +1160,7 @@ static int lfs_quotacheck(int argc, char **argv)
         rc = llapi_poll_quotacheck(mnt, &qchk);
         if (rc) {
                 if (*obd_type)
-                        fprintf(stderr, "%s %s ", obd_type,
-                                obd_uuid2str(&qchk.obd_uuid));
+                        fprintf(stderr, "%s %s ", obd_type, obd_uuid);
                 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
                 return rc;
         }
@@ -1089,8 +1172,8 @@ static int lfs_quotacheck(int argc, char **argv)
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
                 if (*obd_type)
-                        fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
-                                obd_uuid2str(&qctl.obd_uuid));
+                        fprintf(stderr, "%s %s ",
+                                qctl.obd_type, qctl.obd_uuid.uuid);
                 fprintf(stderr, "%s turn on quota failed: %s\n",
                         argv[0], strerror(errno));
                 return rc;
@@ -1104,7 +1187,8 @@ static int lfs_quotaon(int argc, char **argv)
         int c;
         char *mnt;
         struct if_quotactl qctl;
-        char *obd_type = (char *)qctl.obd_type;
+        char *obd_type = qctl.obd_type;
+        char *obd_uuid = qctl.obd_uuid.uuid;
         int rc;
 
         memset(&qctl, 0, sizeof(qctl));
@@ -1132,6 +1216,8 @@ static int lfs_quotaon(int argc, char **argv)
 
         if (qctl.qc_type)
                 qctl.qc_type--;
+        else /* by default, enable quota for both user & group */
+                qctl.qc_type = 0x02;
 
         if (argc == optind)
                 return CMD_HELP;
@@ -1141,8 +1227,7 @@ static int lfs_quotaon(int argc, char **argv)
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
                 if (*obd_type)
-                        fprintf(stderr, "%s %s ", obd_type,
-                                obd_uuid2str(&qctl.obd_uuid));
+                        fprintf(stderr, "%s %s ", obd_type, obd_uuid);
                 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
                 return rc;
         }
@@ -1155,7 +1240,8 @@ static int lfs_quotaoff(int argc, char **argv)
         int c;
         char *mnt;
         struct if_quotactl qctl;
-        char *obd_type = (char *)qctl.obd_type;
+        char *obd_type = qctl.obd_type;
+        char *obd_uuid = qctl.obd_uuid.uuid;
         int rc;
 
         memset(&qctl, 0, sizeof(qctl));
@@ -1179,6 +1265,8 @@ static int lfs_quotaoff(int argc, char **argv)
 
         if (qctl.qc_type)
                 qctl.qc_type--;
+        else /* by default, disable quota for both user & group */
+                qctl.qc_type = 0x02;
 
         if (argc == optind)
                 return CMD_HELP;
@@ -1188,8 +1276,7 @@ static int lfs_quotaoff(int argc, char **argv)
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
                 if (*obd_type)
-                        fprintf(stderr, "%s %s ", obd_type,
-                                obd_uuid2str(&qctl.obd_uuid));
+                        fprintf(stderr, "%s %s ", obd_type, obd_uuid);
                 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
                 return rc;
         }
@@ -1261,12 +1348,72 @@ do {                                                                    \
         }                                                               \
 } while (0)
 
+#define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
+
+/* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
+ * returns the value or ULONG_MAX on integer overflow or incorrect format
+ * Notes:
+ *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
+ *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
+ *        3. empty integer value is interpreted as 0
+ */
+static unsigned long str2sec(const char* timestr) {
+        const char spec[] = "smhdw";
+        const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
+        unsigned long val = 0;
+        char *tail;
+
+        if (strpbrk(timestr, spec) == NULL) {
+                /* no specifiers inside the time string,
+                   should treat it as an integer value */
+                val = strtoul(timestr, &tail, 10);
+                return *tail ? ULONG_MAX : val;
+        }
+
+        /* format string is XXwXXdXXhXXmXXs */
+        while (*timestr) {
+                unsigned long v;
+                int ind;
+                char* ptr;
+
+                v = strtoul(timestr, &tail, 10);
+                if (v == ULONG_MAX || *tail == '\0')
+                        /* value too large (ULONG_MAX or more) 
+                           or missing specifier */
+                        goto error;
+
+                ptr = strchr(spec, *tail);
+                if (ptr == NULL)
+                        /* unknown specifier */
+                        goto error;
+
+                ind = ptr - spec;
+
+                /* check if product will overflow the type */
+                if (!(v < ULONG_MAX / mult[ind]))
+                        goto error;
+
+                ADD_OVERFLOW(val, mult[ind] * v);
+                if (val == ULONG_MAX)
+                        goto error;
+
+                timestr = tail + 1;
+        }
+
+        return val;
+
+error:
+        return ULONG_MAX;
+}
+
 int lfs_setquota(int argc, char **argv)
 {
         int c;
         char *mnt;
         struct if_quotactl qctl;
-        char *obd_type = (char *)qctl.obd_type;
+        char *obd_type = qctl.obd_type;
+        char *obd_uuid = qctl.obd_uuid.uuid;
         int rc;
 
         memset(&qctl, 0, sizeof(qctl));
@@ -1325,8 +1472,14 @@ int lfs_setquota(int argc, char **argv)
                 if (optind + 3 != argc)
                         return CMD_HELP;
 
-                ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
-                ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
+                if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
+                        fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
+                        return CMD_HELP;
+                }
+                if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
+                        fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
+                        return CMD_HELP;
+                }
         }
 
         mnt = argv[optind];
@@ -1334,8 +1487,7 @@ int lfs_setquota(int argc, char **argv)
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
                 if (*obd_type)
-                        fprintf(stderr, "%s %s ", obd_type,
-                                obd_uuid2str(&qctl.obd_uuid));
+                        fprintf(stderr, "%s %s ", obd_type, obd_uuid);
                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
                 return rc;
         }
@@ -1354,19 +1506,29 @@ static inline char *type2name(int check_type)
 }
 
 
-static void grace2str(time_t seconds,char *buf)
+/* Converts seconds value into format string
+ * result is returned in buf
+ * Notes:
+ *        1. result is in descenting order: 1w2d3h4m5s
+ *        2. zero fields are not filled (except for p. 3): 5d1s
+ *        3. zero seconds value is presented as "0s"
+ */
+static void sec2str(time_t seconds, char *buf)
 {
-        uint minutes, hours, days;
-
-        minutes = (seconds + 30) / 60;
-        hours = minutes / 60;
-        minutes %= 60;
-        days = hours / 24;
-        hours %= 24;
-        if (days >= 2)
-                snprintf(buf, 40, "%ddays", days);
-        else
-                snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
+        const char spec[] = "smhdw";
+        const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
+        unsigned long c;
+        char* tail = buf;
+        int i;
+
+        for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
+                c = seconds / mult[i];
+
+                if (c > 0 || (i == 0 && buf == tail))
+                        tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
+
+                seconds %= mult[i];
+        }
 }
 
 
@@ -1380,7 +1542,7 @@ static void diff2str(time_t seconds, char *buf, time_t now)
                 strcpy(buf, "none");
                 return;
         }
-        grace2str(seconds - now, buf);
+        sec2str(seconds - now, buf);
 }
 
 static void print_quota_title(char *name, struct if_quotactl *qctl)
@@ -1390,7 +1552,7 @@ static void print_quota_title(char *name, struct if_quotactl *qctl)
                *type2name(qctl->qc_type), qctl->qc_id);
         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
                "Filesystem",
-               "blocks", "quota", "limit", "grace",
+               "kbytes", "quota", "limit", "grace",
                "files", "quota", "limit", "grace");
 }
 
@@ -1467,8 +1629,8 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
                 char bgtimebuf[40];
                 char igtimebuf[40];
 
-                grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
-                grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
+                sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
+                sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
                 printf("Block grace time: %s; Inode grace time: %s\n",
                        bgtimebuf, igtimebuf);
         }
@@ -1487,13 +1649,13 @@ static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
         }
         qctl->qc_dqblk.dqb_valid = 0;
 
-        print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, 0);
+        print_quota(qctl->obd_uuid.uuid, qctl, 0);
 }
 
 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
 {
         DIR *dir;
-        struct obd_uuid uuids[1024], *uuidp;
+        struct obd_uuid *uuids = NULL, *uuidp;
         int obdcount = 1024;
         int i, rc;
 
@@ -1503,9 +1665,26 @@ static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
                 return;
         }
 
+        uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
+                                          sizeof(struct obd_uuid));
+        if (uuids == NULL)
+                goto out;
+
+retry_get_uuids:
         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
         if (rc != 0) {
-                fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
+                struct obd_uuid *uuids_temp;
+
+                if (rc == -EOVERFLOW) {
+                        uuids_temp = realloc(uuids, obdcount *
+                                             sizeof(struct obd_uuid));
+                        if (uuids_temp != NULL)
+                                goto retry_get_uuids;
+                        else
+                                rc = -ENOMEM;
+                }
+
+                fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
                 goto out;
         }
 
@@ -1521,7 +1700,7 @@ static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
                         continue;
                 }
 
-                print_quota((char *)uuidp->uuid, qctl, 1);
+                print_quota(uuidp->uuid, qctl, 1);
         }
 
 out:
@@ -1533,22 +1712,20 @@ static int lfs_quota(int argc, char **argv)
 {
         int c;
         char *name = NULL, *mnt;
-        struct if_quotactl qctl;
-        char *obd_type = (char *)qctl.obd_type;
-        char *obd_uuid = (char *)qctl.obd_uuid.uuid;
+        struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
+                                    .qc_type = 0x01 };
+        char *obd_type = qctl.obd_type;
+        char *obd_uuid = qctl.obd_uuid.uuid;
         int rc;
 
-        memset(&qctl, 0, sizeof(qctl));
-        qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
-
         optind = 0;
         while ((c = getopt(argc, argv, "ugto:")) != -1) {
                 switch (c) {
                 case 'u':
-                        qctl.qc_type |= 0x01;
+                        qctl.qc_type = 0x01;
                         break;
                 case 'g':
-                        qctl.qc_type |= 0x02;
+                        qctl.qc_type = 0x02;
                         break;
                 case 't':
                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
@@ -1566,25 +1743,23 @@ static int lfs_quota(int argc, char **argv)
         if (qctl.qc_type)
                 qctl.qc_type--;
 
-        if (qctl.qc_type == UGQUOTA) {
-                fprintf(stderr, "error: user or group can't be specified"
-                                "both\n");
-                return CMD_HELP;
-        }
 
         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
-                if (optind + 2 != argc)
+                if (optind + 2 != argc) {
+                        fprintf(stderr, "error: missing quota argument(s)\n");
                         return CMD_HELP;
+                }
 
                 name = argv[optind++];
                 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
                 if (rc) {
-                        fprintf(stderr, "error: find id for name %s failed: %s\n",
+                        fprintf(stderr,"error: can't find id for name %s: %s\n",
                                 name, strerror(errno));
                         return CMD_HELP;
                 }
                 print_quota_title(name, &qctl);
         } else if (optind + 1 != argc) {
+                fprintf(stderr, "error: missing quota info argument(s)\n");
                 return CMD_HELP;
         }
 
@@ -1617,6 +1792,126 @@ static int lfs_quota(int argc, char **argv)
 }
 #endif /* HAVE_QUOTA_SUPPORT */
 
+static int flushctx_ioctl(char *mp)
+{
+        int fd, rc;
+
+        fd = open(mp, O_RDONLY);
+        if (fd == -1) {
+                fprintf(stderr, "flushctx: error open %s: %s\n",
+                        mp, strerror(errno));
+                return -1;
+        }
+
+        rc = ioctl(fd, LL_IOC_FLUSHCTX);
+        if (rc == -1)
+                fprintf(stderr, "flushctx: error ioctl %s: %s\n",
+                        mp, strerror(errno));
+
+        close(fd);
+        return rc;
+}
+
+static int lfs_flushctx(int argc, char **argv)
+{
+        int     kdestroy = 0, c;
+        FILE   *proc;
+        char    procline[PATH_MAX], *line;
+        int     rc = 0;
+
+        optind = 0;
+        while ((c = getopt(argc, argv, "k")) != -1) {
+                switch (c) {
+                case 'k':
+                        kdestroy = 1;
+                        break;
+                default:
+                        fprintf(stderr, "error: %s: option '-%c' "
+                                        "unrecognized\n", argv[0], c);
+                        return CMD_HELP;
+                }
+        }
+
+        if (kdestroy)
+                system("kdestroy > /dev/null");
+
+        if (optind >= argc) {
+                /* flush for all mounted lustre fs. */
+                proc = fopen("/proc/mounts", "r");
+                if (!proc) {
+                        fprintf(stderr, "error: %s: can't open /proc/mounts\n",
+                                argv[0]);
+                        return -1;
+                }
+
+                while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
+                        char dev[PATH_MAX];
+                        char mp[PATH_MAX];
+                        char fs[PATH_MAX];
+
+                        if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
+                                fprintf(stderr, "%s: unexpected format in "
+                                                "/proc/mounts\n",
+                                        argv[0]);
+                                return -1;
+                        }
+
+                        if (strcmp(fs, "lustre") != 0)
+                                continue;
+                        /* we use '@' to determine it's a client. are there
+                         * any other better way?
+                         */
+                        if (strchr(dev, '@') == NULL)
+                                continue;
+
+                        if (flushctx_ioctl(mp))
+                                rc = -1;
+                }
+        } else {
+                /* flush fs as specified */
+                while (optind < argc) {
+                        if (flushctx_ioctl(argv[optind++]))
+                                rc = -1;
+                }
+        }
+
+        return rc;
+}
+
+static int lfs_lsetfacl(int argc, char **argv)
+{
+        argv[0]++;
+        return(llapi_lsetfacl(argc, argv));
+}
+
+static int lfs_lgetfacl(int argc, char **argv)
+{
+        argv[0]++;
+        return(llapi_lgetfacl(argc, argv));
+}
+
+static int lfs_rsetfacl(int argc, char **argv)
+{
+        argv[0]++;
+        return(llapi_rsetfacl(argc, argv));
+}
+
+static int lfs_rgetfacl(int argc, char **argv)
+{
+        argv[0]++;
+        return(llapi_rgetfacl(argc, argv));
+}
+
+static int lfs_cp(int argc, char **argv)
+{
+        return(llapi_cp(argc, argv));
+}
+
+static int lfs_ls(int argc, char **argv)
+{
+        return(llapi_ls(argc, argv));
+}
+
 int main(int argc, char **argv)
 {
         int rc;