Whamcloud - gitweb
b=17187
[fs/lustre-release.git] / lustre / utils / lfs.c
index 315cb7d..f0d07ff 100644 (file)
@@ -1,28 +1,50 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (C) 2002 Cluster File Systems, Inc.
- *   Author: Peter J. Braam <braam@clusterfs.com>
- *   Author: Phil Schwan <phil@clusterfs.com>
- *   Author: Robert Read <rread@clusterfs.com>
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/utils/lfs.c
+ *
+ * Author: Peter J. Braam <braam@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
+ * Author: Robert Read <rread@clusterfs.com>
  */
 
+/* for O_DIRECTORY */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <getopt.h>
@@ -48,7 +70,7 @@
 #include <lustre/liblustreapi.h>
 #include <lustre/lustre_user.h>
 
-#include "parser.h"
+#include <libcfs/libcfsutil.h>
 #include "obdctl.h"
 
 unsigned int libcfs_subsystem_debug = 0;
@@ -71,8 +93,14 @@ 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);
+static int lfs_poollist(int argc, char **argv);
+static int lfs_path2fid(int argc, char **argv);
 
 /* all avaialable commands */
 command_t cmdlist[] = {
@@ -80,34 +108,37 @@ command_t cmdlist[] = {
          "Create a new file with a specific striping pattern or\n"
          "set the default striping pattern on an existing directory or\n"
          "delete the default striping pattern from an existing directory\n"
-         "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
-         "       or \n"
-         "       setstripe <filename|dirname> [--size|-s stripe_size]\n"
-         "                                    [--index|-i stripe_index]\n"
-         "                                    [--count|-c stripe_count]\n"
+         "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
+         "                 [--count|-c stripe_count] [--pool|-p pool_name]\n"
+         "                 <dir|filename>\n"
          "       or \n"
-         "       setstripe -d <dirname>   (to delete default striping)\n"
+         "       setstripe -d <dir>   (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)"},
+         "\t              Can be specified with k, m or g (in KB, MB and GB\n"
+         "\t              respectively)\n"
+         "\tstart_ost:    OST index of first stripe (-1 filesystem default)\n"
+         "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
+         "\tpool_name:    Name of OST pool"},
         {"getstripe", lfs_getstripe, 0,
-         "To list the striping info for a given filename or files in a\n"
+         "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"
          "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
          "                 [--recursive | -r] <dir|file> ..."},
+        {"poollist", lfs_poollist, 0,
+         "List pools or pool OSTs\n"
+         "usage: poollist <fsname>[.<poolname>] | <pathname>\n"},
         {"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> ..."},
+         "usage: find <dir|file> ... \n"
+         "     [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
+         "     [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
+         "     [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
+         "     [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
+         "     [[!] --uid|-u N] [[!] --user|-U <name>]\n"
+         "     [[!] --pool <name>]\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,24 +177,53 @@ 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]..."},
+        {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
+         "usage: path2fid <path>"},
         {"help", Parser_help, 0, "help"},
         {"exit", Parser_quit, 0, "quit"},
         {"quit", Parser_quit, 0, "quit"},
         { 0, 0, 0, NULL }
 };
 
+static int isnumber(const char *str)
+{
+        const char *ptr;
+
+        if (str[0] != '-' && !isdigit(str[0]))
+                return 0;
+
+        for (ptr = str + 1; *ptr != '\0'; ptr++) {
+                if (!isdigit(*ptr))
+                        return 0;
+        }
+
+        return 1;
+}
+
 /* functions */
 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,11 +231,15 @@ static int lfs_setstripe(int argc, char **argv)
         char *stripe_size_arg = NULL;
         char *stripe_off_arg = NULL;
         char *stripe_count_arg = NULL;
+        char *pool_name_arg = NULL;
+        unsigned long long size_units;
 
         struct option long_opts[] = {
                 {"size",        required_argument, 0, 's'},
                 {"count",       required_argument, 0, 'c'},
                 {"index",       required_argument, 0, 'i'},
+                {"offset",      required_argument, 0, 'o'},
+                {"pool",        required_argument, 0, 'p'},
                 {"delete",      no_argument,       0, 'd'},
                 {0, 0, 0, 0}
         };
