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