Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / utils / lfs.c
index 315cb7d..8758601 100644 (file)
@@ -71,8 +71,12 @@ 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_getfacl(int argc, char **argv);
-static int lfs_setfacl(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[] = {
@@ -99,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"
@@ -146,12 +147,24 @@ command_t cmdlist[] = {
 #endif
         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
          "usage: flushctx [-k] [mountpoint...]"},
-        {"getfacl", lfs_getfacl, 0,
-         "Get file access control list in remote client.\n"
-         "usage: getfacl [-dRLPvh] file"},
-        {"setfacl", lfs_setfacl, 0,
-         "Set file access control list in remote client.\n"
-         "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
+        {"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"},
@@ -163,7 +176,7 @@ static int lfs_setstripe(int argc, char **argv)
 {
         char *fname;
         int result;
-        unsigned long st_size;
+        unsigned long long st_size;
         int  st_offset, st_count;
         char *end;
         int c;
@@ -171,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'},
@@ -200,9 +214,9 @@ static int lfs_setstripe(int argc, char **argv)
                 stripe_count_arg = argv[4];
                 optind = 4;
         } else {
+                optind = 0;
                 while ((c = getopt_long(argc, argv, "c:di:s:",
-                                                long_opts, NULL)) >= 0) 
-                {
+                                                long_opts, NULL)) >= 0) {
                         switch (c) {
                         case 0:
                                 /* Long options. */
@@ -253,25 +267,11 @@ static int lfs_setstripe(int argc, char **argv)
 
         /* get the stripe size */
         if (stripe_size_arg != NULL) {
-                st_size = strtoul(stripe_size_arg, &end, 0);
-                if (*end != '\0') {
-                        if ((*end == 'k' || *end == 'K') && 
-                            *(end+1) == '\0' &&
-                            (st_size & (~0UL << (32 - 10))) == 0) {
-                                st_size <<= 10;
-                        } else if ((*end == 'm' || *end == 'M') && 
-                                   *(end+1) == '\0' &&
-                                   (st_size & (~0UL << (32 - 20))) == 0) {
-                                st_size <<= 20;
-                        } else if ((*end == 'g' || *end == 'G') && 
-                                   *(end+1) == '\0' &&
-                                   (st_size & (~0UL << (32 - 30))) == 0) {
-                                st_size <<= 30;
-                        } else {
-                                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 */
@@ -332,7 +332,7 @@ static int lfs_find(int argc, char **argv)
         int c, ret;
         time_t t;
         struct find_param param = { .maxdepth = -1 };
-        char timestr[1024];
+        char str[1024];
         struct option long_opts[] = {
                 /* New find options. */
                 {"atime",     required_argument, 0, 'A'},
@@ -348,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}
         };
@@ -360,7 +362,8 @@ static int lfs_find(int argc, char **argv)
 
         time(&t);
 
-        while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrv",
+        optind = 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;
@@ -411,10 +414,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);
@@ -424,26 +427,49 @@ static int lfs_find(int argc, char **argv)
                                 *xsign = ret;
                         break;
                 case 'D':
+                        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 (param.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;
                         }
-                        param.obduuid = (struct obd_uuid *)optarg;
+
+                        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);
+                        }
+
+                        if (buf)
+                                free(buf);
                         break;
+                }
                 case 'p':
+                        new_fashion = 1;
                         param.zeroend = 1;
                         break;
                 case 'P':
@@ -457,6 +483,51 @@ static int lfs_find(int argc, char **argv)
                         new_fashion = 0;
                         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;
                         param.verbose++;
@@ -483,6 +554,12 @@ static int lfs_find(int argc, char **argv)
         if (new_fashion) {
                 param.quiet = 1;
         } else {
+                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;
         }
@@ -497,6 +574,10 @@ static int lfs_find(int argc, char **argv)
         if (ret)
                 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;
 }
 
@@ -656,7 +737,8 @@ static int showdf(char *mntdir, struct obd_statfs *stat,
         long long avail, used, total;
         double ratio = 0;
         char *suffix = "KMGTPEZY";
-        char tbuf[10], ubuf[10], abuf[10], rbuf[10];
+        /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
+        char tbuf[20], ubuf[20], abuf[20], rbuf[20];
 
         if (!uuid || !stat)
                 return -EINVAL;
@@ -671,8 +753,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;
                 }
 
@@ -1003,6 +1085,7 @@ static int lfs_quotachown(int argc, char **argv)
         int c,rc;
         int flag = 0;
 
+        optind = 0;
         while ((c = getopt(argc, argv, "i")) != -1) {
                 switch (c) {
                 case 'i':
@@ -1575,7 +1658,7 @@ static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
 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;
 
@@ -1585,9 +1668,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;
         }
 
@@ -1781,117 +1881,38 @@ static int lfs_flushctx(int argc, char **argv)
         return rc;
 }
 
-/*
- * We assume one and only one filename is supplied as the
- * last parameter.
- */
-static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
+static int lfs_lsetfacl(int argc, char **argv)
 {
-        char *dname, *rpath = NULL;
-        char path[PATH_MAX], cwd[PATH_MAX];
-        FILE *fp;
-        struct mntent *mnt;
-        int i;
-
-        if (argc < 2)
-                return -1;
-
-        /* FIXME the premise is there is no sub-mounted filesystems under this
-         * mounted lustre tree. */
-        strncpy(fname, argv[argc - 1], PATH_MAX);
-
-        /* get path prefix */
-        dname = dirname(fname);
-
-        /* try to resolve the pathname into relative to the root of the mounted
-         * lustre filesystem.
-         */
-        if (getcwd(cwd, sizeof(cwd)) == NULL) {
-                fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
-                return -1;
-        }
-
-        if (chdir(dname) == -1) {
-                fprintf(stderr, "chdir to %s failed: %s\n",
-                        dname, strerror(errno));
-                return -1;
-        }
-
-        if (getcwd(path, sizeof(path)) == NULL) {
-                fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
-                return -1;
-        }
-
-        if (chdir(cwd) == -1) {
-                fprintf(stderr, "chdir back to %s: %s\n",
-                        cwd, strerror(errno));
-                return -1;
-        }
-
-        strncat(path, "/", PATH_MAX);
-        strncpy(fname, argv[argc - 1], PATH_MAX);
-        strncat(path, basename(fname), PATH_MAX);
-
-        fp = setmntent(MOUNTED, "r");
-        if (fp == NULL) {
-                fprintf(stderr, "setmntent %s failed: %s\n",
-                        MOUNTED, strerror(errno));
-                return -1;
-        }
-
-        while (1) {
-                mnt = getmntent(fp);
-                if (!mnt)
-                        break;
-
-                if (!llapi_is_lustre_mnttype(mnt->mnt_type))
-                        continue;
-
-                if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
-                        rpath = path + strlen(mnt->mnt_dir);
-                        break;
-                }
-        }
-        endmntent(fp);
-
-        /* remove char '/' from rpath to be a relative path */
-        while (rpath && *rpath == '/') rpath++;
-
-        if (!rpath) {
-                fprintf(stderr,
-                        "%s: file %s doesn't belong to a lustre file system!\n",
-                        argv[0], argv[argc - 1]);
-                return -1;
-        }
-
-        for (i = 0; i < argc - 1; i++) {
-                strncat(cmd, argv[i], PATH_MAX);
-                strncat(cmd, " ", PATH_MAX);
-        }
-        strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
-        strncpy(fname, argv[argc - 1], sizeof(fname));
-
-        return 0;
+        argv[0]++;
+        return(llapi_lsetfacl(argc, argv));
 }
 
-static int lfs_getfacl(int argc, char **argv)
+static int lfs_lgetfacl(int argc, char **argv)
 {
-        char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
-
-        if (acl_cmd_parse(argc, argv, fname, cmd))
-                return CMD_HELP;
+        argv[0]++;
+        return(llapi_lgetfacl(argc, argv));
+}
 
-        return llapi_getfacl(fname, cmd);
+static int lfs_rsetfacl(int argc, char **argv)
+{
+        argv[0]++;
+        return(llapi_rsetfacl(argc, argv));
 }
 
-static int lfs_setfacl(int argc, char **argv)
+static int lfs_rgetfacl(int argc, char **argv)
 {
-        char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
+        argv[0]++;
+        return(llapi_rgetfacl(argc, argv));
+}
 
-        if (acl_cmd_parse(argc, argv, fname, cmd))
-                return CMD_HELP;
+static int lfs_cp(int argc, char **argv)
+{
+        return(llapi_cp(argc, argv));
+}
 
-        return llapi_setfacl(fname, cmd);
+static int lfs_ls(int argc, char **argv)
+{
+        return(llapi_ls(argc, argv));
 }
 
 int main(int argc, char **argv)