@@ -183,15 +247,12 @@ static int lfs_setstripe(int argc, char **argv)
         st_size = 0;
         st_offset = -1;
         st_count = 0;
-        if (argc == 3 && strcmp(argv[1], "-d") == 0) {
-                /* for compatibility with the existing positional parameter
-                 * usage */
-                fname = argv[2];
-                optind = 2;
-        } else if (argc == 5  && 
-                   (argv[2][0] != '-' || isdigit(argv[2][1])) &&
-                   (argv[3][0] != '-' || isdigit(argv[3][1])) &&
-                   (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
+
+#if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
+        if (argc == 5 && argv[1][0] != '-' &&
+            isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
+                fprintf(stderr, "warning: deprecated usage of setstripe "
+                        "positional parameters.  Use -c, -i, -s instead.\n");
                 /* for compatibility with the existing positional parameter
                  * usage */
                 fname = argv[1];
@@ -199,10 +260,14 @@ static int lfs_setstripe(int argc, char **argv)
                 stripe_off_arg = argv[3];
                 stripe_count_arg = argv[4];
                 optind = 4;
-        } else {
-                while ((c = getopt_long(argc, argv, "c:di:s:",
-                                                long_opts, NULL)) >= 0) 
-                {
+        } else
+#else
+#warning "remove obsolete positional parameter code"
+#endif
+        {
+                optind = 0;
+                while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
+                                                long_opts, NULL)) >= 0) {
                         switch (c) {
                         case 0:
                                 /* Long options. */
@@ -215,11 +280,15 @@ static int lfs_setstripe(int argc, char **argv)
                                 delete = 1;
                                 break;
                         case 'i':
+                        case 'o':
                                 stripe_off_arg = optarg;
                                 break;
                         case 's':
                                 stripe_size_arg = optarg;
                                 break;
+                        case 'p':
+                                pool_name_arg = optarg;
+                                break;
                         case '?':
                                 return CMD_HELP;
                         default:
@@ -229,49 +298,32 @@ static int lfs_setstripe(int argc, char **argv)
                                 return CMD_HELP;
                         }
                 }
-                if (optind < argc)
-                        fname = argv[optind];
-                else
-                        return CMD_HELP;
 
+                fname = argv[optind];
 
-                if (delete && 
-                    (stripe_size_arg != NULL || stripe_off_arg != NULL || 
-                     stripe_count_arg != NULL)) {
+                if (delete &&
+                    (stripe_size_arg != NULL || stripe_off_arg != NULL ||
+                     stripe_count_arg != NULL || pool_name_arg != NULL)) {
                         fprintf(stderr, "error: %s: cannot specify -d with "
-                                        "-s, -c or -i options\n",
+                                        "-s, -c -o or -p options\n",
                                         argv[0]);
                         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]);
+
+        if (optind == argc) {
+                fprintf(stderr, "error: %s: missing filename|dirname\n",
+                                argv[0]);
                 return CMD_HELP;
         }
 
         /* 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: %s: bad size '%s'\n",
+                                argv[0], stripe_size_arg);
+                        return result;
                 }
         }
         /* get the stripe offset */
@@ -293,19 +345,33 @@ static int lfs_setstripe(int argc, char **argv)
                 }
         }
 
-        result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
-        if (result)
-                fprintf(stderr, "error: %s: create stripe file failed\n",
-                                argv[0]);
+        do {
+                result = llapi_file_create_pool(fname, st_size, st_offset,
+                                                st_count, 0, pool_name_arg);
+                if (result) {
+                        fprintf(stderr,"error: %s: create stripe file '%s' "
+                                "failed\n", argv[0], fname);
+                        break;
+                }
+                fname = argv[++optind];
+        } while (fname != NULL);
 
         return result;
 }
 
