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