Whamcloud - gitweb
Branch 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_mnt(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_mnt(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 == -EAGAIN)
675                         continue;
676
677                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
678                     rc == -ENODATA || rc == 0) {
679                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
680                                "MDT", index, rc);
681                 } else {
682                         fprintf(stderr,
683                                 "error: llapi_obd_statfs(%s): %s (%d)\n",
684                                 uuid_buf.uuid, strerror(-rc), rc);
685                         return rc;
686                 }
687                 if (rc == 0) {
688                         sum.os_ffree += stat_buf.os_ffree;
689                         sum.os_files += stat_buf.os_files;
690                 }
691         }
692
693         for (index = 0; ; index++) {
694                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
695                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
696                 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
697                                       &stat_buf, &uuid_buf);
698                 if (rc == -ENODEV)
699                         break;
700
701                 if (rc == -EAGAIN)
702                         continue;
703
704                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
705                     rc == -ENODATA || rc == 0) {
706                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
707                                "OST", index, rc);
708                 } else {
709                         fprintf(stderr,
710                                 "error: llapi_obd_statfs failed: %s (%d)\n",
711                                 strerror(-rc), rc);
712                         return rc;
713                 }
714                 if (rc == 0) {
715                         sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
716                         sum.os_bfree  += stat_buf.os_bfree * stat_buf.os_bsize;
717                         sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
718                 }
719         }
720
721         printf("\n");
722         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
723
724         return 0;
725 }
726
727 static int lfs_df(int argc, char **argv)
728 {
729         FILE *fp;
730         char *path = NULL;
731         struct mntent *mnt = NULL;
732         char mntdir[PATH_MAX] = {'\0'};
733         int ishow = 0, cooked = 0;
734         int c, rc = 0;
735
736         optind = 0;
737         while ((c = getopt(argc, argv, "ih")) != -1) {
738                 switch (c) {
739                 case 'i':
740                         ishow = 1;
741                         break;
742                 case 'h':
743                         cooked = 1;
744                         break;
745                 default:
746                         return CMD_HELP;
747                 }
748         }
749         if (optind < argc )
750                 path = argv[optind];
751
752         fp = setmntent(MOUNTED, "r");
753         if (fp == NULL) {
754                 rc = -errno;
755                 fprintf(stderr, "error: %s: open %s failed( %s )\n",
756                         argv[0], MOUNTED, strerror(errno));
757                 return rc;
758         }
759         if (path) {
760                 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
761                 if (rc) {
762                         endmntent(fp);
763                         return rc;
764                 }
765
766                 rc = mntdf(mntdir, ishow, cooked);
767                 printf("\n");
768                 endmntent(fp);
769         } else {
770                 mnt = getmntent(fp);
771                 while (feof(fp) == 0 && ferror(fp) == 0) {
772                         if (llapi_is_lustre_mnt(mnt)) {
773                                 rc = mntdf(mnt->mnt_dir, ishow, cooked);
774                                 if (rc)
775                                         break;
776                                 printf("\n");
777                         }
778                         mnt = getmntent(fp);
779                 }
780                 endmntent(fp);
781         }
782
783         return rc;
784 }
785
786 static int lfs_check(int argc, char **argv)
787 {
788         int rc;
789         FILE *fp;
790         struct mntent *mnt = NULL;
791         int num_types = 1;
792         char *obd_types[2];
793         char obd_type1[4];
794         char obd_type2[4];
795
796         if (argc != 2)
797                 return CMD_HELP;
798
799         obd_types[0] = obd_type1;
800         obd_types[1] = obd_type2;
801
802         if (strcmp(argv[1], "osts") == 0) {
803                 strcpy(obd_types[0], "osc");
804         } else if (strcmp(argv[1], "mds") == 0) {
805                 strcpy(obd_types[0], "mdc");
806         } else if (strcmp(argv[1], "servers") == 0) {
807                 num_types = 2;
808                 strcpy(obd_types[0], "osc");
809                 strcpy(obd_types[1], "mdc");
810         } else {
811                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
812                                 argv[0], argv[1]);
813                         return CMD_HELP;
814         }
815
816         fp = setmntent(MOUNTED, "r");
817         if (fp == NULL) {
818                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
819                         strerror (errno));
820         } else {
821                 mnt = getmntent(fp);
822                 while (feof(fp) == 0 && ferror(fp) ==0) {
823                         if (llapi_is_lustre_mnt(mnt))
824                                 break;
825                         mnt = getmntent(fp);
826                 }
827                 endmntent(fp);
828         }
829
830         if (!mnt) {
831                 fprintf(stderr, "No suitable Lustre mount found\n");
832                 return -1;
833         }
834
835         rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
836
837         if (rc)
838                 fprintf(stderr, "error: %s: %s status failed\n",
839                                 argv[0],argv[1]);
840
841         return rc;
842
843 }
844
845 static int lfs_catinfo(int argc, char **argv)
846 {
847         FILE *fp;
848         struct mntent *mnt = NULL;
849         int rc;
850
851         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
852                 return CMD_HELP;
853
854         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
855                 return CMD_HELP;
856
857         fp = setmntent(MOUNTED, "r");
858         if (fp == NULL) {
859                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
860                          strerror(errno));
861         } else {
862                 mnt = getmntent(fp);
863                 while (feof(fp) == 0 && ferror(fp) == 0) {
864                         if (llapi_is_lustre_mnt(mnt))
865                                 break;
866                         mnt = getmntent(fp);
867                 }
868                 endmntent(fp);
869         }
870
871         if (mnt) {
872                 if (argc == 3)
873                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
874                 else
875                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
876         } else {
877                 fprintf(stderr, "no lustre_lite mounted.\n");
878                 rc = -1;
879         }
880
881         return rc;
882 }
883
884 int lfs_join(int argc, char **argv)
885 {
886         char *name_head, *name_tail;
887         int fd, rc;
888         loff_t size;
889
890         if (argc != 3)
891                 return CMD_HELP;
892         name_head = argv[1];
893         fd = open(name_head, O_WRONLY);
894         if (fd < 0) {
895                 fprintf(stderr, "Can not open name_head %s rc=%d\n",
896                         name_head, fd);
897                 return fd;
898         }
899         size = lseek(fd, 0, SEEK_END);
900         if (size % JOIN_FILE_ALIGN) {
901                 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
902                         name_head, (long long)size, JOIN_FILE_ALIGN);
903                 rc = -EINVAL;
904                 goto out;
905         }
906         name_tail = argv[2];
907         rc = ioctl(fd, LL_IOC_JOIN, name_tail);
908 out:
909         close(fd);
910         if (rc) {
911                 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
912                         argv[1], argv[2]);
913         }
914         return rc;
915 }
916
917 #ifdef HAVE_QUOTA_SUPPORT
918 static int lfs_quotachown(int argc, char **argv)
919 {
920
921         int c,rc;
922         int flag = 0;
923
924         while ((c = getopt(argc, argv, "i")) != -1) {
925                 switch (c) {
926                 case 'i':
927                         flag++;
928                         break;
929                 default:
930                         fprintf(stderr, "error: %s: option '-%c' "
931                                         "unrecognized\n", argv[0], c);
932                         return CMD_HELP;
933                 }
934         }
935         if (optind == argc)
936                 return CMD_HELP;
937         rc = llapi_quotachown(argv[optind], flag);
938         if(rc)
939                 fprintf(stderr,"error: change file owner/group failed.\n");
940         return rc;
941 }
942
943
944 static int lfs_quotacheck(int argc, char **argv)
945 {
946         int c, check_type = 0;
947         char *mnt;
948         struct if_quotacheck qchk;
949         struct if_quotactl qctl;
950         char *obd_type = qchk.obd_type;
951         char *obd_uuid = qchk.obd_uuid.uuid;
952         int rc;
953
954         memset(&qchk, 0, sizeof(qchk));
955
956         optind = 0;
957         while ((c = getopt(argc, argv, "ug")) != -1) {
958                 switch (c) {
959                 case 'u':
960                         check_type |= 0x01;
961                         break;
962                 case 'g':
963                         check_type |= 0x02;
964                         break;
965                 default:
966                         fprintf(stderr, "error: %s: option '-%c' "
967                                         "unrecognized\n", argv[0], c);
968                         return CMD_HELP;
969                 }
970         }
971
972         if (check_type)
973                 check_type--;
974         else    /* do quotacheck for both user & group quota by default */
975                 check_type = 0x02;
976
977         if (argc == optind)
978                 return CMD_HELP;
979
980         mnt = argv[optind];
981
982         memset(&qctl, 0, sizeof(qctl));
983         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
984         qctl.qc_id = QFMT_LDISKFS;
985         qctl.qc_type = check_type;
986         rc = llapi_quotactl(mnt, &qctl);
987         if (rc) {
988                 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
989                 return rc;
990         }
991
992         rc = llapi_quotacheck(mnt, check_type);
993         if (rc) {
994                 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
995                 return rc;
996         }
997
998         rc = llapi_poll_quotacheck(mnt, &qchk);
999         if (rc) {
1000                 if (*obd_type)
1001                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1002                 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1003                 return rc;
1004         }
1005
1006         memset(&qctl, 0, sizeof(qctl));
1007         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1008         qctl.qc_id = QFMT_LDISKFS;
1009         qctl.qc_type = check_type;
1010         rc = llapi_quotactl(mnt, &qctl);
1011         if (rc) {
1012                 if (*obd_type)
1013                         fprintf(stderr, "%s %s ",
1014                                 qctl.obd_type, qctl.obd_uuid.uuid);
1015                 fprintf(stderr, "%s turn on quota failed: %s\n",
1016                         argv[0], strerror(errno));
1017                 return rc;
1018         }
1019
1020         return 0;
1021 }
1022
1023 static int lfs_quotaon(int argc, char **argv)
1024 {
1025         int c;
1026         char *mnt;
1027         struct if_quotactl qctl;
1028         char *obd_type = qctl.obd_type;
1029         char *obd_uuid = qctl.obd_uuid.uuid;
1030         int rc;
1031
1032         memset(&qctl, 0, sizeof(qctl));
1033         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1034         qctl.qc_id = QFMT_LDISKFS;
1035
1036         optind = 0;
1037         while ((c = getopt(argc, argv, "ugf")) != -1) {
1038                 switch (c) {
1039                 case 'u':
1040                         qctl.qc_type |= 0x01;
1041                         break;
1042                 case 'g':
1043                         qctl.qc_type |= 0x02;
1044                         break;
1045                 case 'f':
1046                         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1047                         break;
1048                 default:
1049                         fprintf(stderr, "error: %s: option '-%c' "
1050                                         "unrecognized\n", argv[0], c);
1051                         return CMD_HELP;
1052                 }
1053         }
1054
1055         if (qctl.qc_type)
1056                 qctl.qc_type--;
1057
1058         if (argc == optind)
1059                 return CMD_HELP;
1060
1061         mnt = argv[optind];
1062
1063         rc = llapi_quotactl(mnt, &qctl);
1064         if (rc) {
1065                 if (*obd_type)
1066                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1067                 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1068                 return rc;
1069         }
1070
1071         return 0;
1072 }
1073
1074 static int lfs_quotaoff(int argc, char **argv)
1075 {
1076         int c;
1077         char *mnt;
1078         struct if_quotactl qctl;
1079         char *obd_type = qctl.obd_type;
1080         char *obd_uuid = qctl.obd_uuid.uuid;
1081         int rc;
1082
1083         memset(&qctl, 0, sizeof(qctl));
1084         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1085
1086         optind = 0;
1087         while ((c = getopt(argc, argv, "ug")) != -1) {
1088                 switch (c) {
1089                 case 'u':
1090                         qctl.qc_type |= 0x01;
1091                         break;
1092                 case 'g':
1093                         qctl.qc_type |= 0x02;
1094                         break;
1095                 default:
1096                         fprintf(stderr, "error: %s: option '-%c' "
1097                                         "unrecognized\n", argv[0], c);
1098                         return CMD_HELP;
1099                 }
1100         }
1101
1102         if (qctl.qc_type)
1103                 qctl.qc_type--;
1104
1105         if (argc == optind)
1106                 return CMD_HELP;
1107
1108         mnt = argv[optind];
1109
1110         rc = llapi_quotactl(mnt, &qctl);
1111         if (rc) {
1112                 if (*obd_type)
1113                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1114                 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1115                 return rc;
1116         }
1117
1118         return 0;
1119 }
1120
1121 static int name2id(unsigned int *id, char *name, int type)
1122 {
1123         if (type == USRQUOTA) {
1124                 struct passwd *entry;
1125
1126                 if (!(entry = getpwnam(name))) {
1127                         if (!errno)
1128                                 errno = ENOENT;
1129                         return -1;
1130                 }
1131
1132                 *id = entry->pw_uid;
1133         } else {
1134                 struct group *entry;
1135
1136                 if (!(entry = getgrnam(name))) {
1137                         if (!errno)
1138                                 errno = ENOENT;
1139                         return -1;
1140                 }
1141
1142                 *id = entry->gr_gid;
1143         }
1144
1145         return 0;
1146 }
1147
1148 static int id2name(char **name, unsigned int id, int type)
1149 {
1150         if (type == USRQUOTA) {
1151                 struct passwd *entry;
1152
1153                 if (!(entry = getpwuid(id))) {
1154                         if (!errno)
1155                                 errno = ENOENT;
1156                         return -1;
1157                 }
1158
1159                 *name = entry->pw_name;
1160         } else {
1161                 struct group *entry;
1162
1163                 if (!(entry = getgrgid(id))) {
1164                         if (!errno)
1165                                 errno = ENOENT;
1166                         return -1;
1167                 }
1168
1169                 *name = entry->gr_name;
1170         }
1171
1172         return 0;
1173 }
1174
1175 #define ARG2INT(nr, str, msg)                                           \
1176 do {                                                                    \
1177         char *endp;                                                     \
1178         nr = strtol(str, &endp, 0);                                     \
1179         if (*endp) {                                                    \
1180                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
1181                 return CMD_HELP;                                        \
1182         }                                                               \
1183 } while (0)
1184
1185 int lfs_setquota(int argc, char **argv)
1186 {
1187         int c;
1188         char *mnt;
1189         struct if_quotactl qctl;
1190         char *obd_type = qctl.obd_type;
1191         char *obd_uuid = qctl.obd_uuid.uuid;
1192         int rc;
1193
1194         memset(&qctl, 0, sizeof(qctl));
1195         qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1196
1197         optind = 0;
1198         while ((c = getopt(argc, argv, "ugt")) != -1) {
1199                 switch (c) {
1200                 case 'u':
1201                         qctl.qc_type |= 0x01;
1202                         break;
1203                 case 'g':
1204                         qctl.qc_type |= 0x02;
1205                         break;
1206                 case 't':
1207                         qctl.qc_cmd = LUSTRE_Q_SETINFO;
1208                         break;
1209                 default:
1210                         fprintf(stderr, "error: %s: option '-%c' "
1211                                         "unrecognized\n", argv[0], c);
1212                         return CMD_HELP;
1213                 }
1214         }
1215
1216         if (qctl.qc_type)
1217                 qctl.qc_type--;
1218
1219         if (qctl.qc_type == UGQUOTA) {
1220                 fprintf(stderr, "error: user and group quotas can't be set "
1221                                 "both\n");
1222                 return CMD_HELP;
1223         }
1224
1225         if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1226                 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1227
1228                 if (optind + 6 != argc)
1229                         return CMD_HELP;
1230
1231                 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1232                 if (rc) {
1233                         fprintf(stderr, "error: find id for name %s failed: %s\n",
1234                                 argv[optind - 1], strerror(errno));
1235                         return CMD_HELP;
1236                 }
1237
1238                 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1239                 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1240                 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1241                 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1242
1243                 dqb->dqb_valid = QIF_LIMITS;
1244         } else {
1245                 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1246
1247                 if (optind + 3 != argc)
1248                         return CMD_HELP;
1249
1250                 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1251                 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1252         }
1253
1254         mnt = argv[optind];
1255
1256         rc = llapi_quotactl(mnt, &qctl);
1257         if (rc) {
1258                 if (*obd_type)
1259                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1260                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1261                 return rc;
1262         }
1263
1264         return 0;
1265 }
1266
1267 static inline char *type2name(int check_type)
1268 {
1269         if (check_type == USRQUOTA)
1270                 return "user";
1271         else if (check_type == GRPQUOTA)
1272                 return "group";
1273         else
1274                 return "unknown";
1275 }
1276
1277
1278 static void grace2str(time_t seconds,char *buf)
1279 {
1280         uint minutes, hours, days;
1281
1282         minutes = (seconds + 30) / 60;
1283         hours = minutes / 60;
1284         minutes %= 60;
1285         days = hours / 24;
1286         hours %= 24;
1287         if (days >= 2)
1288                 snprintf(buf, 40, "%ddays", days);
1289         else
1290                 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1291 }
1292
1293
1294 static void diff2str(time_t seconds, char *buf, time_t now)
1295 {
1296
1297         buf[0] = 0;
1298         if (!seconds)
1299                 return;
1300         if (seconds <= now) {
1301                 strcpy(buf, "none");
1302                 return;
1303         }
1304         grace2str(seconds - now, buf);
1305 }
1306
1307 static void print_quota_title(char *name, struct if_quotactl *qctl)
1308 {
1309         printf("Disk quotas for %s %s (%cid %u):\n",
1310                type2name(qctl->qc_type), name,
1311                *type2name(qctl->qc_type), qctl->qc_id);
1312         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1313                "Filesystem",
1314                "blocks", "quota", "limit", "grace",
1315                "files", "quota", "limit", "grace");
1316 }
1317
1318 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1319 {
1320         time_t now;
1321
1322         time(&now);
1323
1324         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1325                 int bover = 0, iover = 0;
1326                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1327
1328                 if (dqb->dqb_bhardlimit &&
1329                     toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1330                         bover = 1;
1331                 } else if (dqb->dqb_bsoftlimit &&
1332                            toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1333                         if (dqb->dqb_btime > now) {
1334                                 bover = 2;
1335                         } else {
1336                                 bover = 3;
1337                         }
1338                 }
1339
1340                 if (dqb->dqb_ihardlimit &&
1341                     dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1342                         iover = 1;
1343                 } else if (dqb->dqb_isoftlimit &&
1344                            dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1345                         if (dqb->dqb_btime > now) {
1346                                 iover = 2;
1347                         } else {
1348                                 iover = 3;
1349                         }
1350                 }
1351
1352 #if 0           /* XXX: always print quotas even when no usages */
1353                 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1354 #endif
1355                 {
1356                         char numbuf[3][32];
1357                         char timebuf[40];
1358
1359                         if (strlen(mnt) > 15)
1360                                 printf("%s\n%15s", mnt, "");
1361                         else
1362                                 printf("%15s", mnt);
1363
1364                         if (bover)
1365                                 diff2str(dqb->dqb_btime, timebuf, now);
1366
1367                         sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1368                         sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1369                         sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1370                         printf(" %7s%c %6s %7s %7s",
1371                                numbuf[0], bover ? '*' : ' ', numbuf[1],
1372                                numbuf[2], bover > 1 ? timebuf : "");
1373
1374                         if (iover)
1375                                 diff2str(dqb->dqb_itime, timebuf, now);
1376
1377                         sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1378                         sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1379                         sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1380                         if (!ost_only)
1381                                 printf(" %7s%c %6s %7s %7s",
1382                                        numbuf[0], iover ? '*' : ' ', numbuf[1],
1383                                        numbuf[2], iover > 1 ? timebuf : "");
1384                         printf("\n");
1385                 }
1386         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1387                    qctl->qc_cmd == Q_GETOINFO) {
1388                 char bgtimebuf[40];
1389                 char igtimebuf[40];
1390
1391                 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1392                 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1393                 printf("Block grace time: %s; Inode grace time: %s\n",
1394                        bgtimebuf, igtimebuf);
1395         }
1396 }
1397
1398 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1399 {
1400         int rc;
1401
1402         /* XXX: this is a flag to mark that only mds quota is wanted */
1403         qctl->qc_dqblk.dqb_valid = 1;
1404         rc = llapi_quotactl(mnt, qctl);
1405         if (rc) {
1406                 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1407                 return;
1408         }
1409         qctl->qc_dqblk.dqb_valid = 0;
1410
1411         print_quota(qctl->obd_uuid.uuid, qctl, 0);
1412 }
1413
1414 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1415 {
1416         DIR *dir;
1417         struct obd_uuid uuids[1024], *uuidp;
1418         int obdcount = 1024;
1419         int i, rc;
1420
1421         dir = opendir(mnt);
1422         if (!dir) {
1423                 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1424                 return;
1425         }
1426
1427         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1428         if (rc != 0) {
1429                 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1430                 goto out;
1431         }
1432
1433         for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1434                 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1435
1436                 /* XXX clear this flag to get quota from osts */
1437                 qctl->qc_dqblk.dqb_valid = 0;
1438                 rc = llapi_quotactl(mnt, qctl);
1439                 if (rc) {
1440                         fprintf(stderr, "%s quotactl failed: %s\n",
1441                                 uuidp->uuid, strerror(errno));
1442                         continue;
1443                 }
1444
1445                 print_quota(uuidp->uuid, qctl, 1);
1446         }
1447
1448 out:
1449         closedir(dir);
1450         return;
1451 }
1452
1453 static int lfs_quota(int argc, char **argv)
1454 {
1455         int c;
1456         char *name = NULL, *mnt;
1457         struct if_quotactl qctl;
1458         char *obd_type = qctl.obd_type;
1459         char *obd_uuid = qctl.obd_uuid.uuid;
1460         int rc;
1461
1462         memset(&qctl, 0, sizeof(qctl));
1463         qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1464
1465         optind = 0;
1466         while ((c = getopt(argc, argv, "ugto:")) != -1) {
1467                 switch (c) {
1468                 case 'u':
1469                         qctl.qc_type |= 0x01;
1470                         break;
1471                 case 'g':
1472                         qctl.qc_type |= 0x02;
1473                         break;
1474                 case 't':
1475                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
1476                         break;
1477                 case 'o':
1478                         strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1479                         break;
1480                 default:
1481                         fprintf(stderr, "error: %s: option '-%c' "
1482                                         "unrecognized\n", argv[0], c);
1483                         return CMD_HELP;
1484                 }
1485         }
1486
1487         if (qctl.qc_type)
1488                 qctl.qc_type--;
1489
1490         if (qctl.qc_type == UGQUOTA) {
1491                 fprintf(stderr, "error: user or group can't be specified"
1492                                 "both\n");
1493                 return CMD_HELP;
1494         }
1495
1496         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1497                 if (optind + 2 != argc)
1498                         return CMD_HELP;
1499
1500                 name = argv[optind++];
1501                 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1502                 if (rc) {
1503                         fprintf(stderr, "error: find id for name %s failed: %s\n",
1504                                 name, strerror(errno));
1505                         return CMD_HELP;
1506                 }
1507                 print_quota_title(name, &qctl);
1508         } else if (optind + 1 != argc) {
1509                 return CMD_HELP;
1510         }
1511
1512         mnt = argv[optind];
1513
1514         rc = llapi_quotactl(mnt, &qctl);
1515         if (rc) {
1516                 if (*obd_type)
1517                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1518                 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1519                 return rc;
1520         }
1521
1522         if (!name)
1523                 rc = id2name(&name, getuid(), qctl.qc_type);
1524
1525         if (*obd_uuid) {
1526                 mnt = "";
1527                 name = obd_uuid;
1528         }
1529
1530         print_quota(mnt, &qctl, 0);
1531
1532         if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1533                 print_mds_quota(mnt, &qctl);
1534                 print_lov_quota(mnt, &qctl);
1535         }
1536
1537         return 0;
1538 }
1539 #endif /* HAVE_QUOTA_SUPPORT */
1540
1541 int main(int argc, char **argv)
1542 {
1543         int rc;
1544
1545         setlinebuf(stdout);
1546
1547         ptl_initialize(argc, argv);
1548         if (obd_initialize(argc, argv) < 0)
1549                 exit(2);
1550         if (dbg_initialize(argc, argv) < 0)
1551                 exit(3);
1552
1553         Parser_init("lfs > ", cmdlist);
1554
1555         if (argc > 1) {
1556                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1557         } else {
1558                 rc = Parser_commands();
1559         }
1560
1561         obd_finalize(argc, argv);
1562         return rc;
1563 }