+static int lfs_poollist(int argc, char **argv)
+{
+        if (argc != 2)
+                return CMD_HELP;
+
+        return llapi_poollist(argv[1]);
+}
+
 static int set_time(time_t *time, time_t *set, char *str)
 {
         time_t t;
         int res = 0;
-        
+
         if (str[0] == '+')
                 res = 1;
         else if (str[0] == '-')
@@ -326,20 +392,81 @@ static int set_time(time_t *time, time_t *set, char *str)
         return res;
 }
 
+static int name2id(unsigned int *id, char *name, int type)
+{
+        if (type == USRQUOTA) {
+                struct passwd *entry;
+
+                if (!(entry = getpwnam(name))) {
+                        if (!errno)
+                                errno = ENOENT;
+                        return -1;
+                }
+
+                *id = entry->pw_uid;
+        } else {
+                struct group *entry;
+
+                if (!(entry = getgrnam(name))) {
+                        if (!errno)
+                                errno = ENOENT;
+                        return -1;
+                }
+
+                *id = entry->gr_gid;
+        }
+
+        return 0;
+}
+
+static int id2name(char **name, unsigned int id, int type)
+{
+        if (type == USRQUOTA) {
+                struct passwd *entry;
+
+                if (!(entry = getpwuid(id))) {
+                        if (!errno)
+                                errno = ENOENT;
+                        return -1;
+                }
+
+                *name = entry->pw_name;
+        } else {
+                struct group *entry;
+
+                if (!(entry = getgrgid(id))) {
+                        if (!errno)
+                                errno = ENOENT;
+                        return -1;
+                }
+
+                *name = entry->gr_name;
+        }
+
+        return 0;
+}
+
+#define FIND_POOL_OPT 3
 static int lfs_find(int argc, char **argv)
 {
         int new_fashion = 1;
         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'},
                 {"ctime",     required_argument, 0, 'C'},
                 {"mtime",     required_argument, 0, 'M'},
                 {"maxdepth",  required_argument, 0, 'D'},
+                {"gid",       required_argument, 0, 'g'},
+                {"group",     required_argument, 0, 'G'},
+                {"uid",       required_argument, 0, 'u'},
+                {"user",      required_argument, 0, 'U'},
                 {"name",      required_argument, 0, 'n'},
+                /* no short option for pool, p/P already used */
+                {"pool",      required_argument, 0, FIND_POOL_OPT},
                 /* --obd is considered as a new option. */
                 {"obd",       required_argument, 0, 'O'},
                 {"ost",       required_argument, 0, 'O'},
@@ -348,6 +475,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}
         };
@@ -357,16 +486,25 @@ static int lfs_find(int argc, char **argv)
         time_t *xtime;
         int *xsign;
         int isoption;
+        char *endptr;
 
         time(&t);
 
