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