Whamcloud - gitweb
- merge with 1_5,some fixes.
[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  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Robert Read <rread@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <mntent.h>
32 #include <errno.h>
33 #include <pwd.h>
34 #include <grp.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <dirent.h>
39 #include <time.h>
40
41 #include <lnet/api-support.h>
42 #include <lnet/lnetctl.h>
43
44 #include <liblustre.h>
45 #include <lustre/lustre_idl.h>
46 #include <lustre/liblustreapi.h>
47 #include <lustre/lustre_user.h>
48
49 #include "parser.h"
50 #include "obdctl.h"
51
52 unsigned int libcfs_subsystem_debug = 0;
53
54 /* all functions */
55 static int lfs_setstripe(int argc, char **argv);
56 static int lfs_find(int argc, char **argv);
57 static int lfs_getstripe(int argc, char **argv);
58 static int lfs_osts(int argc, char **argv);
59 static int lfs_df(int argc, char **argv);
60 static int lfs_check(int argc, char **argv);
61 static int lfs_catinfo(int argc, char **argv);
62 #ifdef HAVE_QUOTA_SUPPORT
63 static int lfs_quotachown(int argc, char **argv);
64 static int lfs_quotacheck(int argc, char **argv);
65 static int lfs_quotaon(int argc, char **argv);
66 static int lfs_quotaoff(int argc, char **argv);
67 static int lfs_setquota(int argc, char **argv);
68 static int lfs_quota(int argc, char **argv);
69 #endif
70 static int lfs_join(int argc, char **argv);
71
72 /* all avaialable commands */
73 command_t cmdlist[] = {
74         {"setstripe", lfs_setstripe, 0,
75          "Create a new file with a specific striping pattern or\n"
76          "set the default striping pattern on an existing directory or\n"
77          "delete the default striping pattern from an existing directory\n"
78          "usage: setstripe <filename|dirname> <stripe size> <stripe start> <stripe count>\n"
79          "       or \n"
80          "       setstripe -d <dirname>   (to delete default striping)\n"
81          "\tstripe size:  Number of bytes on each OST (0 filesystem default)\n"
82          "\tstripe start: OST index of first stripe (-1 filesystem default)\n"
83          "\tstripe count: Number of OSTs to stripe over (0 default, -1 all)"},
84         {"getstripe", lfs_getstripe, 0,
85          "To list the striping info for a given filename or files in a\n"
86          "directory or recursively for all files in a directory tree.\n"
87          "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
88          "                 [--recursive | -r] <dir|file> ..."},
89         {"find", lfs_find, 0,
90          "To find files that match given parameters recursively in a directory tree.\n"
91          "usage: find [--atime|-A N] [--mtime|-M N] [--ctime|-C N] [--maxdepth|-D N]\n"
92          "            [--print0|-P] [--print|-p] [--obd|-O <uuid>] <dir/file> ...\n"
93          "If one of the options below is provided, find works the same as 'getstripe':\n"
94          "To list the striping info for a given filename or files in a directory or\n"
95          "recursively.\n"
96          "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
97          "                     [--recursive | -r] <dir|file> ..."},
98         {"check", lfs_check, 0,
99          "Display the status of MDS or OSTs (as specified in the command)\n"
100          "or all the servers (MDS and OSTs).\n"
101          "usage: check <osts|mds|servers>"},
102         {"catinfo", lfs_catinfo, 0,
103          "Show information of specified type logs.\n"
104          "usage: catinfo {keyword} [node name]\n"
105          "\tkeywords are one of followings: config, deletions.\n"
106          "\tnode name must be provided when use keyword config."},
107         {"join", lfs_join, 0,
108          "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
109          "usage: join <filename_A> <filename_B>\n"},
110         {"osts", lfs_osts, 0, "osts"},
111         {"df", lfs_df, 0,
112          "report filesystem disk space usage or inodes usage"
113          "of each MDS/OSD.\n"
114          "Usage: df [-i] [-h] [path]"},
115 #ifdef HAVE_QUOTA_SUPPORT
116         {"quotachown",lfs_quotachown, 0,
117          "Change files' owner or group on the specified filesystem.\n"
118          "usage: quotachown [-i] <filesystem>\n"
119          "\t-i: ignore error if file is not exist\n"},
120         {"quotacheck", lfs_quotacheck, 0,
121          "Scan the specified filesystem for disk usage, and create,\n"
122          "or update quota files.\n"
123          "usage: quotacheck [ -ug ] <filesystem>"},
124         {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
125          "usage: quotaon [ -ugf ] <filesystem>"},
126         {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
127          "usage: quotaoff [ -ug ] <filesystem>"},
128         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
129          "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
130          "       setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
131         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
132          "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
133 #endif
134         {"help", Parser_help, 0, "help"},
135         {"exit", Parser_quit, 0, "quit"},
136         {"quit", Parser_quit, 0, "quit"},
137         { 0, 0, 0, NULL }
138 };
139
140 /* functions */
141 static int lfs_setstripe(int argc, char **argv)
142 {
143         char *fname;
144         int result;
145         long st_size;
146         int  st_offset, st_count;
147         char *end;
148
149         if (argc != 5 && argc != 3)
150                 return CMD_HELP;
151
152
153         if (argc == 3) {
154                 if (strcmp(argv[1], "-d") != 0)
155                         return CMD_HELP;
156
157                 fname = argv[2];
158                 st_size = 0;
159                 st_offset = -1;
160                 st_count = 0;
161         } else {
162                 fname = argv[1];
163
164                 /* get the stripe size */
165                 st_size = strtoul(argv[2], &end, 0);
166                 if (*end != '\0') {
167                         fprintf(stderr, "error: %s: bad stripe size '%s'\n",
168                                 argv[0], argv[2]);
169                         return CMD_HELP;
170                 }
171
172                 /* get the stripe offset */
173                 st_offset = strtoul(argv[3], &end, 0);
174                 if (*end != '\0') {
175                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
176                                 argv[0], argv[3]);
177                         return CMD_HELP;
178                 }
179                 /* get the stripe count */
180                 st_count = strtoul(argv[4], &end, 0);
181                 if (*end != '\0') {
182                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
183                                 argv[0], argv[4]);
184                         return CMD_HELP;
185                 }
186         }
187
188         result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
189         if (result)
190                 fprintf(stderr, "error: %s: create stripe file failed\n",
191                                 argv[0]);
192
193         return result;
194 }
195
196 static int set_time(time_t *time, time_t *set, char *str)
197 {
198         time_t t;
199         int res = 0;
200         
201         if (str[0] == '+')
202                 res = 1;
203         else if (str[0] == '-')
204                 res = -1;
205
206         if (res)
207                 str++;
208
209         t = strtol(str, NULL, 0);
210         if (*time < t * 24 * 60 * 60) {
211                 if (res)
212                         str--;
213                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
214                 return INT_MAX;
215         }
216
217         *set = *time - t * 24 * 60 * 60;
218         return res;
219 }
220
221 static int lfs_find(int argc, char **argv)
222 {
223         int new_fashion = 1;
224         int c, ret;
225         int zeroend;
226         time_t t;
227         unsigned int depth;
228         time_t atime, ctime, mtime;
229         int asign, msign, csign;
230         int quiet, verbose, recursive;
231         struct find_param param;
232         struct obd_uuid *obduuid = NULL;
233         struct option long_opts[] = {
234                 /* New find options. */
235                 {"atime",     required_argument, 0, 'A'},
236                 {"ctime",     required_argument, 0, 'C'},
237                 {"mtime",     required_argument, 0, 'M'},
238                 {"maxdepth",  required_argument, 0, 'D'},
239                 /* --obd is considered as a new option. */
240                 {"obd",       required_argument, 0, 'O'},
241                 {"print",     no_argument,       0, 'P'},
242                 {"print0",    no_argument,       0, 'p'},
243                 /* Old find options. */
244                 {"quiet",     no_argument,       0, 'q'},
245                 {"recursive", no_argument,       0, 'r'},
246                 {"verbose",   no_argument,       0, 'v'},
247                 {0, 0, 0, 0}
248         };
249
250         time(&t);
251         zeroend = 0;
252         depth = -1;
253         atime = ctime = mtime = 0;
254         asign = csign = msign = 0;
255         quiet = verbose = recursive = 0;
256         
257         while ((c = getopt_long_only(argc, argv, "A:C:D:M:PpOqrv", 
258                                      long_opts, NULL)) >= 0)
259         {
260                 switch (c) {
261                 case 0:
262                         /* Long options. */
263                         break;
264                 case 'A':
265                         ret = set_time(&t, &atime, optarg);
266                         if (ret == INT_MAX)
267                                 return -1;
268                         if (ret)
269                                 asign = ret;
270                         break;
271                 case 'C':
272                         ret = set_time(&t, &ctime, optarg);
273                         if (ret == INT_MAX)
274                                 return -1;
275                         if (ret)
276                                 csign = ret;
277                         break;
278                 case 'D':
279                         depth = strtol(optarg, 0, 0);
280                         break;
281                 case 'M':
282                         ret = set_time(&t, &mtime, optarg);
283                         if (ret == INT_MAX)
284                                 return -1;
285                         if (ret)
286                                 msign = ret;
287                         break;
288                 case 'O':
289                         if (obduuid) {
290                                 fprintf(stderr,
291                                         "error: %s: only one obduuid allowed",
292                                         argv[0]);
293                                 return CMD_HELP;
294                         }
295                         obduuid = (struct obd_uuid *)optarg;
296                         break;
297                 case 'p':
298                         zeroend = 1;
299                         break;
300                 case 'P':
301                         break;
302                 case 'q':
303                         new_fashion = 0;
304                         quiet++;
305                         verbose = 0;
306                         break;
307                 case 'r':
308                         new_fashion = 0;
309                         recursive = 1;
310                         break;
311                 case 'v':
312                         new_fashion = 0;
313                         verbose++;
314                         quiet = 0;
315                         break;
316                 case '?':
317                         return CMD_HELP;
318                 default:
319                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
320                                 argv[0], argv[optind - 1]);
321                         return CMD_HELP;
322                 };
323         }
324         
325         if (optind >= argc)
326                 return CMD_HELP;
327
328         memset(&param, 0, sizeof(param));
329         param.obduuid = obduuid;
330         if (new_fashion) {
331                 param.maxdepth = depth;
332                 param.atime = atime;
333                 param.ctime = ctime;
334                 param.mtime = mtime;
335                 param.asign = asign;
336                 param.csign = csign;
337                 param.msign = msign;
338                 param.zeroend = zeroend;
339                 param.quiet = 1;
340         } else {
341                 param.recursive = recursive;
342                 param.verbose = verbose;
343                 param.quiet = quiet;
344                 param.maxdepth = recursive ? -1 : 1;
345         }
346         
347         do {
348                 if (new_fashion)
349                         ret = llapi_find(argv[optind], &param);
350                 else
351                         ret = llapi_getstripe(argv[optind], &param);
352         } while (++optind < argc && !ret);
353
354         if (ret)
355                 fprintf(stderr, "error: %s failed for %s.\n", 
356                         argv[0], argv[optind - 1]);
357         return ret;
358 }
359
360 static int lfs_getstripe(int argc, char **argv)
361 {
362         struct option long_opts[] = {
363                 {"obd", 1, 0, 'O'},
364                 {"quiet", 0, 0, 'q'},
365                 {"recursive", 0, 0, 'r'},
366                 {"verbose", 0, 0, 'v'},
367                 {0, 0, 0, 0}
368         };
369         char short_opts[] = "hO:qrv";
370         int quiet, verbose, recursive, c, rc;
371         struct obd_uuid *obduuid = NULL;
372         struct find_param param;
373
374         optind = 0;
375         quiet = verbose = recursive = 0;
376         while ((c = getopt_long(argc, argv, short_opts,
377                                 long_opts, NULL)) != -1) {
378                 switch (c) {
379                 case 'O':
380                         if (obduuid) {
381                                 fprintf(stderr,
382                                         "error: %s: only one obduuid allowed",
383                                         argv[0]);
384                                 return CMD_HELP;
385                         }
386                         obduuid = (struct obd_uuid *)optarg;
387                         break;
388                 case 'q':
389                         quiet++;
390                         verbose = 0;
391                         break;
392                 case 'r':
393                         recursive = 1;
394                         break;
395                 case 'v':
396                         verbose++;
397                         quiet = 0;
398                         break;
399                 case '?':
400                         return CMD_HELP;
401                 default:
402                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
403                                 argv[0], argv[optind - 1]);
404                         return CMD_HELP;
405                 }
406         }
407
408         if (optind >= argc)
409                 return CMD_HELP;
410
411         memset(&param, 0, sizeof(param));
412         param.recursive = recursive;
413         param.verbose = verbose;
414         param.quiet = quiet;
415         param.obduuid = obduuid;
416         param.maxdepth = recursive ? -1 : 1;
417
418         do {
419                 rc = llapi_getstripe(argv[optind], &param);
420         } while (++optind < argc && !rc);
421
422         if (rc)
423                 fprintf(stderr, "error: %s failed for %s.\n", 
424                         argv[0], argv[optind - 1]);
425         return rc;
426 }
427
428 static int lfs_osts(int argc, char **argv)
429 {
430         FILE *fp;
431         struct mntent *mnt = NULL;
432         struct obd_uuid *obduuid = NULL;
433         struct find_param param;
434         int rc=0;
435
436         if (argc != 1)
437                 return CMD_HELP;
438
439         fp = setmntent(MOUNTED, "r");
440
441         if (fp == NULL) {
442                  fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
443                         strerror (errno));
444         } else {
445                 mnt = getmntent(fp);
446                 memset(&param, 0, sizeof(param));
447                 param.obduuid = obduuid;
448                 while (feof(fp) == 0 && ferror(fp) ==0) {
449                         if (llapi_is_lustre_mnttype(mnt)) {
450                                 rc = llapi_getstripe(mnt->mnt_dir, &param);
451                                 if (rc)
452                                         fprintf(stderr,
453                                                "error: %s: failed on %s\n",
454                                                argv[0], mnt->mnt_dir);
455                         }
456                         mnt = getmntent(fp);
457                 }
458                 endmntent(fp);
459         }
460
461         return rc;
462 }
463
464 #define COOK(value)                                                     \
465 ({                                                                      \
466         int radix = 0;                                                  \
467         while (value > 1024) {                                          \
468                 value /= 1024;                                          \
469                 radix++;                                                \
470         }                                                               \
471         radix;                                                          \
472 })
473 #define UUF     "%-20s"
474 #define CSF     "%9s"
475 #define CDF     "%9llu"
476 #define HSF     "%8s"
477 #define HDF     "%6.1f"
478 #define RSF     "%5s"
479 #define RDF     "%5d"
480
481 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
482 {
483         char rpath[PATH_MAX] = {'\0'};
484         struct mntent *mnt;
485         int rc, len, out_len = 0;
486
487         if (!realpath(path, rpath)) {
488                 rc = -errno;
489                 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
490                         path, strerror(-rc));
491                 return rc;
492         }
493
494         len = 0;
495         mnt = getmntent(fp);
496         while (feof(fp) == 0 && ferror(fp) == 0) {
497                 if (llapi_is_lustre_mnttype(mnt)) {
498                         len = strlen(mnt->mnt_dir);
499                         if (len > out_len &&
500                             !strncmp(rpath, mnt->mnt_dir, len)) {
501                                 out_len = len;
502                                 memset(mntdir, 0, dir_len);
503                                 strncpy(mntdir, mnt->mnt_dir, dir_len);
504                         }
505                 }
506                 mnt = getmntent(fp);
507         }
508
509         if (out_len > 0)
510                 return 0;
511
512         fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
513         return -EINVAL;
514 }
515
516 static int showdf(char *mntdir, struct obd_statfs *stat,
517                   char *uuid, int ishow, int cooked,
518                   char *type, int index, int rc)
519 {
520         long long avail, used, total;
521         double ratio = 0;
522         char *suffix = "KMGTPEZY";
523         char tbuf[10], ubuf[10], abuf[10], rbuf[10];
524
525         if (!uuid || !stat)
526                 return -EINVAL;
527
528         switch (rc) {
529         case 0:
530                 if (ishow) {
531                         avail = stat->os_ffree;
532                         used = stat->os_files - stat->os_ffree;
533                         total = stat->os_files;
534                 } else {
535                         int shift = cooked ? 0 : 10;
536
537                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
538                         used = stat->os_blocks - stat->os_bavail;
539                         used = (used * stat->os_bsize) >> shift;
540                         total = (stat->os_blocks * stat->os_bsize) >> shift;
541                 }
542
543                 if (total > 0)
544                         ratio = (double)used / (double)total;
545
546                 if (cooked) {
547                         int i;
548                         double cook_val;
549
550                         cook_val = (double)total;
551                         i = COOK(cook_val);
552                         if (i > 0)
553                                 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
554                         else
555                                 sprintf(tbuf, CDF, total);
556
557                         cook_val = (double)used;
558                         i = COOK(cook_val);
559                         if (i > 0)
560                                 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
561                         else
562                                 sprintf(ubuf, CDF, used);
563
564                         cook_val = (double)avail;
565                         i = COOK(cook_val);
566                         if (i > 0)
567                                 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
568                         else
569                                 sprintf(abuf, CDF, avail);
570                 } else {
571                         sprintf(tbuf, CDF, total);
572                         sprintf(ubuf, CDF, used);
573                         sprintf(abuf, CDF, avail);
574                 }
575
576                 sprintf(rbuf, RDF, (int)(ratio * 100));
577                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
578                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
579                 if (type)
580                         printf("[%s:%d]\n", type, index);
581                 else
582                         printf("\n");
583
584                 break;
585         case -ENODATA:
586                 printf(UUF": inactive device\n", uuid);
587                 break;
588         default:
589                 printf(UUF": %s\n", uuid, strerror(-rc));
590                 break;
591         }
592
593         return 0;
594 }
595
596 static int mntdf(char *mntdir, int ishow, int cooked)
597 {
598         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
599         struct obd_uuid uuid_buf;
600         __u32 index;
601         int rc;
602
603         if (ishow)
604                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
605                        "UUID", "Inodes", "IUsed", "IFree",
606                        "IUse%", "Mounted on");
607         else
608                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
609                        "UUID", cooked ? "bytes" : "1K-blocks",
610                        "Used", "Available", "Use%", "Mounted on");
611
612         for (index = 0; ; index++) {
613                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
614                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
615                 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
616                                       &stat_buf, &uuid_buf);
617                 if (rc == -ENODEV)
618                         break;
619
620                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
621                     rc == -ENODATA || rc == 0) {
622                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
623                                "MDT", index, rc);
624                 } else {
625                         fprintf(stderr,
626                                 "error: llapi_obd_statfs(%s): %s (%d)\n",
627                                 uuid_buf.uuid, strerror(-rc), rc);
628                         return rc;
629                 }
630                 if (rc == 0) {
631                         sum.os_ffree += stat_buf.os_ffree;
632                         sum.os_files += stat_buf.os_files;
633                 }
634         }
635
636         for (index = 0; ; index++) {
637                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
638                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
639                 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
640                                       &stat_buf, &uuid_buf);
641                 if (rc == -ENODEV)
642                         break;
643
644                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
645                     rc == -ENODATA || rc == 0) {
646                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
647                                "OST", index, rc);
648                 } else {
649                         fprintf(stderr,
650                                 "error: llapi_obd_statfs failed: %s (%d)\n",
651                                 strerror(-rc), rc);
652                         return rc;
653                 }
654                 if (rc == 0) {
655                         sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
656                         sum.os_bfree  += stat_buf.os_bfree * stat_buf.os_bsize;
657                         sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
658                 }
659         }
660
661         printf("\n");
662         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
663
664         return 0;
665 }
666
667 static int lfs_df(int argc, char **argv)
668 {
669         FILE *fp;
670         char *path = NULL;
671         struct mntent *mnt = NULL;
672         char mntdir[PATH_MAX] = {'\0'};
673         int ishow = 0, cooked = 0;
674         int c, rc = 0;
675
676         optind = 0;
677         while ((c = getopt(argc, argv, "ih")) != -1) {
678                 switch (c) {
679                 case 'i':
680                         ishow = 1;
681                         break;
682                 case 'h':
683                         cooked = 1;
684                         break;
685                 default:
686                         return CMD_HELP;
687                 }
688         }
689         if (optind < argc )
690                 path = argv[optind];
691
692         fp = setmntent(MOUNTED, "r");
693         if (fp == NULL) {
694                 rc = -errno;
695                 fprintf(stderr, "error: %s: open %s failed( %s )\n",
696                         argv[0], MOUNTED, strerror(errno));
697                 return rc;
698         }
699         if (path) {
700                 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
701                 if (rc) {
702                         endmntent(fp);
703                         return rc;
704                 }
705
706                 rc = mntdf(mntdir, ishow, cooked);
707                 printf("\n");
708                 endmntent(fp);
709         } else {
710                 mnt = getmntent(fp);
711                 while (feof(fp) == 0 && ferror(fp) == 0) {
712                         if (llapi_is_lustre_mnttype(mnt)) {
713                                 rc = mntdf(mnt->mnt_dir, ishow, cooked);
714                                 if (rc)
715                                         break;
716                                 printf("\n");
717                         }
718                         mnt = getmntent(fp);
719                 }
720                 endmntent(fp);
721         }
722
723         return rc;
724 }
725
726 static int lfs_check(int argc, char **argv)
727 {
728         int rc;
729         FILE *fp;
730         struct mntent *mnt = NULL;
731         int num_types = 1;
732         char *obd_types[2];
733         char obd_type1[4];
734         char obd_type2[4];
735
736         if (argc != 2)
737                 return CMD_HELP;
738
739         obd_types[0] = obd_type1;
740         obd_types[1] = obd_type2;
741
742         if (strcmp(argv[1], "osts") == 0) {
743                 strcpy(obd_types[0], "osc");
744         } else if (strcmp(argv[1], "mds") == 0) {
745                 strcpy(obd_types[0], "mdc");
746         } else if (strcmp(argv[1], "servers") == 0) {
747                 num_types = 2;
748                 strcpy(obd_types[0], "osc");
749                 strcpy(obd_types[1], "mdc");
750         } else {
751                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
752                                 argv[0], argv[1]);
753                         return CMD_HELP;
754         }
755
756         fp = setmntent(MOUNTED, "r");
757         if (fp == NULL) {
758                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
759                         strerror (errno));
760         } else {
761                 mnt = getmntent(fp);
762                 while (feof(fp) == 0 && ferror(fp) ==0) {
763                         if (llapi_is_lustre_mnttype(mnt))
764                                 break;
765                         mnt = getmntent(fp);
766                 }
767                 endmntent(fp);
768         }
769
770         if (!mnt) {
771                 fprintf(stderr, "No suitable Lustre mount found\n");
772                 return -1;
773         }
774
775         rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
776
777         if (rc)
778                 fprintf(stderr, "error: %s: %s status failed\n",
779                                 argv[0],argv[1]);
780
781         return rc;
782
783 }
784
785 static int lfs_catinfo(int argc, char **argv)
786 {
787         FILE *fp;
788         struct mntent *mnt = NULL;
789         int rc;
790
791         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
792                 return CMD_HELP;
793
794         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
795                 return CMD_HELP;
796
797         fp = setmntent(MOUNTED, "r");
798         if (fp == NULL) {
799                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
800                          strerror(errno));
801         } else {
802                 mnt = getmntent(fp);
803                 while (feof(fp) == 0 && ferror(fp) == 0) {
804                         if (llapi_is_lustre_mnttype(mnt))
805                                 break;
806                         mnt = getmntent(fp);
807                 }
808                 endmntent(fp);
809         }
810
811         if (mnt) {
812                 if (argc == 3)
813                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
814                 else
815                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
816         } else {
817                 fprintf(stderr, "no lustre_lite mounted.\n");
818                 rc = -1;
819         }
820
821         return rc;
822 }
823
824 int lfs_join(int argc, char **argv)
825 {
826         char *name_head, *name_tail;
827         int fd, rc;
828         loff_t size;
829
830         if (argc != 3)
831                 return CMD_HELP;
832         name_head = argv[1];
833         fd = open(name_head, O_WRONLY);
834         if (fd < 0) {
835                 fprintf(stderr, "Can not open name_head %s rc=%d\n",
836                         name_head, fd);
837                 return fd;
838         }
839         size = lseek(fd, 0, SEEK_END);
840         if (size % JOIN_FILE_ALIGN) {
841                 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
842                         name_head, (long long)size, JOIN_FILE_ALIGN);
843                 rc = -EINVAL;
844                 goto out;
845         }
846         name_tail = argv[2];
847         rc = ioctl(fd, LL_IOC_JOIN, name_tail);
848 out:
849         close(fd);
850         if (rc) {
851                 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
852                         argv[1], argv[2]);
853         }
854         return rc;
855 }
856
857 #ifdef HAVE_QUOTA_SUPPORT
858 static int lfs_quotachown(int argc, char **argv)
859 {
860
861         int c,rc;
862         int flag = 0;
863
864         while ((c = getopt(argc, argv, "i")) != -1) {
865                 switch (c) {
866                 case 'i':
867                         flag++;
868                         break;
869                 default:
870                         fprintf(stderr, "error: %s: option '-%c' "
871                                         "unrecognized\n", argv[0], c);
872                         return CMD_HELP;
873                 }
874         }
875         if (optind == argc)
876                 return CMD_HELP;
877         rc = llapi_quotachown(argv[optind], flag);
878         if(rc)
879                 fprintf(stderr,"error: change file owner/group failed.\n");
880         return rc;
881 }
882
883
884 static int lfs_quotacheck(int argc, char **argv)
885 {
886         int c, check_type = 0;
887         char *mnt;
888         struct if_quotacheck qchk;
889         struct if_quotactl qctl;
890         char *obd_type = qchk.obd_type;
891         char *obd_uuid = qchk.obd_uuid.uuid;
892         int rc;
893
894         memset(&qchk, 0, sizeof(qchk));
895
896         optind = 0;
897         while ((c = getopt(argc, argv, "ug")) != -1) {
898                 switch (c) {
899                 case 'u':
900                         check_type |= 0x01;
901                         break;
902                 case 'g':
903                         check_type |= 0x02;
904                         break;
905                 default:
906                         fprintf(stderr, "error: %s: option '-%c' "
907                                         "unrecognized\n", argv[0], c);
908                         return CMD_HELP;
909                 }
910         }
911
912         if (check_type)
913                 check_type--;
914         else    /* do quotacheck for both user & group quota by default */
915                 check_type = 0x02;
916
917         if (argc == optind)
918                 return CMD_HELP;
919
920         mnt = argv[optind];
921
922         memset(&qctl, 0, sizeof(qctl));
923         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
924         qctl.qc_id = QFMT_LDISKFS;
925         qctl.qc_type = check_type;
926         rc = llapi_quotactl(mnt, &qctl);
927         if (rc) {
928                 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
929                 return rc;
930         }
931
932         rc = llapi_quotacheck(mnt, check_type);
933         if (rc) {
934                 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
935                 return rc;
936         }
937
938         rc = llapi_poll_quotacheck(mnt, &qchk);
939         if (rc) {
940                 if (*obd_type)
941                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
942                 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
943                 return rc;
944         }
945
946         memset(&qctl, 0, sizeof(qctl));
947         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
948         qctl.qc_id = QFMT_LDISKFS;
949         qctl.qc_type = check_type;
950         rc = llapi_quotactl(mnt, &qctl);
951         if (rc) {
952                 if (*obd_type)
953                         fprintf(stderr, "%s %s ",
954                                 qctl.obd_type, qctl.obd_uuid.uuid);
955                 fprintf(stderr, "%s turn on quota failed: %s\n",
956                         argv[0], strerror(errno));
957                 return rc;
958         }
959
960         return 0;
961 }
962
963 static int lfs_quotaon(int argc, char **argv)
964 {
965         int c;
966         char *mnt;
967         struct if_quotactl qctl;
968         char *obd_type = qctl.obd_type;
969         char *obd_uuid = qctl.obd_uuid.uuid;
970         int rc;
971
972         memset(&qctl, 0, sizeof(qctl));
973         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
974         qctl.qc_id = QFMT_LDISKFS;
975
976         optind = 0;
977         while ((c = getopt(argc, argv, "ugf")) != -1) {
978                 switch (c) {
979                 case 'u':
980                         qctl.qc_type |= 0x01;
981                         break;
982                 case 'g':
983                         qctl.qc_type |= 0x02;
984                         break;
985                 case 'f':
986                         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
987                         break;
988                 default:
989                         fprintf(stderr, "error: %s: option '-%c' "
990                                         "unrecognized\n", argv[0], c);
991                         return CMD_HELP;
992                 }
993         }
994
995         if (qctl.qc_type)
996                 qctl.qc_type--;
997
998         if (argc == optind)
999                 return CMD_HELP;
1000
1001         mnt = argv[optind];
1002
1003         rc = llapi_quotactl(mnt, &qctl);
1004         if (rc) {
1005                 if (*obd_type)
1006                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1007                 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1008                 return rc;
1009         }
1010
1011         return 0;
1012 }
1013
1014 static int lfs_quotaoff(int argc, char **argv)
1015 {
1016         int c;
1017         char *mnt;
1018         struct if_quotactl qctl;
1019         char *obd_type = qctl.obd_type;
1020         char *obd_uuid = qctl.obd_uuid.uuid;
1021         int rc;
1022
1023         memset(&qctl, 0, sizeof(qctl));
1024         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1025
1026         optind = 0;
1027         while ((c = getopt(argc, argv, "ug")) != -1) {
1028                 switch (c) {
1029                 case 'u':
1030                         qctl.qc_type |= 0x01;
1031                         break;
1032                 case 'g':
1033                         qctl.qc_type |= 0x02;
1034                         break;
1035                 default:
1036                         fprintf(stderr, "error: %s: option '-%c' "
1037                                         "unrecognized\n", argv[0], c);
1038                         return CMD_HELP;
1039                 }
1040         }
1041
1042         if (qctl.qc_type)
1043                 qctl.qc_type--;
1044
1045         if (argc == optind)
1046                 return CMD_HELP;
1047
1048         mnt = argv[optind];
1049
1050         rc = llapi_quotactl(mnt, &qctl);
1051         if (rc) {
1052                 if (*obd_type)
1053                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1054                 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1055                 return rc;
1056         }
1057
1058         return 0;
1059 }
1060
1061 static int name2id(unsigned int *id, char *name, int type)
1062 {
1063         if (type == USRQUOTA) {
1064                 struct passwd *entry;
1065
1066                 if (!(entry = getpwnam(name))) {
1067                         if (!errno)
1068                                 errno = ENOENT;
1069                         return -1;
1070                 }
1071
1072                 *id = entry->pw_uid;
1073         } else {
1074                 struct group *entry;
1075
1076                 if (!(entry = getgrnam(name))) {
1077                         if (!errno)
1078                                 errno = ENOENT;
1079                         return -1;
1080                 }
1081
1082                 *id = entry->gr_gid;
1083         }
1084
1085         return 0;
1086 }
1087
1088 static int id2name(char **name, unsigned int id, int type)
1089 {
1090         if (type == USRQUOTA) {
1091                 struct passwd *entry;
1092
1093                 if (!(entry = getpwuid(id))) {
1094                         if (!errno)
1095                                 errno = ENOENT;
1096                         return -1;
1097                 }
1098
1099                 *name = entry->pw_name;
1100         } else {
1101                 struct group *entry;
1102
1103                 if (!(entry = getgrgid(id))) {
1104                         if (!errno)
1105                                 errno = ENOENT;
1106                         return -1;
1107                 }
1108
1109                 *name = entry->gr_name;
1110         }
1111
1112         return 0;
1113 }
1114
1115 #define ARG2INT(nr, str, msg)                                           \
1116 do {                                                                    \
1117         char *endp;                                                     \
1118         nr = strtol(str, &endp, 0);                                     \
1119         if (*endp) {                                                    \
1120                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
1121                 return CMD_HELP;                                        \
1122         }                                                               \
1123 } while (0)
1124
1125 int lfs_setquota(int argc, char **argv)
1126 {
1127         int c;
1128         char *mnt;
1129         struct if_quotactl qctl;
1130         char *obd_type = qctl.obd_type;
1131         char *obd_uuid = qctl.obd_uuid.uuid;
1132         int rc;
1133
1134         memset(&qctl, 0, sizeof(qctl));
1135         qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1136
1137         optind = 0;
1138         while ((c = getopt(argc, argv, "ugt")) != -1) {
1139                 switch (c) {
1140                 case 'u':
1141                         qctl.qc_type |= 0x01;
1142                         break;
1143                 case 'g':
1144                         qctl.qc_type |= 0x02;
1145                         break;
1146                 case 't':
1147                         qctl.qc_cmd = LUSTRE_Q_SETINFO;
1148                         break;
1149                 default:
1150                         fprintf(stderr, "error: %s: option '-%c' "
1151                                         "unrecognized\n", argv[0], c);
1152                         return CMD_HELP;
1153                 }
1154         }
1155
1156         if (qctl.qc_type)
1157                 qctl.qc_type--;
1158
1159         if (qctl.qc_type == UGQUOTA) {
1160                 fprintf(stderr, "error: user and group quotas can't be set "
1161                                 "both\n");
1162                 return CMD_HELP;
1163         }
1164
1165         if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1166                 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1167
1168                 if (optind + 6 != argc)
1169                         return CMD_HELP;
1170
1171                 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1172                 if (rc) {
1173                         fprintf(stderr, "error: find id for name %s failed: %s\n",
1174                                 argv[optind - 1], strerror(errno));
1175                         return CMD_HELP;
1176                 }
1177
1178                 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1179                 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1180                 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1181                 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1182
1183                 dqb->dqb_valid = QIF_LIMITS;
1184         } else {
1185                 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1186
1187                 if (optind + 3 != argc)
1188                         return CMD_HELP;
1189
1190                 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1191                 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1192         }
1193
1194         mnt = argv[optind];
1195
1196         rc = llapi_quotactl(mnt, &qctl);
1197         if (rc) {
1198                 if (*obd_type)
1199                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1200                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1201                 return rc;
1202         }
1203
1204         return 0;
1205 }
1206
1207 static inline char *type2name(int check_type)
1208 {
1209         if (check_type == USRQUOTA)
1210                 return "user";
1211         else if (check_type == GRPQUOTA)
1212                 return "group";
1213         else
1214                 return "unknown";
1215 }
1216
1217
1218 static void grace2str(time_t seconds,char *buf)
1219 {
1220         uint minutes, hours, days;
1221
1222         minutes = (seconds + 30) / 60;
1223         hours = minutes / 60;
1224         minutes %= 60;
1225         days = hours / 24;
1226         hours %= 24;
1227         if (days >= 2)
1228                 snprintf(buf, 40, "%ddays", days);
1229         else
1230                 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1231 }
1232
1233
1234 static void diff2str(time_t seconds, char *buf, time_t now)
1235 {
1236
1237         buf[0] = 0;
1238         if (!seconds)
1239                 return;
1240         if (seconds <= now) {
1241                 strcpy(buf, "none");
1242                 return;
1243         }
1244         grace2str(seconds - now, buf);
1245 }
1246
1247 static void print_quota_title(char *name, struct if_quotactl *qctl)
1248 {
1249         printf("Disk quotas for %s %s (%cid %u):\n",
1250                type2name(qctl->qc_type), name,
1251                *type2name(qctl->qc_type), qctl->qc_id);
1252         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1253                "Filesystem",
1254                "blocks", "quota", "limit", "grace",
1255                "files", "quota", "limit", "grace");
1256 }
1257
1258 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1259 {
1260         time_t now;
1261
1262         time(&now);
1263
1264         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1265                 int bover = 0, iover = 0;
1266                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1267
1268                 if (dqb->dqb_bhardlimit &&
1269                     toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1270                         bover = 1;
1271                 } else if (dqb->dqb_bsoftlimit &&
1272                            toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1273                         if (dqb->dqb_btime > now) {
1274                                 bover = 2;
1275                         } else {
1276                                 bover = 3;
1277                         }
1278                 }
1279
1280                 if (dqb->dqb_ihardlimit &&
1281                     dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1282                         iover = 1;
1283                 } else if (dqb->dqb_isoftlimit &&
1284                            dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1285                         if (dqb->dqb_btime > now) {
1286                                 iover = 2;
1287                         } else {
1288                                 iover = 3;
1289                         }
1290                 }
1291
1292 #if 0           /* XXX: always print quotas even when no usages */
1293                 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1294 #endif
1295                 {
1296                         char numbuf[3][32];
1297                         char timebuf[40];
1298
1299                         if (strlen(mnt) > 15)
1300                                 printf("%s\n%15s", mnt, "");
1301                         else
1302                                 printf("%15s", mnt);
1303
1304                         if (bover)
1305                                 diff2str(dqb->dqb_btime, timebuf, now);
1306
1307                         sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1308                         sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1309                         sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1310                         printf(" %7s%c %6s %7s %7s",
1311                                numbuf[0], bover ? '*' : ' ', numbuf[1],
1312                                numbuf[2], bover > 1 ? timebuf : "");
1313
1314                         if (iover)
1315                                 diff2str(dqb->dqb_itime, timebuf, now);
1316
1317                         sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1318                         sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1319                         sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1320                         if (!ost_only)
1321                                 printf(" %7s%c %6s %7s %7s",
1322                                        numbuf[0], iover ? '*' : ' ', numbuf[1],
1323                                        numbuf[2], iover > 1 ? timebuf : "");
1324                         printf("\n");
1325                 }
1326         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1327                    qctl->qc_cmd == Q_GETOINFO) {
1328                 char bgtimebuf[40];
1329                 char igtimebuf[40];
1330
1331                 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1332                 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1333                 printf("Block grace time: %s; Inode grace time: %s\n",
1334                        bgtimebuf, igtimebuf);
1335         }
1336 }
1337
1338 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1339 {
1340         int rc;
1341
1342         /* XXX: this is a flag to mark that only mds quota is wanted */
1343         qctl->qc_dqblk.dqb_valid = 1;
1344         rc = llapi_quotactl(mnt, qctl);
1345         if (rc) {
1346                 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1347                 return;
1348         }
1349         qctl->qc_dqblk.dqb_valid = 0;
1350
1351         print_quota(qctl->obd_uuid.uuid, qctl, 0);
1352 }
1353
1354 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1355 {
1356         DIR *dir;
1357         struct obd_uuid uuids[1024], *uuidp;
1358         int obdcount = 1024;
1359         int i, rc;
1360
1361         dir = opendir(mnt);
1362         if (!dir) {
1363                 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1364                 return;
1365         }
1366
1367         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1368         if (rc != 0) {
1369                 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1370                 goto out;
1371         }
1372
1373         for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1374                 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1375
1376                 /* XXX clear this flag to get quota from osts */
1377                 qctl->qc_dqblk.dqb_valid = 0;
1378                 rc = llapi_quotactl(mnt, qctl);
1379                 if (rc) {
1380                         fprintf(stderr, "%s quotactl failed: %s\n",
1381                                 uuidp->uuid, strerror(errno));
1382                         continue;
1383                 }
1384
1385                 print_quota(uuidp->uuid, qctl, 1);
1386         }
1387
1388 out:
1389         closedir(dir);
1390         return;
1391 }
1392
1393 static int lfs_quota(int argc, char **argv)
1394 {
1395         int c;
1396         char *name = NULL, *mnt;
1397         struct if_quotactl qctl;
1398         char *obd_type = qctl.obd_type;
1399         char *obd_uuid = qctl.obd_uuid.uuid;
1400         int rc;
1401
1402         memset(&qctl, 0, sizeof(qctl));
1403         qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1404
1405         optind = 0;
1406         while ((c = getopt(argc, argv, "ugto:")) != -1) {
1407                 switch (c) {
1408                 case 'u':
1409                         qctl.qc_type |= 0x01;
1410                         break;
1411                 case 'g':
1412                         qctl.qc_type |= 0x02;
1413                         break;
1414                 case 't':
1415                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
1416                         break;
1417                 case 'o':
1418                         strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
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
1430         if (qctl.qc_type == UGQUOTA) {
1431                 fprintf(stderr, "error: user or group can't be specified"
1432                                 "both\n");
1433                 return CMD_HELP;
1434         }
1435
1436         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1437                 if (optind + 2 != argc)
1438                         return CMD_HELP;
1439
1440                 name = argv[optind++];
1441                 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1442                 if (rc) {
1443                         fprintf(stderr, "error: find id for name %s failed: %s\n",
1444                                 name, strerror(errno));
1445                         return CMD_HELP;
1446                 }
1447                 print_quota_title(name, &qctl);
1448         } else if (optind + 1 != argc) {
1449                 return CMD_HELP;
1450         }
1451
1452         mnt = argv[optind];
1453
1454         rc = llapi_quotactl(mnt, &qctl);
1455         if (rc) {
1456                 if (*obd_type)
1457                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1458                 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1459                 return rc;
1460         }
1461
1462         if (!name)
1463                 rc = id2name(&name, getuid(), qctl.qc_type);
1464
1465         if (*obd_uuid) {
1466                 mnt = "";
1467                 name = obd_uuid;
1468         }
1469
1470         print_quota(mnt, &qctl, 0);
1471
1472         if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1473                 print_mds_quota(mnt, &qctl);
1474                 print_lov_quota(mnt, &qctl);
1475         }
1476
1477         return 0;
1478 }
1479 #endif /* HAVE_QUOTA_SUPPORT */
1480
1481 int main(int argc, char **argv)
1482 {
1483         int rc;
1484
1485         setlinebuf(stdout);
1486
1487         ptl_initialize(argc, argv);
1488         if (obd_initialize(argc, argv) < 0)
1489                 exit(2);
1490         if (dbg_initialize(argc, argv) < 0)
1491                 exit(3);
1492
1493         Parser_init("lfs > ", cmdlist);
1494
1495         if (argc > 1) {
1496                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1497         } else {
1498                 rc = Parser_commands();
1499         }
1500
1501         obd_finalize(argc, argv);
1502         return rc;
1503 }