-        while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrv",
+        optind = 0;
+        /* when getopt_long_only() hits '!' it returns 1 and puts "!" in optarg */
+        while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
                                      long_opts, NULL)) >= 0) {
                 xtime = NULL;
                 xsign = NULL;
                 if (neg_opt)
                         --neg_opt;
                 /* '!' is part of option */
+                /* when getopt_long_only() finds a string which is not
+                 * an option nor a known option argument it returns 1
+                 * in that case if we already have found pathstart and pathend
+                 * (i.e. we have the list of pathnames),
+                 * the only supported value is "!"
+                 */
                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
                 if (!isoption && pathend != -1) {
                         fprintf(stderr, "err: %s: filename|dirname must either "
@@ -388,6 +526,9 @@ static int lfs_find(int argc, char **argv)
                         /* Long options. */
                         break;
                 case 1:
+                        /* unknown; opt is "!" or path component,
+                         * checking done above.
+                         */
                         if (strcmp(optarg, "!") == 0)
                                 neg_opt = 2;
                         break;
@@ -411,10 +552,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 +565,113 @@ static int lfs_find(int argc, char **argv)
                                 *xsign = ret;
                         break;
                 case 'D':
+                        new_fashion = 1;
                         param.maxdepth = strtol(optarg, 0, 0);
                         break;
+                case 'g':
+                        new_fashion = 1;
+                        param.gid = strtol(optarg, &endptr, 10);
+                        if (optarg == endptr) {
+                                fprintf(stderr, "Bad gid: %s\n", optarg);
+                                return CMD_HELP;
+                        }
+                        param.exclude_gid = !!neg_opt;
+                        param.check_gid = 1;
+                        break;
+                case 'G':
+                        new_fashion = 1;
+                        param.gid = strtol(optarg, &endptr, 10);
+                        if (optarg == endptr) {
+                                ret = name2id(&param.gid, optarg, GRPQUOTA);
+                                if (ret != 0) {
+                                        fprintf(stderr, "Group/GID: %s cannot "
+                                                "be found.\n", optarg);
+                                        return -1;
+                                }
+                        }
+                        param.exclude_gid = !!neg_opt;
+                        param.check_gid = 1;
+                        break;
+                case 'u':
+                        new_fashion = 1;
+                        param.uid = strtol(optarg, &endptr, 10);
+                        if (optarg == endptr) {
+                                fprintf(stderr, "Bad uid: %s\n", optarg);
+                                return CMD_HELP;
+                        }
+                        param.exclude_uid = !!neg_opt;
+                        param.check_uid = 1;
+                        break;
+                case 'U':
+                        new_fashion = 1;
+                        param.uid = strtol(optarg, &endptr, 10);
+                        if (optarg == endptr) {
+                                ret = name2id(&param.uid, optarg, USRQUOTA);
+                                if (ret != 0) {
+                                        fprintf(stderr, "User/UID: %s cannot "
+                                                "be found.\n", optarg);
+                                        return -1;
+                                }
+                        }
+                        param.exclude_uid = !!neg_opt;
+                        param.check_uid = 1;
+                        break;
+                case FIND_POOL_OPT:
+                        new_fashion = 1;
+                        if (strlen(optarg) > LOV_MAXPOOLNAME) {
+                                fprintf(stderr,
+                                        "Pool name %s is too long"
+                                        " (max is %d)\n", optarg,
+                                        LOV_MAXPOOLNAME);
+                                return -1;
+                        }
+                        /* we do check for empty pool because empty pool
+                         * is used to find V1 lov attributes */
+                        strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
+                        param.poolname[LOV_MAXPOOLNAME] = '\0';
+                        param.exclude_pool = !!neg_opt;
+                        param.check_pool = 1;
+                        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 = 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 +685,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++;
@@ -470,7 +743,7 @@ static int lfs_find(int argc, char **argv)
                         return CMD_HELP;
                 };
         }
-        
+
         if (pathstart == -1) {
                 fprintf(stderr, "error: %s: no filename|pathname\n",
                         argv[0]);
@@ -483,10 +756,16 @@ 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;
         }
-        
+
         do {
                 if (new_fashion)
                         ret = llapi_find(argv[pathstart], &param);
@@ -497,6 +776,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;
 }
 
@@ -556,7 +839,7 @@ static int lfs_getstripe(int argc, char **argv)
         } while (++optind < argc && !rc);
 
         if (rc)
-                fprintf(stderr, "error: %s failed for %s.\n", 
+                fprintf(stderr, "error: %s failed for %s.\n",
                         argv[0], argv[optind - 1]);
         return rc;
 }
