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