Whamcloud - gitweb
b=17299
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/lfs.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Robert Read <rread@clusterfs.com>
41  */
42
43 /* for O_DIRECTORY */
44 #ifndef _GNU_SOURCE
45 #define _GNU_SOURCE
46 #endif
47
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <getopt.h>
51 #include <string.h>
52 #include <mntent.h>
53 #include <errno.h>
54 #include <pwd.h>
55 #include <grp.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <dirent.h>
60 #include <time.h>
61 #include <ctype.h>
62 /* For dirname() */
63 #include <libgen.h>
64
65 #include <lnet/api-support.h>
66 #include <lnet/lnetctl.h>
67
68 #include <liblustre.h>
69 #include <lustre/lustre_idl.h>
70 #include <lustre/liblustreapi.h>
71 #include <lustre/lustre_user.h>
72
73 #include <libcfs/libcfsutil.h>
74 #include "obdctl.h"
75
76 unsigned int libcfs_subsystem_debug = 0;
77
78 /* all functions */
79 static int lfs_setstripe(int argc, char **argv);
80 static int lfs_find(int argc, char **argv);
81 static int lfs_getstripe(int argc, char **argv);
82 static int lfs_osts(int argc, char **argv);
83 static int lfs_df(int argc, char **argv);
84 static int lfs_check(int argc, char **argv);
85 static int lfs_catinfo(int argc, char **argv);
86 #ifdef HAVE_QUOTA_SUPPORT
87 static int lfs_quotachown(int argc, char **argv);
88 static int lfs_quotacheck(int argc, char **argv);
89 static int lfs_quotaon(int argc, char **argv);
90 static int lfs_quotaoff(int argc, char **argv);
91 static int lfs_setquota(int argc, char **argv);
92 static int lfs_quota(int argc, char **argv);
93 #endif
94 static int lfs_flushctx(int argc, char **argv);
95 static int lfs_join(int argc, char **argv);
96 static int lfs_lsetfacl(int argc, char **argv);
97 static int lfs_lgetfacl(int argc, char **argv);
98 static int lfs_rsetfacl(int argc, char **argv);
99 static int lfs_rgetfacl(int argc, char **argv);
100 static int lfs_cp(int argc, char **argv);
101 static int lfs_ls(int argc, char **argv);
102 static int lfs_poollist(int argc, char **argv);
103
104 /* all avaialable commands */
105 command_t cmdlist[] = {
106         {"setstripe", lfs_setstripe, 0,
107          "Create a new file with a specific striping pattern or\n"
108          "set the default striping pattern on an existing directory or\n"
109          "delete the default striping pattern from an existing directory\n"
110          "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
111          "                 [--count|-c stripe_count] [--pool|-p pool_name]\n"
112          "                 <dir|filename>\n"
113          "       or \n"
114          "       setstripe -d <dir>   (to delete default striping)\n"
115          "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n"
116          "\t              Can be specified with k, m or g (in KB, MB and GB\n"
117          "\t              respectively)\n"
118          "\tstart_ost:    OST index of first stripe (-1 filesystem default)\n"
119          "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
120          "\tpool_name:    Name of OST pool"},
121         {"getstripe", lfs_getstripe, 0,
122          "To list the striping info for a given file or files in a\n"
123          "directory or recursively for all files in a directory tree.\n"
124          "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
125          "                 [--recursive | -r] <dir|file> ..."},
126         {"poollist", lfs_poollist, 0,
127          "List pools or pool OSTs\n"
128          "usage: poollist <fsname>[.<poolname>] | <pathname>\n"},
129         {"find", lfs_find, 0,
130          "To find files that match given parameters recursively in a directory tree.\n"
131          "usage: find <dir|file> ... \n"
132          "     [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
133          "     [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
134          "     [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
135          "     [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
136          "     [[!] --uid|-u N] [[!] --user|-U <name>]\n"
137          "     [[!] --pool <name>]\n"
138          "\t !: used before an option indicates 'NOT' the requested attribute\n"
139          "\t -: used before an value indicates 'AT MOST' the requested value\n"
140          "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
141         {"check", lfs_check, 0,
142          "Display the status of MDS or OSTs (as specified in the command)\n"
143          "or all the servers (MDS and OSTs).\n"
144          "usage: check <osts|mds|servers>"},
145         {"catinfo", lfs_catinfo, 0,
146          "Show information of specified type logs.\n"
147          "usage: catinfo {keyword} [node name]\n"
148          "\tkeywords are one of followings: config, deletions.\n"
149          "\tnode name must be provided when use keyword config."},
150         {"join", lfs_join, 0,
151          "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
152          "usage: join <filename_A> <filename_B>\n"},
153         {"osts", lfs_osts, 0, "osts"},
154         {"df", lfs_df, 0,
155          "report filesystem disk space usage or inodes usage"
156          "of each MDS/OSD.\n"
157          "Usage: df [-i] [-h] [path]"},
158 #ifdef HAVE_QUOTA_SUPPORT
159         {"quotachown",lfs_quotachown, 0,
160          "Change files' owner or group on the specified filesystem.\n"
161          "usage: quotachown [-i] <filesystem>\n"
162          "\t-i: ignore error if file is not exist\n"},
163         {"quotacheck", lfs_quotacheck, 0,
164          "Scan the specified filesystem for disk usage, and create,\n"
165          "or update quota files.\n"
166          "usage: quotacheck [ -ug ] <filesystem>"},
167         {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
168          "usage: quotaon [ -ugf ] <filesystem>"},
169         {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
170          "usage: quotaoff [ -ug ] <filesystem>"},
171         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
172          "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
173          "       setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
174         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
175          "usage: quota [ -o obd_uuid ] [{-u|-g  <name>}|-t] <filesystem>"},
176 #endif
177         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
178          "usage: flushctx [-k] [mountpoint...]"},
179         {"lsetfacl", lfs_lsetfacl, 0,
180          "Remote user setfacl for user/group on the same remote client.\n"
181          "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
182         {"lgetfacl", lfs_lgetfacl, 0,
183          "Remote user getfacl for user/group on the same remote client.\n"
184          "usage: lgetfacl [-dRLPvh] file ..."},
185         {"rsetfacl", lfs_rsetfacl, 0,
186          "Remote user setfacl for user/group on other clients.\n"
187          "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
188         {"rgetfacl", lfs_rgetfacl, 0,
189          "Remote user getfacl for user/group on other clients.\n"
190          "usage: rgetfacl [-dRLPvh] file ..."},
191         {"cp", lfs_cp, 0,
192          "Remote user copy files and directories.\n"
193          "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
194         {"ls", lfs_ls, 0,
195          "Remote user list directory contents.\n"
196          "usage: ls [OPTION]... [FILE]..."},
197         {"help", Parser_help, 0, "help"},
198         {"exit", Parser_quit, 0, "quit"},
199         {"quit", Parser_quit, 0, "quit"},
200         { 0, 0, 0, NULL }
201 };
202
203 static int isnumber(const char *str)
204 {
205         const char *ptr;
206
207         if (str[0] != '-' && !isdigit(str[0]))
208                 return 0;
209
210         for (ptr = str + 1; *ptr != '\0'; ptr++) {
211                 if (!isdigit(*ptr))
212                         return 0;
213         }
214
215         return 1;
216 }
217
218 /* functions */
219 static int lfs_setstripe(int argc, char **argv)
220 {
221         char *fname;
222         int result;
223         unsigned long long st_size;
224         int  st_offset, st_count;
225         char *end;
226         int c;
227         int delete = 0;
228         char *stripe_size_arg = NULL;
229         char *stripe_off_arg = NULL;
230         char *stripe_count_arg = NULL;
231         char *pool_name_arg = NULL;
232         unsigned long long size_units;
233
234         struct option long_opts[] = {
235                 {"size",        required_argument, 0, 's'},
236                 {"count",       required_argument, 0, 'c'},
237                 {"index",       required_argument, 0, 'i'},
238                 {"offset",      required_argument, 0, 'o'},
239                 {"pool",        required_argument, 0, 'p'},
240                 {"delete",      no_argument,       0, 'd'},
241                 {0, 0, 0, 0}
242         };
243
244         st_size = 0;
245         st_offset = -1;
246         st_count = 0;
247
248 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
249         if (argc == 5 && argv[1][0] != '-' &&
250             isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
251                 fprintf(stderr, "warning: deprecated usage of setstripe "
252                         "positional parameters.  Use -c, -i, -s instead.\n");
253                 /* for compatibility with the existing positional parameter
254                  * usage */
255                 fname = argv[1];
256                 stripe_size_arg = argv[2];
257                 stripe_off_arg = argv[3];
258                 stripe_count_arg = argv[4];
259                 optind = 4;
260         } else
261 #else
262 #warning "remove obsolete positional parameter code"
263 #endif
264         {
265                 optind = 0;
266                 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
267                                                 long_opts, NULL)) >= 0) {
268                         switch (c) {
269                         case 0:
270                                 /* Long options. */
271                                 break;
272                         case 'c':
273                                 stripe_count_arg = optarg;
274                                 break;
275                         case 'd':
276                                 /* delete the default striping pattern */
277                                 delete = 1;
278                                 break;
279                         case 'i':
280                         case 'o':
281                                 stripe_off_arg = optarg;
282                                 break;
283                         case 's':
284                                 stripe_size_arg = optarg;
285                                 break;
286                         case 'p':
287                                 pool_name_arg = optarg;
288                                 break;
289                         case '?':
290                                 return CMD_HELP;
291                         default:
292                                 fprintf(stderr, "error: %s: option '%s' "
293                                                 "unrecognized\n",
294                                                 argv[0], argv[optind - 1]);
295                                 return CMD_HELP;
296                         }
297                 }
298
299                 fname = argv[optind];
300
301                 if (delete &&
302                     (stripe_size_arg != NULL || stripe_off_arg != NULL ||
303                      stripe_count_arg != NULL || pool_name_arg != NULL)) {
304                         fprintf(stderr, "error: %s: cannot specify -d with "
305                                         "-s, -c -o or -p options\n",
306                                         argv[0]);
307                         return CMD_HELP;
308                 }
309         }
310
311         if (optind == argc) {
312                 fprintf(stderr, "error: %s: missing filename|dirname\n",
313                                 argv[0]);
314                 return CMD_HELP;
315         }
316
317         /* get the stripe size */
318         if (stripe_size_arg != NULL) {
319                 result = parse_size(stripe_size_arg, &st_size, &size_units);
320                 if (result) {
321                         fprintf(stderr, "error: %s: bad size '%s'\n",
322                                 argv[0], stripe_size_arg);
323                         return result;
324                 }
325         }
326         /* get the stripe offset */
327         if (stripe_off_arg != NULL) {
328                 st_offset = strtoul(stripe_off_arg, &end, 0);
329                 if (*end != '\0') {
330                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
331                                 argv[0], stripe_off_arg);
332                         return CMD_HELP;
333                 }
334         }
335         /* get the stripe count */
336         if (stripe_count_arg != NULL) {
337                 st_count = strtoul(stripe_count_arg, &end, 0);
338                 if (*end != '\0') {
339                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
340                                 argv[0], stripe_count_arg);
341                         return CMD_HELP;
342                 }
343         }
344
345         do {
346                 result = llapi_file_create_pool(fname, st_size, st_offset,
347                                                 st_count, 0, pool_name_arg);
348                 if (result) {
349                         fprintf(stderr,"error: %s: create stripe file '%s' "
350                                 "failed\n", argv[0], fname);
351                         break;
352                 }
353                 fname = argv[++optind];
354         } while (fname != NULL);
355
356         return result;
357 }
358
359 static int lfs_poollist(int argc, char **argv)
360 {
361         if (argc != 2)
362                 return CMD_HELP;
363
364         return llapi_poollist(argv[1]);
365 }
366
367 static int set_time(time_t *time, time_t *set, char *str)
368 {
369         time_t t;
370         int res = 0;
371
372         if (str[0] == '+')
373                 res = 1;
374         else if (str[0] == '-')
375                 res = -1;
376
377         if (res)
378                 str++;
379
380         t = strtol(str, NULL, 0);
381         if (*time < t * 24 * 60 * 60) {
382                 if (res)
383                         str--;
384                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
385                 return INT_MAX;
386         }
387
388         *set = *time - t * 24 * 60 * 60;
389         return res;
390 }
391
392 static int name2id(unsigned int *id, char *name, int type)
393 {
394         if (type == USRQUOTA) {
395                 struct passwd *entry;
396
397                 if (!(entry = getpwnam(name))) {
398                         if (!errno)
399                                 errno = ENOENT;
400                         return -1;
401                 }
402
403                 *id = entry->pw_uid;
404         } else {
405                 struct group *entry;
406
407                 if (!(entry = getgrnam(name))) {
408                         if (!errno)
409                                 errno = ENOENT;
410                         return -1;
411                 }
412
413                 *id = entry->gr_gid;
414         }
415
416         return 0;
417 }
418
419 static int id2name(char **name, unsigned int id, int type)
420 {
421         if (type == USRQUOTA) {
422                 struct passwd *entry;
423
424                 if (!(entry = getpwuid(id))) {
425                         if (!errno)
426                                 errno = ENOENT;
427                         return -1;
428                 }
429
430                 *name = entry->pw_name;
431         } else {
432                 struct group *entry;
433
434                 if (!(entry = getgrgid(id))) {
435                         if (!errno)
436                                 errno = ENOENT;
437                         return -1;
438                 }
439
440                 *name = entry->gr_name;
441         }
442
443         return 0;
444 }
445
446 #define FIND_POOL_OPT 3
447 static int lfs_find(int argc, char **argv)
448 {
449         int new_fashion = 1;
450         int c, ret;
451         time_t t;
452         struct find_param param = { .maxdepth = -1 };
453         char str[1024];
454         struct option long_opts[] = {
455                 /* New find options. */
456                 {"atime",     required_argument, 0, 'A'},
457                 {"ctime",     required_argument, 0, 'C'},
458                 {"mtime",     required_argument, 0, 'M'},
459                 {"maxdepth",  required_argument, 0, 'D'},
460                 {"gid",       required_argument, 0, 'g'},
461                 {"group",     required_argument, 0, 'G'},
462                 {"uid",       required_argument, 0, 'u'},
463                 {"user",      required_argument, 0, 'U'},
464                 {"name",      required_argument, 0, 'n'},
465                 /* no short option for pool, p/P already used */
466                 {"pool",      required_argument, 0, FIND_POOL_OPT},
467                 /* --obd is considered as a new option. */
468                 {"obd",       required_argument, 0, 'O'},
469                 {"ost",       required_argument, 0, 'O'},
470                 {"print",     no_argument,       0, 'P'},
471                 {"print0",    no_argument,       0, 'p'},
472                 /* Old find options. */
473                 {"quiet",     no_argument,       0, 'q'},
474                 {"recursive", no_argument,       0, 'r'},
475                 {"size",      required_argument, 0, 's'},
476                 {"type",      required_argument, 0, 't'},
477                 {"verbose",   no_argument,       0, 'v'},
478                 {0, 0, 0, 0}
479         };
480         int pathstart = -1;
481         int pathend = -1;
482         int neg_opt = 0;
483         time_t *xtime;
484         int *xsign;
485         int isoption;
486         char *endptr;
487
488         time(&t);
489
490         optind = 0;
491         /* when getopt_long_only() hits '!' it returns 1 and puts "!" in optarg */
492         while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
493                                      long_opts, NULL)) >= 0) {
494                 xtime = NULL;
495                 xsign = NULL;
496                 if (neg_opt)
497                         --neg_opt;
498                 /* '!' is part of option */
499                 /* when getopt_long_only() finds a string which is not
500                  * an option nor a known option argument it returns 1
501                  * in that case if we already have found pathstart and pathend
502                  * (i.e. we have the list of pathnames),
503                  * the only supported value is "!"
504                  */
505                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
506                 if (!isoption && pathend != -1) {
507                         fprintf(stderr, "err: %s: filename|dirname must either "
508                                         "precede options or follow options\n",
509                                         argv[0]);
510                         return CMD_HELP;
511                 }
512                 if (!isoption && pathstart == -1)
513                         pathstart = optind - 1;
514                 if (isoption && pathstart != -1 && pathend == -1) {
515                         pathend = optind - 2;
516                         if ((c == 1 && strcmp(optarg, "!") == 0) ||
517                             c == 'P' || c == 'p' || c == 'O' ||
518                             c == 'q' || c == 'r' || c == 'v')
519                                 pathend = optind - 1;
520                 }
521                 switch (c) {
522                 case 0:
523                         /* Long options. */
524                         break;
525                 case 1:
526                         /* unknown; opt is "!" or path component,
527                          * checking done above.
528                          */
529                         if (strcmp(optarg, "!") == 0)
530                                 neg_opt = 2;
531                         break;
532                 case 'A':
533                         xtime = &param.atime;
534                         xsign = &param.asign;
535                 case 'C':
536                         if (c == 'C') {
537                                 xtime = &param.ctime;
538                                 xsign = &param.csign;
539                         }
540                 case 'M':
541                         if (c == 'M') {
542                                 xtime = &param.mtime;
543                                 xsign = &param.msign;
544                         }
545                         new_fashion = 1;
546                         if (neg_opt) {
547                                 if (optarg[0] == '-')
548                                         optarg[0] = '+';
549                                 else if (optarg[0] == '+')
550                                         optarg[0] = '-';
551                                 else {
552                                         str[0] = '-';
553                                         str[1] = '\0';
554                                         strcat(str, optarg);
555                                         optarg = str;
556                                 }
557                         }
558                         ret = set_time(&t, xtime, optarg);
559                         if (ret == INT_MAX)
560                                 return -1;
561                         if (ret)
562                                 *xsign = ret;
563                         break;
564                 case 'D':
565                         new_fashion = 1;
566                         param.maxdepth = strtol(optarg, 0, 0);
567                         break;
568                 case 'g':
569                         new_fashion = 1;
570                         param.gid = strtol(optarg, &endptr, 10);
571                         if (optarg == endptr) {
572                                 fprintf(stderr, "Bad gid: %s\n", optarg);
573                                 return CMD_HELP;
574                         }
575                         param.exclude_gid = !!neg_opt;
576                         param.check_gid = 1;
577                         break;
578                 case 'G':
579                         new_fashion = 1;
580                         param.gid = strtol(optarg, &endptr, 10);
581                         if (optarg == endptr) {
582                                 ret = name2id(&param.gid, optarg, GRPQUOTA);
583                                 if (ret != 0) {
584                                         fprintf(stderr, "Group/GID: %s cannot "
585                                                 "be found.\n", optarg);
586                                         return -1;
587                                 }
588                         }
589                         param.exclude_gid = !!neg_opt;
590                         param.check_gid = 1;
591                         break;
592                 case 'u':
593                         new_fashion = 1;
594                         param.uid = strtol(optarg, &endptr, 10);
595                         if (optarg == endptr) {
596                                 fprintf(stderr, "Bad uid: %s\n", optarg);
597                                 return CMD_HELP;
598                         }
599                         param.exclude_uid = !!neg_opt;
600                         param.check_uid = 1;
601                         break;
602                 case 'U':
603                         new_fashion = 1;
604                         param.uid = strtol(optarg, &endptr, 10);
605                         if (optarg == endptr) {
606                                 ret = name2id(&param.uid, optarg, USRQUOTA);
607                                 if (ret != 0) {
608                                         fprintf(stderr, "User/UID: %s cannot "
609                                                 "be found.\n", optarg);
610                                         return -1;
611                                 }
612                         }
613                         param.exclude_uid = !!neg_opt;
614                         param.check_uid = 1;
615                         break;
616                 case FIND_POOL_OPT:
617                         new_fashion = 1;
618                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
619                                 fprintf(stderr,
620                                         "Pool name %s is too long"
621                                         " (max is %d)\n", optarg,
622                                         LOV_MAXPOOLNAME);
623                                 return -1;
624                         }
625                         /* we do check for empty pool because empty pool
626                          * is used to find V1 lov attributes */
627                         strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
628                         param.poolname[LOV_MAXPOOLNAME] = '\0';
629                         param.exclude_pool = !!neg_opt;
630                         param.check_pool = 1;
631                         break;
632                 case 'n':
633                         new_fashion = 1;
634                         param.pattern = (char *)optarg;
635                         param.exclude_pattern = !!neg_opt;
636                         break;
637                 case 'O': {
638                         char *buf, *token, *next, *p;
639                         int len;
640
641                         len = strlen((char *)optarg);
642                         buf = malloc(len+1);
643                         if (buf == NULL)
644                                 return -ENOMEM;
645                         strcpy(buf, (char *)optarg);
646
647                         if (param.num_alloc_obds == 0) {
648                                 param.obduuid = malloc(FIND_MAX_OSTS *
649                                                        sizeof(struct obd_uuid));
650                                 if (param.obduuid == NULL)
651                                         return -ENOMEM;
652                                 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
653                         }
654
655                         for (token = buf; token && *token; token = next) {
656                                 p = strchr(token, ',');
657                                 next = 0;
658                                 if (p) {
659                                         *p = 0;
660                                         next = p+1;
661                                 }
662                                 strcpy((char *)&param.obduuid[param.num_obds++].uuid,
663                                        token);
664                         }
665
666                         if (buf)
667                                 free(buf);
668                         break;
669                 }
670                 case 'p':
671                         new_fashion = 1;
672                         param.zeroend = 1;
673                         break;
674                 case 'P':
675                         break;
676                 case 'q':
677                         new_fashion = 0;
678                         param.quiet++;
679                         param.verbose = 0;
680                         break;
681                 case 'r':
682                         new_fashion = 0;
683                         param.recursive = 1;
684                         break;
685                 case 't':
686                         param.exclude_type = !!neg_opt;
687                         switch(optarg[0]) {
688                         case 'b': param.type = S_IFBLK; break;
689                         case 'c': param.type = S_IFCHR; break;
690                         case 'd': param.type = S_IFDIR; break;
691                         case 'f': param.type = S_IFREG; break;
692                         case 'l': param.type = S_IFLNK; break;
693                         case 'p': param.type = S_IFIFO; break;
694                         case 's': param.type = S_IFSOCK; break;
695 #ifdef S_IFDOOR /* Solaris only */
696                         case 'D': param.type = S_IFDOOR; break;
697 #endif
698                         default: fprintf(stderr, "error: %s: bad type '%s'\n",
699                                          argv[0], optarg);
700                                  return CMD_HELP;
701                         };
702                         break;
703                 case 's':
704                         if (neg_opt) {
705                                 if (optarg[0] == '-')
706                                         optarg[0] = '+';
707                                 else if (optarg[0] == '+')
708                                         optarg[0] = '-';
709                                 else {
710                                         str[0] = '-';
711                                         str[1] = '\0';
712                                         strcat(str, optarg);
713                                         optarg = str;
714                                 }
715                         }
716                         if (optarg[0] == '+')
717                                 param.size_sign = -1;
718                         else if (optarg[0] == '-')
719                                 param.size_sign = +1;
720
721                         if (param.size_sign)
722                                 optarg++;
723                         ret = parse_size(optarg, &param.size,&param.size_units);
724                         if (ret) {
725                                 fprintf(stderr,"error: bad size '%s'\n",
726                                         optarg);
727                                 return ret;
728                         }
729                         break;
730                 case 'v':
731                         new_fashion = 0;
732                         param.verbose++;
733                         param.quiet = 0;
734                         break;
735                 case '?':
736                         return CMD_HELP;
737                 default:
738                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
739                                 argv[0], argv[optind - 1]);
740                         return CMD_HELP;
741                 };
742         }
743
744         if (pathstart == -1) {
745                 fprintf(stderr, "error: %s: no filename|pathname\n",
746                         argv[0]);
747                 return CMD_HELP;
748         } else if (pathend == -1) {
749                 /* no options */
750                 pathend = argc;
751         }
752
753         if (new_fashion) {
754                 param.quiet = 1;
755         } else {
756                 static int deprecated_warning;
757                 if (!deprecated_warning) {
758                         fprintf(stderr, "lfs find: -q, -r, -v options "
759                                 "deprecated.  Use 'lfs getstripe' instead.\n");
760                         deprecated_warning = 1;
761                 }
762                 if (!param.recursive && param.maxdepth == -1)
763                         param.maxdepth = 1;
764         }
765
766         do {
767                 if (new_fashion)
768                         ret = llapi_find(argv[pathstart], &param);
769                 else
770                         ret = llapi_getstripe(argv[pathstart], &param);
771         } while (++pathstart < pathend && !ret);
772
773         if (ret)
774                 fprintf(stderr, "error: %s failed for %s.\n",
775                         argv[0], argv[optind - 1]);
776
777         if (param.obduuid && param.num_alloc_obds)
778                 free(param.obduuid);
779
780         return ret;
781 }
782
783 static int lfs_getstripe(int argc, char **argv)
784 {
785         struct option long_opts[] = {
786                 {"obd", 1, 0, 'O'},
787                 {"quiet", 0, 0, 'q'},
788                 {"recursive", 0, 0, 'r'},
789                 {"verbose", 0, 0, 'v'},
790                 {0, 0, 0, 0}
791         };
792         char short_opts[] = "hO:qrv";
793         int c, rc;
794         struct find_param param = { 0 };
795
796         optind = 0;
797         while ((c = getopt_long(argc, argv, short_opts,
798                                 long_opts, NULL)) != -1) {
799                 switch (c) {
800                 case 'O':
801                         if (param.obduuid) {
802                                 fprintf(stderr,
803                                         "error: %s: only one obduuid allowed",
804                                         argv[0]);
805                                 return CMD_HELP;
806                         }
807                         param.obduuid = (struct obd_uuid *)optarg;
808                         break;
809                 case 'q':
810                         param.quiet++;
811                         param.verbose = 0;
812                         break;
813                 case 'r':
814                         param.recursive = 1;
815                         break;
816                 case 'v':
817                         param.verbose++;
818                         param.quiet = 0;
819                         break;
820                 case '?':
821                         return CMD_HELP;
822                 default:
823                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
824                                 argv[0], argv[optind - 1]);
825                         return CMD_HELP;
826                 }
827         }
828
829         if (optind >= argc)
830                 return CMD_HELP;
831
832         param.maxdepth = param.recursive ? -1 : 1;
833
834         do {
835                 rc = llapi_getstripe(argv[optind], &param);
836         } while (++optind < argc && !rc);
837
838         if (rc)
839                 fprintf(stderr, "error: %s failed for %s.\n",
840                         argv[0], argv[optind - 1]);
841         return rc;
842 }
843
844 static int lfs_osts(int argc, char **argv)
845 {
846         FILE *fp;
847         struct mntent *mnt = NULL;
848         struct find_param param;
849         int rc=0;
850
851         if (argc != 1)
852                 return CMD_HELP;
853
854         fp = setmntent(MOUNTED, "r");
855
856         if (fp == NULL) {
857                  fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
858                         strerror (errno));
859         } else {
860                 mnt = getmntent(fp);
861                 while (feof(fp) == 0 && ferror(fp) ==0) {
862                         memset(&param, 0, sizeof(param));
863                         if (llapi_is_lustre_mnt(mnt)) {
864                                 rc = llapi_getstripe(mnt->mnt_dir, &param);
865                                 if (rc)
866                                         fprintf(stderr,
867                                                "error: %s: failed on %s\n",
868                                                argv[0], mnt->mnt_dir);
869                         }
870                         mnt = getmntent(fp);
871                 }
872                 endmntent(fp);
873         }
874
875         return rc;
876 }
877
878 #define COOK(value)                                                     \
879 ({                                                                      \
880         int radix = 0;                                                  \
881         while (value > 1024) {                                          \
882                 value /= 1024;                                          \
883                 radix++;                                                \
884         }                                                               \
885         radix;                                                          \
886 })
887 #define UUF     "%-20s"
888 #define CSF     "%9s"
889 #define CDF     "%9llu"
890 #define HSF     "%8s"
891 #define HDF     "%6.1f"
892 #define RSF     "%5s"
893 #define RDF     "%4d%%"
894
895 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
896 {
897         char rpath[PATH_MAX] = {'\0'};
898         struct mntent *mnt;
899         int rc, len, out_len = 0;
900
901         if (!realpath(path, rpath)) {
902                 rc = -errno;
903                 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
904                         path, strerror(-rc));
905                 return rc;
906         }
907
908         len = 0;
909         mnt = getmntent(fp);
910         while (feof(fp) == 0 && ferror(fp) == 0) {
911                 if (llapi_is_lustre_mnt(mnt)) {
912                         len = strlen(mnt->mnt_dir);
913                         if (len > out_len &&
914                             !strncmp(rpath, mnt->mnt_dir, len)) {
915                                 out_len = len;
916                                 memset(mntdir, 0, dir_len);
917                                 strncpy(mntdir, mnt->mnt_dir, dir_len);
918                         }
919                 }
920                 mnt = getmntent(fp);
921         }
922
923         if (out_len > 0)
924                 return 0;
925
926         fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
927         return -EINVAL;
928 }
929
930 static int showdf(char *mntdir, struct obd_statfs *stat,
931                   char *uuid, int ishow, int cooked,
932                   char *type, int index, int rc)
933 {
934         long long avail, used, total;
935         double ratio = 0;
936         char *suffix = "KMGTPEZY";
937         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
938         char tbuf[20], ubuf[20], abuf[20], rbuf[20];
939
940         if (!uuid || !stat)
941                 return -EINVAL;
942
943         switch (rc) {
944         case 0:
945                 if (ishow) {
946                         avail = stat->os_ffree;
947                         used = stat->os_files - stat->os_ffree;
948                         total = stat->os_files;
949                 } else {
950                         int shift = cooked ? 0 : 10;
951
952                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
953                         used  = ((stat->os_blocks - stat->os_bfree) *
954                                  stat->os_bsize) >> shift;
955                         total = (stat->os_blocks * stat->os_bsize) >> shift;
956                 }
957
958                 if (total > 0)
959                         ratio = (double)used / (double)total;
960
961                 if (cooked) {
962                         int i;
963                         double cook_val;
964
965                         cook_val = (double)total;
966                         i = COOK(cook_val);
967                         if (i > 0)
968                                 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
969                         else
970                                 sprintf(tbuf, CDF, total);
971
972                         cook_val = (double)used;
973                         i = COOK(cook_val);
974                         if (i > 0)
975                                 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
976                         else
977                                 sprintf(ubuf, CDF, used);
978
979                         cook_val = (double)avail;
980                         i = COOK(cook_val);
981                         if (i > 0)
982                                 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
983                         else
984                                 sprintf(abuf, CDF, avail);
985                 } else {
986                         sprintf(tbuf, CDF, total);
987                         sprintf(ubuf, CDF, used);
988                         sprintf(abuf, CDF, avail);
989                 }
990
991                 sprintf(rbuf, RDF, (int)(ratio * 100));
992                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
993                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
994                 if (type)
995                         printf("[%s:%d]\n", type, index);
996                 else
997                         printf("\n");
998
999                 break;
1000         case -ENODATA:
1001                 printf(UUF": inactive device\n", uuid);
1002                 break;
1003         default:
1004                 printf(UUF": %s\n", uuid, strerror(-rc));
1005                 break;
1006         }
1007
1008         return 0;
1009 }
1010
1011 static int mntdf(char *mntdir, int ishow, int cooked)
1012 {
1013         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1014         struct obd_uuid uuid_buf;
1015         __u32 index;
1016         int rc;
1017
1018         if (ishow)
1019                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1020                        "UUID", "Inodes", "IUsed", "IFree",
1021                        "IUse%", "Mounted on");
1022         else
1023                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1024                        "UUID", cooked ? "bytes" : "1K-blocks",
1025                        "Used", "Available", "Use%", "Mounted on");
1026
1027         for (index = 0; ; index++) {
1028                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1029                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1030                 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1031                                       &stat_buf, &uuid_buf);
1032                 if (rc == -ENODEV)
1033                         break;
1034
1035                 if (rc == -EAGAIN)
1036                         continue;
1037
1038                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1039                     rc == -ENODATA || rc == 0) {
1040                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
1041                                "MDT", index, rc);
1042                 } else {
1043                         fprintf(stderr,
1044                                 "error: llapi_obd_statfs(%s): %s (%d)\n",
1045                                 uuid_buf.uuid, strerror(-rc), rc);
1046                         return rc;
1047                 }
1048                 if (rc == 0) {
1049                         sum.os_ffree += stat_buf.os_ffree;
1050                         sum.os_files += stat_buf.os_files;
1051                 }
1052         }
1053
1054         for (index = 0; ; index++) {
1055                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1056                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1057                 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1058                                       &stat_buf, &uuid_buf);
1059                 if (rc == -ENODEV)
1060                         break;
1061
1062                 if (rc == -EAGAIN)
1063                         continue;
1064
1065                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1066                     rc == -ENODATA || rc == 0) {
1067                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
1068                                "OST", index, rc);
1069                 } else {
1070                         fprintf(stderr,
1071                                 "error: llapi_obd_statfs failed: %s (%d)\n",
1072                                 strerror(-rc), rc);
1073                         return rc;
1074                 }
1075                 if (rc == 0) {
1076                         sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1077                         sum.os_bfree  += stat_buf.os_bfree * stat_buf.os_bsize;
1078                         sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1079                 }
1080         }
1081
1082         printf("\n");
1083         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1084
1085         return 0;
1086 }
1087
1088 static int lfs_df(int argc, char **argv)
1089 {
1090         FILE *fp;
1091         char *path = NULL;
1092         struct mntent *mnt = NULL;
1093         char *mntdir = NULL;
1094         int ishow = 0, cooked = 0;
1095         int c, rc = 0;
1096
1097         optind = 0;
1098         while ((c = getopt(argc, argv, "ih")) != -1) {
1099                 switch (c) {
1100                 case 'i':
1101                         ishow = 1;
1102                         break;
1103                 case 'h':
1104                         cooked = 1;
1105                         break;
1106                 default:
1107                         return CMD_HELP;
1108                 }
1109         }
1110         if (optind < argc )
1111                 path = argv[optind];
1112
1113         fp = setmntent(MOUNTED, "r");
1114         if (fp == NULL) {
1115                 rc = -errno;
1116                 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1117                         argv[0], MOUNTED, strerror(errno));
1118                 return rc;
1119         }
1120
1121         if ((mntdir = malloc(PATH_MAX)) == NULL) {
1122                 fprintf(stderr, "error: cannot allocate %d bytes\n",
1123                         PATH_MAX);
1124                 return -ENOMEM;
1125         }
1126         memset(mntdir, 0, PATH_MAX);
1127
1128         if (path) {
1129                 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1130                 if (rc) {
1131                         endmntent(fp);
1132                         free(mntdir);
1133                         return rc;
1134                 }
1135
1136                 rc = mntdf(mntdir, ishow, cooked);
1137                 printf("\n");
1138                 endmntent(fp);
1139         } else {
1140                 mnt = getmntent(fp);
1141                 while (feof(fp) == 0 && ferror(fp) == 0) {
1142                         if (llapi_is_lustre_mnt(mnt)) {
1143                                 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1144                                 if (rc)
1145                                         break;
1146                                 printf("\n");
1147                         }
1148                         mnt = getmntent(fp);
1149                 }
1150                 endmntent(fp);
1151         }
1152
1153         free(mntdir);
1154         return rc;
1155 }
1156
1157 static int lfs_check(int argc, char **argv)
1158 {
1159         int rc;
1160         FILE *fp;
1161         struct mntent *mnt = NULL;
1162         int num_types = 1;
1163         char *obd_types[2];
1164         char obd_type1[4];
1165         char obd_type2[4];
1166
1167         if (argc != 2)
1168                 return CMD_HELP;
1169
1170         obd_types[0] = obd_type1;
1171         obd_types[1] = obd_type2;
1172
1173         if (strcmp(argv[1], "osts") == 0) {
1174                 strcpy(obd_types[0], "osc");
1175         } else if (strcmp(argv[1], "mds") == 0) {
1176                 strcpy(obd_types[0], "mdc");
1177         } else if (strcmp(argv[1], "servers") == 0) {
1178                 num_types = 2;
1179                 strcpy(obd_types[0], "osc");
1180                 strcpy(obd_types[1], "mdc");
1181         } else {
1182                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1183                                 argv[0], argv[1]);
1184                         return CMD_HELP;
1185         }
1186
1187         fp = setmntent(MOUNTED, "r");
1188         if (fp == NULL) {
1189                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1190                         strerror (errno));
1191         } else {
1192                 mnt = getmntent(fp);
1193                 while (feof(fp) == 0 && ferror(fp) ==0) {
1194                         if (llapi_is_lustre_mnt(mnt))
1195                                 break;
1196                         mnt = getmntent(fp);
1197                 }
1198                 endmntent(fp);
1199         }
1200
1201         if (!mnt) {
1202                 fprintf(stderr, "No suitable Lustre mount found\n");
1203                 return -1;
1204         }
1205
1206         rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1207
1208         if (rc)
1209                 fprintf(stderr, "error: %s: %s status failed\n",
1210                                 argv[0],argv[1]);
1211
1212         return rc;
1213
1214 }
1215
1216 static int lfs_catinfo(int argc, char **argv)
1217 {
1218         FILE *fp;
1219         struct mntent *mnt = NULL;
1220         int rc;
1221
1222         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1223                 return CMD_HELP;
1224
1225         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1226                 return CMD_HELP;
1227
1228         fp = setmntent(MOUNTED, "r");
1229         if (fp == NULL) {
1230                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1231                          strerror(errno));
1232         } else {
1233                 mnt = getmntent(fp);
1234                 while (feof(fp) == 0 && ferror(fp) == 0) {
1235                         if (llapi_is_lustre_mnt(mnt))
1236                                 break;
1237                         mnt = getmntent(fp);
1238                 }
1239                 endmntent(fp);
1240         }
1241
1242         if (mnt) {
1243                 if (argc == 3)
1244                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1245                 else
1246                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1247         } else {
1248                 fprintf(stderr, "no lustre_lite mounted.\n");
1249                 rc = -1;
1250         }
1251
1252         return rc;
1253 }
1254
1255 int lfs_join(int argc, char **argv)
1256 {
1257         char *name_head, *name_tail;
1258         int fd, rc;
1259         loff_t size;
1260
1261         if (argc != 3)
1262                 return CMD_HELP;
1263         name_head = argv[1];
1264         fd = open(name_head, O_WRONLY);
1265         if (fd < 0) {
1266                 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1267                         name_head, fd);
1268                 return fd;
1269         }
1270         size = lseek(fd, 0, SEEK_END);
1271         if (size % JOIN_FILE_ALIGN) {
1272                 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1273                         name_head, (long long)size, JOIN_FILE_ALIGN);
1274                 rc = -EINVAL;
1275                 goto out;
1276         }
1277         name_tail = argv[2];
1278         rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1279 out:
1280         close(fd);
1281         if (rc) {
1282                 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1283                         argv[1], argv[2]);
1284         }
1285         return rc;
1286 }
1287
1288 #ifdef HAVE_QUOTA_SUPPORT
1289 static int lfs_quotachown(int argc, char **argv)
1290 {
1291
1292         int c,rc;
1293         int flag = 0;
1294
1295         optind = 0;
1296         while ((c = getopt(argc, argv, "i")) != -1) {
1297                 switch (c) {
1298                 case 'i':
1299                         flag++;
1300                         break;
1301                 default:
1302                         fprintf(stderr, "error: %s: option '-%c' "
1303                                         "unrecognized\n", argv[0], c);
1304                         return CMD_HELP;
1305                 }
1306         }
1307         if (optind == argc)
1308                 return CMD_HELP;
1309         rc = llapi_quotachown(argv[optind], flag);
1310         if(rc)
1311                 fprintf(stderr,"error: change file owner/group failed.\n");
1312         return rc;
1313 }
1314
1315
1316 static int lfs_quotacheck(int argc, char **argv)
1317 {
1318         int c, check_type = 0;
1319         char *mnt;
1320         struct if_quotacheck qchk;
1321         struct if_quotactl qctl;
1322         char *obd_type = qchk.obd_type;
1323         char *obd_uuid = qchk.obd_uuid.uuid;
1324         int rc;
1325
1326         memset(&qchk, 0, sizeof(qchk));
1327
1328         optind = 0;
1329         while ((c = getopt(argc, argv, "ug")) != -1) {
1330                 switch (c) {
1331                 case 'u':
1332                         check_type |= 0x01;
1333                         break;
1334                 case 'g':
1335                         check_type |= 0x02;
1336                         break;
1337                 default:
1338                         fprintf(stderr, "error: %s: option '-%c' "
1339                                         "unrecognized\n", argv[0], c);
1340                         return CMD_HELP;
1341                 }
1342         }
1343
1344         if (check_type)
1345                 check_type--;
1346         else    /* do quotacheck for both user & group quota by default */
1347                 check_type = 0x02;
1348
1349         if (argc == optind)
1350                 return CMD_HELP;
1351
1352         mnt = argv[optind];
1353
1354         memset(&qctl, 0, sizeof(qctl));
1355         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1356         qctl.qc_id = QFMT_LDISKFS;
1357         qctl.qc_type = check_type;
1358         rc = llapi_quotactl(mnt, &qctl);
1359         if (rc) {
1360                 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1361                 return rc;
1362         }
1363
1364         rc = llapi_quotacheck(mnt, check_type);
1365         if (rc) {
1366                 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1367                 return rc;
1368         }
1369
1370         rc = llapi_poll_quotacheck(mnt, &qchk);
1371         if (rc) {
1372                 if (*obd_type)
1373                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1374                 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1375                 return rc;
1376         }
1377
1378         memset(&qctl, 0, sizeof(qctl));
1379         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1380         qctl.qc_id = QFMT_LDISKFS;
1381         qctl.qc_type = check_type;
1382         rc = llapi_quotactl(mnt, &qctl);
1383         if (rc) {
1384                 if (*obd_type)
1385                         fprintf(stderr, "%s %s ",
1386                                 qctl.obd_type, qctl.obd_uuid.uuid);
1387                 fprintf(stderr, "%s turn on quota failed: %s\n",
1388                         argv[0], strerror(errno));
1389                 return rc;
1390         }
1391
1392         return 0;
1393 }
1394
1395 static int lfs_quotaon(int argc, char **argv)
1396 {
1397         int c;
1398         char *mnt;
1399         struct if_quotactl qctl;
1400         char *obd_type = qctl.obd_type;
1401         char *obd_uuid = qctl.obd_uuid.uuid;
1402         int rc;
1403
1404         memset(&qctl, 0, sizeof(qctl));
1405         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1406         qctl.qc_id = QFMT_LDISKFS;
1407
1408         optind = 0;
1409         while ((c = getopt(argc, argv, "ugf")) != -1) {
1410                 switch (c) {
1411                 case 'u':
1412                         qctl.qc_type |= 0x01;
1413                         break;
1414                 case 'g':
1415                         qctl.qc_type |= 0x02;
1416                         break;
1417                 case 'f':
1418                         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1419                         break;
1420                 default:
1421                         fprintf(stderr, "error: %s: option '-%c' "
1422                                         "unrecognized\n", argv[0], c);
1423                         return CMD_HELP;
1424                 }
1425         }
1426
1427         if (qctl.qc_type)
1428                 qctl.qc_type--;
1429         else /* by default, enable quota for both user & group */
1430                 qctl.qc_type = 0x02;
1431
1432         if (argc == optind)
1433                 return CMD_HELP;
1434
1435         mnt = argv[optind];
1436
1437         rc = llapi_quotactl(mnt, &qctl);
1438         if (rc) {
1439                 if (*obd_type)
1440                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1441                 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1442                 return rc;
1443         }
1444
1445         return 0;
1446 }
1447
1448 static int lfs_quotaoff(int argc, char **argv)
1449 {
1450         int c;
1451         char *mnt;
1452         struct if_quotactl qctl;
1453         char *obd_type = qctl.obd_type;
1454         char *obd_uuid = qctl.obd_uuid.uuid;
1455         int rc;
1456
1457         memset(&qctl, 0, sizeof(qctl));
1458         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1459
1460         optind = 0;
1461         while ((c = getopt(argc, argv, "ug")) != -1) {
1462                 switch (c) {
1463                 case 'u':
1464                         qctl.qc_type |= 0x01;
1465                         break;
1466                 case 'g':
1467                         qctl.qc_type |= 0x02;
1468                         break;
1469                 default:
1470                         fprintf(stderr, "error: %s: option '-%c' "
1471                                         "unrecognized\n", argv[0], c);
1472                         return CMD_HELP;
1473                 }
1474         }
1475
1476         if (qctl.qc_type)
1477                 qctl.qc_type--;
1478         else /* by default, disable quota for both user & group */
1479                 qctl.qc_type = 0x02;
1480
1481         if (argc == optind)
1482                 return CMD_HELP;
1483
1484         mnt = argv[optind];
1485
1486         rc = llapi_quotactl(mnt, &qctl);
1487         if (rc) {
1488                 if (*obd_type)
1489                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1490                 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1491                 return rc;
1492         }
1493
1494         return 0;
1495 }
1496
1497 #define ARG2INT(nr, str, msg)                                           \
1498 do {                                                                    \
1499         char *endp;                                                     \
1500         nr = strtol(str, &endp, 0);                                     \
1501         if (*endp) {                                                    \
1502                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
1503                 return CMD_HELP;                                        \
1504         }                                                               \
1505 } while (0)
1506
1507 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1508
1509 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1510  * returns the value or ULONG_MAX on integer overflow or incorrect format
1511  * Notes:
1512  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1513  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1514  *        3. empty integer value is interpreted as 0
1515  */
1516
1517 static unsigned long str2sec(const char* timestr) {
1518         const char spec[] = "smhdw";
1519         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1520         unsigned long val = 0;
1521         char *tail;
1522
1523         if (strpbrk(timestr, spec) == NULL) {
1524                 /* no specifiers inside the time string,
1525                    should treat it as an integer value */
1526                 val = strtoul(timestr, &tail, 10);
1527                 return *tail ? ULONG_MAX : val;
1528         }
1529
1530         /* format string is XXwXXdXXhXXmXXs */
1531         while (*timestr) {
1532                 unsigned long v;
1533                 int ind;
1534                 char* ptr;
1535
1536                 v = strtoul(timestr, &tail, 10);
1537                 if (v == ULONG_MAX || *tail == '\0')
1538                         /* value too large (ULONG_MAX or more)
1539                            or missing specifier */
1540                         goto error;
1541
1542                 ptr = strchr(spec, *tail);
1543                 if (ptr == NULL)
1544                         /* unknown specifier */
1545                         goto error;
1546
1547                 ind = ptr - spec;
1548
1549                 /* check if product will overflow the type */
1550                 if (!(v < ULONG_MAX / mult[ind]))
1551                         goto error;
1552
1553                 ADD_OVERFLOW(val, mult[ind] * v);
1554                 if (val == ULONG_MAX)
1555                         goto error;
1556
1557                 timestr = tail + 1;
1558         }
1559
1560         return val;
1561
1562 error:
1563         return ULONG_MAX;
1564 }
1565
1566 int lfs_setquota(int argc, char **argv)
1567 {
1568         int c;
1569         char *mnt;
1570         struct if_quotactl qctl;
1571         char *obd_type = qctl.obd_type;
1572         char *obd_uuid = qctl.obd_uuid.uuid;
1573         int rc;
1574
1575         memset(&qctl, 0, sizeof(qctl));
1576         qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1577
1578         optind = 0;
1579         while ((c = getopt(argc, argv, "ugt")) != -1) {
1580                 switch (c) {
1581                 case 'u':
1582                         qctl.qc_type |= 0x01;
1583                         break;
1584                 case 'g':
1585                         qctl.qc_type |= 0x02;
1586                         break;
1587                 case 't':
1588                         qctl.qc_cmd = LUSTRE_Q_SETINFO;
1589                         break;
1590                 default:
1591                         fprintf(stderr, "error: %s: option '-%c' "
1592                                         "unrecognized\n", argv[0], c);
1593                         return CMD_HELP;
1594                 }
1595         }
1596
1597         if (qctl.qc_type)
1598                 qctl.qc_type--;
1599
1600         if (qctl.qc_type == UGQUOTA) {
1601                 fprintf(stderr, "error: user and group quotas can't be set "
1602                                 "both\n");
1603                 return CMD_HELP;
1604         }
1605
1606         if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1607                 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1608
1609                 if (optind + 6 != argc)
1610                         return CMD_HELP;
1611
1612                 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1613                 if (rc) {
1614                         fprintf(stderr, "error: find id for name %s failed: %s\n",
1615                                 argv[optind - 1], strerror(errno));
1616                         return CMD_HELP;
1617                 }
1618
1619                 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1620                 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1621                 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1622                 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1623
1624                 dqb->dqb_valid = QIF_LIMITS;
1625         } else {
1626                 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1627
1628                 if (optind + 3 != argc)
1629                         return CMD_HELP;
1630
1631                 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1632                         fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1633                         return CMD_HELP;
1634                 }
1635                 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1636                         fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1637                         return CMD_HELP;
1638                 }
1639         }
1640
1641         mnt = argv[optind];
1642
1643         rc = llapi_quotactl(mnt, &qctl);
1644         if (rc) {
1645                 if (*obd_type)
1646                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1647                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1648                 return rc;
1649         }
1650
1651         return 0;
1652 }
1653
1654 static inline char *type2name(int check_type)
1655 {
1656         if (check_type == USRQUOTA)
1657                 return "user";
1658         else if (check_type == GRPQUOTA)
1659                 return "group";
1660         else
1661                 return "unknown";
1662 }
1663
1664
1665 /* Converts seconds value into format string
1666  * result is returned in buf
1667  * Notes:
1668  *        1. result is in descenting order: 1w2d3h4m5s
1669  *        2. zero fields are not filled (except for p. 3): 5d1s
1670  *        3. zero seconds value is presented as "0s"
1671  */
1672 static void sec2str(time_t seconds, char *buf)
1673 {
1674         const char spec[] = "smhdw";
1675         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1676         unsigned long c;
1677         char* tail = buf;
1678         int i;
1679
1680         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1681                 c = seconds / mult[i];
1682
1683                 if (c > 0 || (i == 0 && buf == tail))
1684                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1685
1686                 seconds %= mult[i];
1687         }
1688 }
1689
1690
1691 static void diff2str(time_t seconds, char *buf, time_t now)
1692 {
1693
1694         buf[0] = 0;
1695         if (!seconds)
1696                 return;
1697         if (seconds <= now) {
1698                 strcpy(buf, "none");
1699                 return;
1700         }
1701         sec2str(seconds - now, buf);
1702 }
1703
1704 static void print_quota_title(char *name, struct if_quotactl *qctl)
1705 {
1706         printf("Disk quotas for %s %s (%cid %u):\n",
1707                type2name(qctl->qc_type), name,
1708                *type2name(qctl->qc_type), qctl->qc_id);
1709         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1710                "Filesystem",
1711                "kbytes", "quota", "limit", "grace",
1712                "files", "quota", "limit", "grace");
1713 }
1714
1715 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1716 {
1717         time_t now;
1718
1719         time(&now);
1720
1721         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1722                 int bover = 0, iover = 0;
1723                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1724
1725                 if (dqb->dqb_bhardlimit &&
1726                     toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1727                         bover = 1;
1728                 } else if (dqb->dqb_bsoftlimit &&
1729                            toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1730                         if (dqb->dqb_btime > now) {
1731                                 bover = 2;
1732                         } else {
1733                                 bover = 3;
1734                         }
1735                 }
1736
1737                 if (dqb->dqb_ihardlimit &&
1738                     dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1739                         iover = 1;
1740                 } else if (dqb->dqb_isoftlimit &&
1741                            dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1742                         if (dqb->dqb_btime > now) {
1743                                 iover = 2;
1744                         } else {
1745                                 iover = 3;
1746                         }
1747                 }
1748
1749 #if 0           /* XXX: always print quotas even when no usages */
1750                 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1751 #endif
1752                 {
1753                         char numbuf[3][32];
1754                         char timebuf[40];
1755
1756                         if (strlen(mnt) > 15)
1757                                 printf("%s\n%15s", mnt, "");
1758                         else
1759                                 printf("%15s", mnt);
1760
1761                         if (bover)
1762                                 diff2str(dqb->dqb_btime, timebuf, now);
1763
1764                         sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1765                         sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1766                         sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1767                         printf(" %7s%c %6s %7s %7s",
1768                                numbuf[0], bover ? '*' : ' ', numbuf[1],
1769                                numbuf[2], bover > 1 ? timebuf : "");
1770
1771                         if (iover)
1772                                 diff2str(dqb->dqb_itime, timebuf, now);
1773
1774                         sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1775                         sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1776                         sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1777                         if (!ost_only)
1778                                 printf(" %7s%c %6s %7s %7s",
1779                                        numbuf[0], iover ? '*' : ' ', numbuf[1],
1780                                        numbuf[2], iover > 1 ? timebuf : "");
1781                         printf("\n");
1782                 }
1783         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1784                    qctl->qc_cmd == Q_GETOINFO) {
1785                 char bgtimebuf[40];
1786                 char igtimebuf[40];
1787
1788                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1789                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1790                 printf("Block grace time: %s; Inode grace time: %s\n",
1791                        bgtimebuf, igtimebuf);
1792         }
1793 }
1794
1795 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1796 {
1797         int rc;
1798
1799         /* XXX: this is a flag to mark that only mds quota is wanted */
1800         qctl->qc_dqblk.dqb_valid = 1;
1801         rc = llapi_quotactl(mnt, qctl);
1802         if (rc) {
1803                 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1804                 return;
1805         }
1806         qctl->qc_dqblk.dqb_valid = 0;
1807
1808         print_quota(qctl->obd_uuid.uuid, qctl, 0);
1809 }
1810
1811 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1812 {
1813         DIR *dir;
1814         struct obd_uuid *uuids = NULL, *uuidp;
1815         int obdcount = 1024;
1816         int i, rc;
1817
1818         dir = opendir(mnt);
1819         if (!dir) {
1820                 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1821                 return;
1822         }
1823
1824         uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1825                                           sizeof(struct obd_uuid));
1826         if (uuids == NULL)
1827                 goto out;
1828
1829 retry_get_uuids:
1830         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1831         if (rc != 0) {
1832                 struct obd_uuid *uuids_temp;
1833
1834                 if (rc == -EOVERFLOW) {
1835                         uuids_temp = realloc(uuids, obdcount *
1836                                              sizeof(struct obd_uuid));
1837                         if (uuids_temp != NULL)
1838                                 goto retry_get_uuids;
1839                         else
1840                                 rc = -ENOMEM;
1841                 }
1842
1843                 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1844                 goto out;
1845         }
1846
1847         for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1848                 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1849
1850                 /* XXX clear this flag to get quota from osts */
1851                 qctl->qc_dqblk.dqb_valid = 0;
1852                 rc = llapi_quotactl(mnt, qctl);
1853                 if (rc) {
1854                         fprintf(stderr, "%s quotactl failed: %s\n",
1855                                 uuidp->uuid, strerror(errno));
1856                         continue;
1857                 }
1858
1859                 print_quota(uuidp->uuid, qctl, 1);
1860         }
1861
1862 out:
1863         closedir(dir);
1864         return;
1865 }
1866
1867 static int lfs_quota(int argc, char **argv)
1868 {
1869         int c;
1870         char *name = NULL, *mnt;
1871         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1872                                     .qc_type = 0x01 };
1873         char *obd_type = qctl.obd_type;
1874         char *obd_uuid = qctl.obd_uuid.uuid;
1875         int rc;
1876
1877         optind = 0;
1878         while ((c = getopt(argc, argv, "ugto:")) != -1) {
1879                 switch (c) {
1880                 case 'u':
1881                         qctl.qc_type = 0x01;
1882                         break;
1883                 case 'g':
1884                         qctl.qc_type = 0x02;
1885                         break;
1886                 case 't':
1887                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
1888                         break;
1889                 case 'o':
1890                         strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1891                         break;
1892                 default:
1893                         fprintf(stderr, "error: %s: option '-%c' "
1894                                         "unrecognized\n", argv[0], c);
1895                         return CMD_HELP;
1896                 }
1897         }
1898
1899         if (qctl.qc_type)
1900                 qctl.qc_type--;
1901
1902
1903         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1904                 if (optind + 2 != argc) {
1905                         fprintf(stderr, "error: missing quota argument(s)\n");
1906                         return CMD_HELP;
1907                 }
1908
1909                 name = argv[optind++];
1910                 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1911                 if (rc) {
1912                         fprintf(stderr,"error: can't find id for name %s: %s\n",
1913                                 name, strerror(errno));
1914                         return CMD_HELP;
1915                 }
1916                 print_quota_title(name, &qctl);
1917         } else if (optind + 1 != argc) {
1918                 fprintf(stderr, "error: missing quota info argument(s)\n");
1919                 return CMD_HELP;
1920         }
1921
1922         mnt = argv[optind];
1923
1924         rc = llapi_quotactl(mnt, &qctl);
1925         if (rc) {
1926                 if (*obd_type)
1927                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1928                 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1929                 return rc;
1930         }
1931
1932         if (!name)
1933                 rc = id2name(&name, getuid(), qctl.qc_type);
1934
1935         if (*obd_uuid) {
1936                 mnt = "";
1937                 name = obd_uuid;
1938         }
1939
1940         print_quota(mnt, &qctl, 0);
1941
1942         if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1943                 print_mds_quota(mnt, &qctl);
1944                 print_lov_quota(mnt, &qctl);
1945         }
1946
1947         return 0;
1948 }
1949 #endif /* HAVE_QUOTA_SUPPORT */
1950
1951 static int flushctx_ioctl(char *mp)
1952 {
1953         int fd, rc;
1954
1955         fd = open(mp, O_RDONLY);
1956         if (fd == -1) {
1957                 fprintf(stderr, "flushctx: error open %s: %s\n",
1958                         mp, strerror(errno));
1959                 return -1;
1960         }
1961
1962         rc = ioctl(fd, LL_IOC_FLUSHCTX);
1963         if (rc == -1)
1964                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1965                         mp, strerror(errno));
1966
1967         close(fd);
1968         return rc;
1969 }
1970
1971 static int lfs_flushctx(int argc, char **argv)
1972 {
1973         int     kdestroy = 0, c;
1974         FILE   *proc;
1975         char    procline[PATH_MAX], *line;
1976         int     rc = 0;
1977
1978         optind = 0;
1979         while ((c = getopt(argc, argv, "k")) != -1) {
1980                 switch (c) {
1981                 case 'k':
1982                         kdestroy = 1;
1983                         break;
1984                 default:
1985                         fprintf(stderr, "error: %s: option '-%c' "
1986                                         "unrecognized\n", argv[0], c);
1987                         return CMD_HELP;
1988                 }
1989         }
1990
1991         if (kdestroy)
1992                 system("kdestroy > /dev/null");
1993
1994         if (optind >= argc) {
1995                 /* flush for all mounted lustre fs. */
1996                 proc = fopen("/proc/mounts", "r");
1997                 if (!proc) {
1998                         fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1999                                 argv[0]);
2000                         return -1;
2001                 }
2002
2003                 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2004                         char dev[PATH_MAX];
2005                         char mp[PATH_MAX];
2006                         char fs[PATH_MAX];
2007
2008                         if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2009                                 fprintf(stderr, "%s: unexpected format in "
2010                                                 "/proc/mounts\n",
2011                                         argv[0]);
2012                                 return -1;
2013                         }
2014
2015                         if (strcmp(fs, "lustre") != 0)
2016                                 continue;
2017                         /* we use '@' to determine it's a client. are there
2018                          * any other better way?
2019                          */
2020                         if (strchr(dev, '@') == NULL)
2021                                 continue;
2022
2023                         if (flushctx_ioctl(mp))
2024                                 rc = -1;
2025                 }
2026         } else {
2027                 /* flush fs as specified */
2028                 while (optind < argc) {
2029                         if (flushctx_ioctl(argv[optind++]))
2030                                 rc = -1;
2031                 }
2032         }
2033
2034         return rc;
2035 }
2036
2037 static int lfs_lsetfacl(int argc, char **argv)
2038 {
2039         argv[0]++;
2040         return(llapi_lsetfacl(argc, argv));
2041 }
2042
2043 static int lfs_lgetfacl(int argc, char **argv)
2044 {
2045         argv[0]++;
2046         return(llapi_lgetfacl(argc, argv));
2047 }
2048
2049 static int lfs_rsetfacl(int argc, char **argv)
2050 {
2051         argv[0]++;
2052         return(llapi_rsetfacl(argc, argv));
2053 }
2054
2055 static int lfs_rgetfacl(int argc, char **argv)
2056 {
2057         argv[0]++;
2058         return(llapi_rgetfacl(argc, argv));
2059 }
2060
2061 static int lfs_cp(int argc, char **argv)
2062 {
2063         return(llapi_cp(argc, argv));
2064 }
2065
2066 static int lfs_ls(int argc, char **argv)
2067 {
2068         return(llapi_ls(argc, argv));
2069 }
2070
2071 int main(int argc, char **argv)
2072 {
2073         int rc;
2074
2075         setlinebuf(stdout);
2076
2077         ptl_initialize(argc, argv);
2078         if (obd_initialize(argc, argv) < 0)
2079                 exit(2);
2080         if (dbg_initialize(argc, argv) < 0)
2081                 exit(3);
2082
2083         Parser_init("lfs > ", cmdlist);
2084
2085         if (argc > 1) {
2086                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2087         } else {
2088                 rc = Parser_commands();
2089         }
2090
2091         obd_finalize(argc, argv);
2092         return rc;
2093 }