@@ -565,7 +848,6 @@ static int lfs_osts(int argc, char **argv)
 {
         FILE *fp;
         struct mntent *mnt = NULL;
-        struct obd_uuid *obduuid = NULL;
         struct find_param param;
         int rc=0;
 
@@ -579,9 +861,8 @@ static int lfs_osts(int argc, char **argv)
                         strerror (errno));
         } else {
                 mnt = getmntent(fp);
-                memset(&param, 0, sizeof(param));
-                param.obduuid = obduuid;
                 while (feof(fp) == 0 && ferror(fp) ==0) {
+                        memset(&param, 0, sizeof(param));
                         if (llapi_is_lustre_mnt(mnt)) {
                                 rc = llapi_getstripe(mnt->mnt_dir, &param);
                                 if (rc)
@@ -597,6 +878,32 @@ static int lfs_osts(int argc, char **argv)
         return rc;
 }
 
+static int lfs_path2fid(int argc, char **argv)
+{
+        char *path;
+        unsigned long long seq;
+        unsigned long oid, ver;
+        int rc;
+
+        if (argc != 2)
+                return CMD_HELP;
+
+        path = argv[1];
+        rc = llapi_path2fid(path, &seq, &oid, &ver);
+        if (rc) {
+                fprintf(stderr, "error: can't get fid for %s\n", path);
+                return rc;
+        }
+
+        printf("%llu:%lu", seq, oid);
+        if (ver)
+                printf(":%lu", ver);
+
+        printf("\n");
+
+        return 0;
+}
+
 #define COOK(value)                                                     \
 ({                                                                      \
         int radix = 0;                                                  \
@@ -656,7 +963,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 +979,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;
                 }
 
@@ -811,7 +1119,7 @@ static int lfs_df(int argc, char **argv)
         FILE *fp;
         char *path = NULL;
         struct mntent *mnt = NULL;
-        char mntdir[PATH_MAX] = {'\0'};
+        char *mntdir = NULL;
         int ishow = 0, cooked = 0;
         int c, rc = 0;
 
@@ -838,10 +1146,19 @@ static int lfs_df(int argc, char **argv)
                         argv[0], MOUNTED, strerror(errno));
                 return rc;
         }
+
+        if ((mntdir = malloc(PATH_MAX)) == NULL) {
+                fprintf(stderr, "error: cannot allocate %d bytes\n",
+                        PATH_MAX);
+                return -ENOMEM;
+        }
+        memset(mntdir, 0, PATH_MAX);
+
         if (path) {
-                rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
+                rc = path2mnt(path, fp, mntdir, PATH_MAX);
                 if (rc) {
                         endmntent(fp);
+                        free(mntdir);
                         return rc;
                 }
 
@@ -862,6 +1179,7 @@ static int lfs_df(int argc, char **argv)
                 endmntent(fp);
         }
 
+        free(mntdir);
         return rc;
 }
 
@@ -1003,6 +1321,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':
@@ -1204,60 +1523,6 @@ static int lfs_quotaoff(int argc, char **argv)
         return 0;
 }
 
-static int name2id(unsigned int *id, char *name, int type)
-{
-        if (type == USRQUOTA) {
-                struct passwd *entry;
-
-                if (!(entry = getpwnam(name))) {
-                        if (!errno)
-                                errno = ENOENT;
-                        return -1;
-                }
-
-                *id = entry->pw_uid;
-        } else {
-                struct group *entry;
-
-                if (!(entry = getgrnam(name))) {
-                        if (!errno)
-                                errno = ENOENT;
-                        return -1;
-                }
-
-                *id = entry->gr_gid;
-        }
-
-        return 0;
-}
-
-static int id2name(char **name, unsigned int id, int type)
-{
-        if (type == USRQUOTA) {
-                struct passwd *entry;
-
-                if (!(entry = getpwuid(id))) {
-                        if (!errno)
-                                errno = ENOENT;
-                        return -1;
-                }
-
-                *name = entry->pw_name;
-        } else {
-                struct group *entry;
-
-                if (!(entry = getgrgid(id))) {
-                        if (!errno)
-                                errno = ENOENT;
-                        return -1;
-                }
-
-                *name = entry->gr_name;
-        }
-
-        return 0;
-}
-
 #define ARG2INT(nr, str, msg)                                           \
 do {                                                                    \
         char *endp;                                                     \
@@ -1277,7 +1542,7 @@ do {                                                                    \
  *        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};
@@ -1299,7 +1564,7 @@ static unsigned long str2sec(const char* timestr) {
 
                 v = strtoul(timestr, &tail, 10);
                 if (v == ULONG_MAX || *tail == '\0')
-                        /* value too large (ULONG_MAX or more) 
+                        /* value too large (ULONG_MAX or more)
                            or missing specifier */
                         goto error;
 
@@ -1575,7 +1840,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 +1850,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 +2063,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)