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