Whamcloud - gitweb
b=19336 Get rid of the EXTRA_VERSION_DELIMETER shenanigans
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/lfs.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  * Author: Robert Read <rread@clusterfs.com>
41  */
42
43 /* for O_DIRECTORY */
44 #ifndef _GNU_SOURCE
45 #define _GNU_SOURCE
46 #endif
47
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <getopt.h>
51 #include <string.h>
52 #include <mntent.h>
53 #include <errno.h>
54 #include <pwd.h>
55 #include <grp.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/param.h>
59 #include <fcntl.h>
60 #include <dirent.h>
61 #include <time.h>
62 #include <ctype.h>
63 #ifdef HAVE_SYS_QUOTA_H
64 #include <sys/quota.h>
65 #endif
66
67 #include <lnet/api-support.h>
68 #include <lnet/lnetctl.h>
69
70 #include <liblustre.h>
71 #include <lustre/lustre_idl.h>
72 #include <lustre/liblustreapi.h>
73 #include <lustre/lustre_user.h>
74 #include <lustre_quota.h>
75
76 #include "parser.h"
77 #include "obdctl.h"
78
79 unsigned int libcfs_subsystem_debug = 0;
80
81 /* all functions */
82 static int lfs_setstripe(int argc, char **argv);
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_osts(int argc, char **argv);
86 static int lfs_df(int argc, char **argv);
87 static int lfs_check(int argc, char **argv);
88 static int lfs_catinfo(int argc, char **argv);
89 #ifdef HAVE_SYS_QUOTA_H
90 static int lfs_quotachown(int argc, char **argv);
91 static int lfs_quotacheck(int argc, char **argv);
92 static int lfs_quotaon(int argc, char **argv);
93 static int lfs_quotaoff(int argc, char **argv);
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_quotainv(int argc, char **argv);
97 #endif
98 static int lfs_join(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_path2fid(int argc, char **argv);
101
102 /* all avaialable commands */
103 command_t cmdlist[] = {
104         {"setstripe", lfs_setstripe, 0,
105          "Create a new file with a specific striping pattern or\n"
106          "set the default striping pattern on an existing directory or\n"
107          "delete the default striping pattern from an existing directory\n"
108          "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
109          "                 [--count|-c stripe_count] [--pool|-p <pool>]\n"
110          "                 <dir|filename>\n"
111          "       or \n"
112          "       setstripe -d <dir>   (to delete default striping)\n"
113          "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n"
114          "\t              Can be specified with k, m or g (in KB, MB and GB\n"
115          "\t              respectively)\n"
116          "\tstart_ost:    OST index of first stripe (-1 filesystem default)\n"
117          "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
118          "\tpool:         Name of OST pool"},
119         {"getstripe", lfs_getstripe, 0,
120          "To list the striping info for a given file or files in a\n"
121          "directory or recursively for all files in a directory tree.\n"
122          "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
123          "                 [--count | -c ] [--size | -s ] [--index | -i ]\n"
124          "                 [--offset | -o ] [--pool | -p ]\n"
125          "                 [--recursive | -r] <dir|file> ..."},
126         {"pool_list", lfs_poollist, 0,
127          "List pools or pool OSTs\n"
128          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
129         {"find", lfs_find, 0,
130          "To find files that match given parameters recursively in a directory tree.\n"
131          "usage: find <dir|file> ... \n"
132          "     [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
133          "     [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
134          "     [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
135          "     [[!] --type|-t <filetype>] [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
136          "     [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
137          "     [[!] --pool <pool>]\n"
138          "\t !: used before an option indicates 'NOT' the requested attribute\n"
139          "\t -: used before an value indicates 'AT MOST' the requested value\n"
140          "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
141         {"check", lfs_check, 0,
142          "Display the status of MDS or OSTs (as specified in the command)\n"
143          "or all the servers (MDS and OSTs).\n"
144          "usage: check <osts|mds|servers>"},
145         {"catinfo", lfs_catinfo, 0,
146          "Show information of specified type logs.\n"
147          "usage: catinfo {keyword} [node name]\n"
148          "\tkeywords are one of followings: config, deletions.\n"
149          "\tnode name must be provided when use keyword config."},
150         {"join", lfs_join, 0,
151          "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
152          "usage: join <filename_A> <filename_B>\n"},
153         {"osts", lfs_osts, 0, "osts"},
154         {"df", lfs_df, 0,
155          "report filesystem disk space usage or inodes usage"
156          "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
157          "Usage: df [-i] [-h] [--pool|-p <fsname>[.<pool>] [path]"},
158 #ifdef HAVE_SYS_QUOTA_H
159         {"quotachown",lfs_quotachown, 0,
160          "Change files' owner or group on the specified filesystem.\n"
161          "usage: quotachown [-i] <filesystem>\n"
162          "\t-i: ignore error if file is not exist\n"},
163         {"quotacheck", lfs_quotacheck, 0,
164          "Scan the specified filesystem for disk usage, and create,\n"
165          "or update quota files.\n"
166          "usage: quotacheck [ -ug ] <filesystem>"},
167         {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
168          "usage: quotaon [ -ugf ] <filesystem>"},
169         {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
170          "usage: quotaoff [ -ug ] <filesystem>"},
171         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
172          "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
173          "                <block-softlimit> <block-hardlimit>\n"
174          "                <inode-softlimit> <inode-hardlimit>\n"
175          "                <filesystem>\n"
176          "       setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
177          "                [--block-softlimit <block-softlimit>]\n"
178          "                [--block-hardlimit <block-hardlimit>]\n"
179          "                [--inode-softlimit <inode-softlimit>]\n"
180          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
181          "       setquota -t <-u|-g> <block-grace> <inode-grace> <filesystem>\n"
182          "       setquota -t <-u|--user|-g|--group>\n"
183          "                [--block-grace <block-grace>]\n"
184          "                [--inode-grace <inode-grace>] <filesystem>\n"
185          "       -b can be used instead of --block-softlimit/--block-grace\n"
186          "       -B can be used instead of --block-hardlimit\n"
187          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
188          "       -I can be used instead of --inode-hardlimit"},
189         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
190          "usage: quota [-v] [ -o obd_uuid ] [<-u|-g> <uname>|<uid>|<gname>|<gid>]\n"
191          "                <filesystem>\n"
192          "       quota [ -o obd_uuid ] -t <-u|-g> <filesystem>"},
193         {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
194          "usage: quotainv [-u|-g] <filesystem>"},
195 #endif
196         {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
197          "usage: path2fid <path>"},
198         {"help", Parser_help, 0, "help"},
199         {"exit", Parser_quit, 0, "quit"},
200         {"quit", Parser_quit, 0, "quit"},
201         { 0, 0, 0, NULL }
202 };
203
204 static int isnumber(const char *str)
205 {
206         const char *ptr;
207
208         if (str[0] != '-' && !isdigit(str[0]))
209                 return 0;
210
211         for (ptr = str + 1; *ptr != '\0'; ptr++) {
212                 if (!isdigit(*ptr))
213                         return 0;
214         }
215
216         return 1;
217 }
218
219 /* functions */
220 static int lfs_setstripe(int argc, char **argv)
221 {
222         char *fname;
223         int result;
224         unsigned long long st_size;
225         int  st_offset, st_count;
226         char *end;
227         int c;
228         int delete = 0;
229         char *stripe_size_arg = NULL;
230         char *stripe_off_arg = NULL;
231         char *stripe_count_arg = NULL;
232         char *pool_name_arg = NULL;
233         unsigned long long size_units;
234
235         struct option long_opts[] = {
236                 {"size",        required_argument, 0, 's'},
237                 {"count",       required_argument, 0, 'c'},
238                 {"index",       required_argument, 0, 'i'},
239                 {"offset",      required_argument, 0, 'o'},
240                 {"pool",        required_argument, 0, 'p'},
241                 {"delete",      no_argument,       0, 'd'},
242                 {0, 0, 0, 0}
243         };
244
245         st_size = 0;
246         st_offset = -1;
247         st_count = 0;
248
249 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
250         if (argc == 5 && argv[1][0] != '-' &&
251             isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
252                 fprintf(stderr, "warning: deprecated usage of setstripe "
253                         "positional parameters.  Use -c, -i, -s instead.\n");
254                 /* for compatibility with the existing positional parameter
255                  * usage */
256                 fname = argv[1];
257                 stripe_size_arg = argv[2];
258                 stripe_off_arg = argv[3];
259                 stripe_count_arg = argv[4];
260                 optind = 4;
261         } else
262 #else
263 #warning "remove obsolete positional parameter code"
264 #endif
265         {
266                 optind = 0;
267                 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
268                                         long_opts, NULL)) >= 0) {
269                         switch (c) {
270                         case 0:
271                                 /* Long options. */
272                                 break;
273                         case 'c':
274                                 stripe_count_arg = optarg;
275                                 break;
276                         case 'd':
277                                 /* delete the default striping pattern */
278                                 delete = 1;
279                                 break;
280                         case 'i':
281                         case 'o':
282                                 stripe_off_arg = optarg;
283                                 break;
284                         case 's':
285                                 stripe_size_arg = optarg;
286                                 break;
287                         case 'p':
288                                 pool_name_arg = optarg;
289                                 break;
290                         case '?':
291                                 return CMD_HELP;
292                         default:
293                                 fprintf(stderr, "error: %s: option '%s' "
294                                                 "unrecognized\n",
295                                                 argv[0], argv[optind - 1]);
296                                 return CMD_HELP;
297                         }
298                 }
299
300                 fname = argv[optind];
301
302                 if (delete &&
303                     (stripe_size_arg != NULL || stripe_off_arg != NULL ||
304                      stripe_count_arg != NULL || pool_name_arg != NULL)) {
305                         fprintf(stderr, "error: %s: cannot specify -d with "
306                                         "-s, -c -o or -p options\n",
307                                         argv[0]);
308                         return CMD_HELP;
309                 }
310         }
311
312         if (optind == argc) {
313                 fprintf(stderr, "error: %s: missing filename|dirname\n",
314                         argv[0]);
315                 return CMD_HELP;
316         }
317
318         /* get the stripe size */
319         if (stripe_size_arg != NULL) {
320                 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
321                 if (result) {
322                         fprintf(stderr, "error: %s: bad size '%s'\n",
323                                 argv[0], stripe_size_arg);
324                         return result;
325                 }
326         }
327         /* get the stripe offset */
328         if (stripe_off_arg != NULL) {
329                 st_offset = strtoul(stripe_off_arg, &end, 0);
330                 if (*end != '\0') {
331                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
332                                         argv[0], stripe_off_arg);
333                         return CMD_HELP;
334                 }
335         }
336         /* get the stripe count */
337         if (stripe_count_arg != NULL) {
338                 st_count = strtoul(stripe_count_arg, &end, 0);
339                 if (*end != '\0') {
340                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
341                                         argv[0], stripe_count_arg);
342                         return CMD_HELP;
343                 }
344         }
345
346         do {
347                 result = llapi_file_create_pool(fname, st_size, st_offset,
348                                                 st_count, 0, pool_name_arg);
349                 if (result) {
350                         fprintf(stderr,"error: %s: create stripe file '%s' "
351                                 "failed\n", argv[0], fname);
352                         break;
353                 }
354                 fname = argv[++optind];
355         } while (fname != NULL);
356
357         return result;
358 }
359
360 static int lfs_poollist(int argc, char **argv)
361 {
362         if (argc != 2)
363                 return CMD_HELP;
364
365         return llapi_poollist(argv[1]);
366 }
367
368 static int set_time(time_t *time, time_t *set, char *str)
369 {
370         time_t t;
371         int res = 0;
372
373         if (str[0] == '+')
374                 res = 1;
375         else if (str[0] == '-')
376                 res = -1;
377
378         if (res)
379                 str++;
380
381         t = strtol(str, NULL, 0);
382         if (*time < t * 24 * 60 * 60) {
383                 if (res)
384                         str--;
385                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
386                 return INT_MAX;
387         }
388
389         *set = *time - t * 24 * 60 * 60;
390         return res;
391 }
392
393 #define USER 0
394 #define GROUP 1
395
396 static int name2id(unsigned int *id, char *name, int type)
397 {
398         if (type == USER) {
399                 struct passwd *entry;
400
401                 if (!(entry = getpwnam(name))) {
402                         if (!errno)
403                                 errno = ENOENT;
404                         return -1;
405                 }
406
407                 *id = entry->pw_uid;
408         } else {
409                 struct group *entry;
410
411                 if (!(entry = getgrnam(name))) {
412                         if (!errno)
413                                 errno = ENOENT;
414                         return -1;
415                 }
416
417                 *id = entry->gr_gid;
418         }
419
420         return 0;
421 }
422
423 static int id2name(char **name, unsigned int id, int type)
424 {
425         if (type == USER) {
426                 struct passwd *entry;
427
428                 if (!(entry = getpwuid(id))) {
429                         if (!errno)
430                                 errno = ENOENT;
431                         return -1;
432                 }
433
434                 *name = entry->pw_name;
435         } else {
436                 struct group *entry;
437
438                 if (!(entry = getgrgid(id))) {
439                         if (!errno)
440                                 errno = ENOENT;
441                         return -1;
442                 }
443
444                 *name = entry->gr_name;
445         }
446
447         return 0;
448 }
449
450 #define FIND_POOL_OPT 3
451 static int lfs_find(int argc, char **argv)
452 {
453         int new_fashion = 1;
454         int c, ret;
455         time_t t;
456         struct find_param param = { .maxdepth = -1 };
457         char str[1024];
458         struct option long_opts[] = {
459                 /* New find options. */
460                 {"atime",     required_argument, 0, 'A'},
461                 {"ctime",     required_argument, 0, 'C'},
462                 {"mtime",     required_argument, 0, 'M'},
463                 {"maxdepth",  required_argument, 0, 'D'},
464                 {"gid",       required_argument, 0, 'g'},
465                 {"group",     required_argument, 0, 'G'},
466                 {"uid",       required_argument, 0, 'u'},
467                 {"user",      required_argument, 0, 'U'},
468                 {"name",      required_argument, 0, 'n'},
469                 /* no short option for pool, p/P already used */
470                 {"pool",      required_argument, 0, FIND_POOL_OPT},
471                 /* --obd is considered as a new option. */
472                 {"obd",       required_argument, 0, 'O'},
473                 {"ost",       required_argument, 0, 'O'},
474                 {"print",     no_argument,       0, 'P'},
475                 {"print0",    no_argument,       0, 'p'},
476                 /* Old find options. */
477                 {"quiet",     no_argument,       0, 'q'},
478                 {"recursive", no_argument,       0, 'r'},
479                 {"size",      required_argument, 0, 's'},
480                 {"type",      required_argument, 0, 't'},
481                 {"verbose",   no_argument,       0, 'v'},
482                 {0, 0, 0, 0}
483         };
484         int pathstart = -1;
485         int pathend = -1;
486         int neg_opt = 0;
487         time_t *xtime;
488         int *xsign;
489         int isoption;
490         char *endptr;
491
492         time(&t);
493
494         optind = 0;
495         /* when getopt_long_only() hits '!' it returns 1 and puts "!" in optarg */
496         while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
497                                      long_opts, NULL)) >= 0) {
498                 xtime = NULL;
499                 xsign = NULL;
500                 if (neg_opt)
501                         --neg_opt;
502                 /* '!' is part of option */
503                 /* when getopt_long_only() finds a string which is not
504                  * an option nor a known option argument it returns 1
505                  * in that case if we already have found pathstart and pathend
506                  * (i.e. we have the list of pathnames),
507                  * the only supported value is "!"
508                  */
509                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
510                 if (!isoption && pathend != -1) {
511                         fprintf(stderr, "err: %s: filename|dirname must either "
512                                         "precede options or follow options\n",
513                                         argv[0]);
514                         return CMD_HELP;
515                 }
516                 if (!isoption && pathstart == -1)
517                         pathstart = optind - 1;
518                 if (isoption && pathstart != -1 && pathend == -1) {
519                         pathend = optind - 2;
520                         if ((c == 1 && strcmp(optarg, "!") == 0) ||
521                             c == 'P' || c == 'p' || c == 'O' ||
522                             c == 'q' || c == 'r' || c == 'v')
523                                 pathend = optind - 1;
524                 }
525                 switch (c) {
526                 case 0:
527                         /* Long options. */
528                         break;
529                 case 1:
530                         /* unknown; opt is "!" or path component,
531                          * checking done above.
532                          */
533                         if (strcmp(optarg, "!") == 0)
534                                 neg_opt = 2;
535                       break;
536                 case 'A':
537                         xtime = &param.atime;
538                         xsign = &param.asign;
539                 case 'C':
540                         if (c == 'C') {
541                                 xtime = &param.ctime;
542                                 xsign = &param.csign;
543                         }
544                 case 'M':
545                         if (c == 'M') {
546                                 xtime = &param.mtime;
547                                 xsign = &param.msign;
548                         }
549                         new_fashion = 1;
550                         if (neg_opt) {
551                                 if (optarg[0] == '-')
552                                         optarg[0] = '+';
553                                 else if (optarg[0] == '+')
554                                         optarg[0] = '-';
555                                 else {
556                                         str[0] = '-';
557                                         str[1] = '\0';
558                                         strcat(str, optarg);
559                                         optarg = str;
560                                 }
561                         }
562                         ret = set_time(&t, xtime, optarg);
563                         if (ret == INT_MAX)
564                                 return -1;
565                         if (ret)
566                                 *xsign = ret;
567                         break;
568                 case 'D':
569                         new_fashion = 1;
570                         param.maxdepth = strtol(optarg, 0, 0);
571                         break;
572                 case 'g':
573                 case 'G':
574                         new_fashion = 1;
575                         ret = name2id(&param.gid, optarg, GROUP);
576                         if (ret) {
577                                 param.gid = strtoul(optarg, &endptr, 10);
578                                 if (*endptr != '\0') {
579                                         fprintf(stderr, "Group/GID: %s cannot "
580                                                 "be found.\n", optarg);
581                                         return -1;
582                                 }
583                         }
584                         param.exclude_gid = !!neg_opt;
585                         param.check_gid = 1;
586                         break;
587                 case 'u':
588                 case 'U':
589                         new_fashion = 1;
590                         ret = name2id(&param.uid, optarg, USER);
591                         if (ret) {
592                                 param.uid = strtoul(optarg, &endptr, 10);
593                                 if (*endptr != '\0') {
594                                         fprintf(stderr, "User/UID: %s cannot "
595                                                 "be found.\n", optarg);
596                                         return -1;
597                                 }
598                         }
599                         param.exclude_uid = !!neg_opt;
600                         param.check_uid = 1;
601                         break;
602                 case FIND_POOL_OPT:
603                         new_fashion = 1;
604                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
605                                 fprintf(stderr,
606                                         "Pool name %s is too long"
607                                         " (max is %d)\n", optarg,
608                                         LOV_MAXPOOLNAME);
609                                 return -1;
610                         }
611                         /* we do check for empty pool because empty pool
612                          * is used to find V1 lov attributes */
613                         strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
614                         param.poolname[LOV_MAXPOOLNAME] = '\0';
615                         param.exclude_pool = !!neg_opt;
616                         param.check_pool = 1;
617                         break;
618                 case 'n':
619                         new_fashion = 1;
620                         param.pattern = (char *)optarg;
621                         param.exclude_pattern = !!neg_opt;
622                         break;
623                 case 'O': {
624                         char *buf, *token, *next, *p;
625                         int len;
626
627                         len = strlen((char *)optarg);
628                         buf = malloc(len+1);
629                         if (buf == NULL)
630                                 return -ENOMEM;
631                         strcpy(buf, (char *)optarg);
632
633                         if (param.num_alloc_obds == 0) {
634                                 param.obduuid = malloc(FIND_MAX_OSTS *
635                                                        sizeof(struct obd_uuid));
636                                 if (param.obduuid == NULL)
637                                         return -ENOMEM;
638                                 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
639                         }
640
641                         for (token = buf; token && *token; token = next) {
642                                 p = strchr(token, ',');
643                                 next = 0;
644                                 if (p) {
645                                         *p = 0;
646                                         next = p+1;
647                                 }
648                                 strcpy((char *)&param.obduuid[param.num_obds++].uuid,
649                                        token);
650                         }
651
652                         if (buf)
653                                 free(buf);
654                         break;
655                 }
656                 case 'p':
657                         new_fashion = 1;
658                         param.zeroend = 1;
659                         break;
660                 case 'P':
661                         break;
662                 case 'q':
663                         new_fashion = 0;
664                         param.quiet++;
665                         param.verbose = 0;
666                         break;
667                 case 'r':
668                         new_fashion = 0;
669                         param.recursive = 1;
670                         break;
671                 case 't':
672                         param.exclude_type = !!neg_opt;
673                         switch(optarg[0]) {
674                         case 'b': param.type = S_IFBLK; break;
675                         case 'c': param.type = S_IFCHR; break;
676                         case 'd': param.type = S_IFDIR; break;
677                         case 'f': param.type = S_IFREG; break;
678                         case 'l': param.type = S_IFLNK; break;
679                         case 'p': param.type = S_IFIFO; break;
680                         case 's': param.type = S_IFSOCK; break;
681 #ifdef S_IFDOOR /* Solaris only */
682                         case 'D': param.type = S_IFDOOR; break;
683 #endif
684                         default: fprintf(stderr, "error: %s: bad type '%s'\n",
685                                          argv[0], optarg);
686                                  return CMD_HELP;
687                         };
688                         break;
689                 case 's':
690                         if (neg_opt) {
691                                 if (optarg[0] == '-')
692                                         optarg[0] = '+';
693                                 else if (optarg[0] == '+')
694                                         optarg[0] = '-';
695                                 else {
696                                         str[0] = '-';
697                                         str[1] = '\0';
698                                         strcat(str, optarg);
699                                         optarg = str;
700                                 }
701                         }
702                         if (optarg[0] == '+')
703                                 param.size_sign = -1;
704                         else if (optarg[0] == '-')
705                                 param.size_sign = +1;
706
707                         if (param.size_sign)
708                                 optarg++;
709                         ret = parse_size(optarg, &param.size,
710                                          &param.size_units, 0);
711                         if (ret) {
712                                 fprintf(stderr,"error: bad size '%s'\n",
713                                         optarg);
714                                 return ret;
715                         }
716                         param.size_check = 1;
717                         break;
718                 case 'v':
719                         new_fashion = 0;
720                         param.verbose++;
721                         param.quiet = 0;
722                         break;
723                 case '?':
724                         return CMD_HELP;
725                 default:
726                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
727                                 argv[0], argv[optind - 1]);
728                         return CMD_HELP;
729                 };
730         }
731
732         if (pathstart == -1) {
733                 fprintf(stderr, "error: %s: no filename|pathname\n",
734                                 argv[0]);
735                 return CMD_HELP;
736         } else if (pathend == -1) {
737                 /* no options */
738                 pathend = argc;
739         }
740
741         if (new_fashion) {
742                 param.quiet = 1;
743         } else {
744                 static int deprecated_warning;
745                 if (!deprecated_warning) {
746                         fprintf(stderr, "lfs find: -q, -r, -v options "
747                                 "deprecated.  Use 'lfs getstripe' instead.\n");
748                         deprecated_warning = 1;
749                 }
750                 if (!param.recursive && param.maxdepth == -1)
751                         param.maxdepth = 1;
752         }
753
754         do {
755                 if (new_fashion)
756                         ret = llapi_find(argv[pathstart], &param);
757                 else
758                         ret = llapi_getstripe(argv[pathstart], &param);
759         } while (++pathstart < pathend && !ret);
760
761         if (ret)
762                 fprintf(stderr, "error: %s failed for %s.\n",
763                         argv[0], argv[optind - 1]);
764
765         if (param.obduuid && param.num_alloc_obds)
766                 free(param.obduuid);
767
768         return ret;
769 }
770
771 static int lfs_getstripe(int argc, char **argv)
772 {
773         struct option long_opts[] = {
774                 {"obd", 1, 0, 'O'},
775                 {"quiet", 0, 0, 'q'},
776                 {"recursive", 0, 0, 'r'},
777                 {"count", 0, 0, 'c'},
778                 {"size", 0, 0, 's'},
779                 {"index", 0, 0, 'i'},
780                 {"offset", 0, 0, 'o'},
781                 {"pool", 0, 0, 'p'},
782                 {"verbose", 0, 0, 'v'},
783                 {"directory", 0, 0, 'd'},
784                 {0, 0, 0, 0}
785         };
786         int c, rc;
787         struct find_param param = { 0 };
788
789         param.maxdepth = 1;
790         optind = 0;
791         while ((c = getopt_long(argc, argv, "cdhioO:pqrsv",
792                                 long_opts, NULL)) != -1) {
793                 switch (c) {
794                 case 'O':
795                         if (param.obduuid) {
796                                 fprintf(stderr,
797                                         "error: %s: only one obduuid allowed",
798                                         argv[0]);
799                                 return CMD_HELP;
800                         }
801                         param.obduuid = (struct obd_uuid *)optarg;
802                         break;
803                 case 'q':
804                         param.quiet++;
805                         break;
806                 case 'd':
807                         param.maxdepth = 0;
808                         break;
809                 case 'r':
810                         param.recursive = 1;
811                         break;
812                 case 'v':
813                         param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
814                         break;
815                 case 'c':
816                         if (!(param.verbose & VERBOSE_DETAIL)) {
817                                 param.verbose |= VERBOSE_COUNT;
818                                 param.maxdepth = 0;
819                         }
820                         break;
821                 case 's':
822                         if (!(param.verbose & VERBOSE_DETAIL)) {
823                                 param.verbose |= VERBOSE_SIZE;
824                                 param.maxdepth = 0;
825                         }
826                         break;
827                 case 'i':
828                 case 'o':
829                         if (!(param.verbose & VERBOSE_DETAIL)) {
830                                 param.verbose |= VERBOSE_OFFSET;
831                                 param.maxdepth = 0;
832                         }
833                         break;
834                 case 'p':
835                         param.verbose |= VERBOSE_POOL;
836                         break;
837                 case '?':
838                         return CMD_HELP;
839                 default:
840                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
841                                 argv[0], argv[optind - 1]);
842                         return CMD_HELP;
843                 }
844         }
845
846         if (optind >= argc)
847                 return CMD_HELP;
848
849         if (param.recursive)
850                 param.maxdepth = -1;
851
852         if (!param.verbose)
853                 param.verbose = VERBOSE_ALL;
854         if (param.quiet)
855                 param.verbose = VERBOSE_OBJID;
856
857         do {
858                 rc = llapi_getstripe(argv[optind], &param);
859         } while (++optind < argc && !rc);
860
861         if (rc)
862                 fprintf(stderr, "error: %s failed for %s.\n",
863                         argv[0], argv[optind - 1]);
864         return rc;
865 }
866
867 static int lfs_osts(int argc, char **argv)
868 {
869         char mntdir[PATH_MAX] = {'\0'};
870         struct find_param param;
871         int index = 0, rc = 0;
872
873         if (argc != 1)
874                 return CMD_HELP;
875
876         while (llapi_search_mounts(NULL, index++, mntdir, NULL) == 0) {
877                 memset(&param, 0, sizeof(param));
878                 rc = llapi_ostlist(mntdir, &param);
879                 if (rc) {
880                         fprintf(stderr, "error: %s: failed on %s\n",
881                                 argv[0], mntdir);
882                 }
883                 memset(mntdir, 0, PATH_MAX);
884         }
885
886         return rc;
887 }
888
889 static int lfs_path2fid(int argc, char **argv)
890 {
891         char *path;
892         lustre_fid fid;
893         int rc;
894
895         if (argc != 2)
896                 return CMD_HELP;
897
898         path = argv[1];
899         rc = llapi_path2fid(path, &fid);
900         if (rc) {
901                 fprintf(stderr, "can't get fid for %s: %s\n", path,
902                         strerror(errno = -rc));
903                 return rc;
904         }
905
906         printf(DFID"\n", PFID(&fid));
907
908         return 0;
909 }
910
911 #define COOK(value)                                                     \
912 ({                                                                      \
913         int radix = 0;                                                  \
914         while (value > 1024) {                                          \
915                 value /= 1024;                                          \
916                 radix++;                                                \
917         }                                                               \
918         radix;                                                          \
919 })
920 #define UUF     "%-20s"
921 #define CSF     "%9s"
922 #define CDF     "%9llu"
923 #define HSF     "%8s"
924 #define HDF     "%6.1f"
925 #define RSF     "%5s"
926 #define RDF     "%4d%%"
927
928 static int showdf(char *mntdir, struct obd_statfs *stat,
929                   char *uuid, int ishow, int cooked,
930                   char *type, int index, int rc)
931 {
932         long long avail, used, total;
933         double ratio = 0;
934         char *suffix = "KMGTPEZY";
935         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
936         char tbuf[20], ubuf[20], abuf[20], rbuf[20];
937
938         if (!uuid || !stat)
939                 return -EINVAL;
940
941         switch (rc) {
942         case 0:
943                 if (ishow) {
944                         avail = stat->os_ffree;
945                         used = stat->os_files - stat->os_ffree;
946                         total = stat->os_files;
947                 } else {
948                         int shift = cooked ? 0 : 10;
949
950                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
951                         used  = ((stat->os_blocks - stat->os_bfree) *
952                                  stat->os_bsize) >> shift;
953                         total = (stat->os_blocks * stat->os_bsize) >> shift;
954                 }
955
956                 if (total > 0)
957                         ratio = (double)used / (double)total;
958
959                 if (cooked) {
960                         int i;
961                         double cook_val;
962
963                         cook_val = (double)total;
964                         i = COOK(cook_val);
965                         if (i > 0)
966                                 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
967                         else
968                                 sprintf(tbuf, CDF, total);
969
970                         cook_val = (double)used;
971                         i = COOK(cook_val);
972                         if (i > 0)
973                                 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
974                         else
975                                 sprintf(ubuf, CDF, used);
976
977                         cook_val = (double)avail;
978                         i = COOK(cook_val);
979                         if (i > 0)
980                                 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
981                         else
982                                 sprintf(abuf, CDF, avail);
983                 } else {
984                         sprintf(tbuf, CDF, total);
985                         sprintf(ubuf, CDF, used);
986                         sprintf(abuf, CDF, avail);
987                 }
988
989                 sprintf(rbuf, RDF, (int)(ratio * 100));
990                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
991                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
992                 if (type)
993                         printf("[%s:%d]\n", type, index);
994                 else
995                         printf("\n");
996
997                 break;
998         case -ENODATA:
999                 printf(UUF": inactive device\n", uuid);
1000                 break;
1001         default:
1002                 printf(UUF": %s\n", uuid, strerror(-rc));
1003                 break;
1004         }
1005
1006         return 0;
1007 }
1008
1009 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow, int cooked)
1010 {
1011         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1012         struct obd_uuid uuid_buf;
1013         char *poolname = NULL;
1014         __u32 index;
1015         int rc;
1016
1017         if (pool) {
1018                 poolname = strchr(pool, '.');
1019                 if (poolname != NULL) {
1020                         if (strncmp(fsname, pool, strlen(fsname))) {
1021                                 fprintf(stderr, "filesystem name incorrect\n");
1022                                 return -ENODEV;
1023                         }
1024                         poolname++;
1025                 } else
1026                         poolname = pool;
1027         }
1028
1029         if (ishow)
1030                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1031                        "UUID", "Inodes", "IUsed", "IFree",
1032                        "IUse%", "Mounted on");
1033         else
1034                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1035                        "UUID", cooked ? "bytes" : "1K-blocks",
1036                        "Used", "Available", "Use%", "Mounted on");
1037
1038         for (index = 0; ; index++) {
1039                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1040                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1041                 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1042                                       &stat_buf, &uuid_buf);
1043                 if (rc == -ENODEV)
1044                         break;
1045
1046                 if (rc == -EAGAIN)
1047                         continue;
1048
1049                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1050                     rc == -ENODATA || rc == 0) {
1051                         showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1052                                ishow, cooked, "MDT", index, rc);
1053                 } else {
1054                         fprintf(stderr,
1055                                 "error: llapi_obd_statfs(%s): %s (%d)\n",
1056                                 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1057                         return rc;
1058                 }
1059                 if (rc == 0) {
1060                         sum.os_ffree += stat_buf.os_ffree;
1061                         sum.os_files += stat_buf.os_files;
1062                 }
1063         }
1064
1065         for (index = 0; ; index++) {
1066                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1067                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1068                 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1069                                       &stat_buf, &uuid_buf);
1070                 if (rc == -ENODEV)
1071                         break;
1072
1073                 if (rc == -EAGAIN)
1074                         continue;
1075
1076                 if (llapi_search_ost(fsname, poolname,
1077                                      obd_uuid2str(&uuid_buf)) != 1)
1078                         continue;
1079
1080                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1081                     rc == -ENODATA || rc == 0) {
1082                         showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1083                                ishow, cooked, "OST", index, rc);
1084                 } else {
1085                         fprintf(stderr,
1086                                 "error: llapi_obd_statfs failed: %s (%d)\n",
1087                                 strerror(-rc), rc);
1088                         return rc;
1089                 }
1090                 if (rc == 0) {
1091                         sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1092                         sum.os_bfree  += stat_buf.os_bfree * stat_buf.os_bsize;
1093                         sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1094                 }
1095         }
1096
1097         printf("\n");
1098         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1099
1100         return 0;
1101 }
1102
1103 static int lfs_df(int argc, char **argv)
1104 {
1105         char *path = NULL;
1106         char *mntdir = NULL;
1107         int ishow = 0, cooked = 0;
1108         int c, rc = 0;
1109         char fsname[PATH_MAX], *pool_name = NULL;
1110         struct option long_opts[] = {
1111                 {"pool", required_argument, 0, 'p'},
1112                 {0, 0, 0, 0}
1113         };
1114
1115         optind = 0;
1116         while ((c = getopt_long(argc, argv, "ihp:", long_opts, NULL)) != -1) {
1117                 switch (c) {
1118                 case 'i':
1119                         ishow = 1;
1120                         break;
1121                 case 'h':
1122                         cooked = 1;
1123                         break;
1124                 case 'p':
1125                         pool_name = optarg;
1126                         break;
1127                 default:
1128                         return CMD_HELP;
1129                 }
1130         }
1131         if (optind < argc )
1132                 path = argv[optind];
1133
1134         if ((mntdir = malloc(PATH_MAX)) == NULL) {
1135                 fprintf(stderr, "error: cannot allocate %d bytes\n",
1136                         PATH_MAX);
1137                 return -ENOMEM;
1138         }
1139         memset(mntdir, 0, PATH_MAX);
1140
1141         if (path) {
1142                 char rpath[PATH_MAX] = {'\0'};
1143
1144                 if (!realpath(path, rpath)) {
1145                         rc = -errno;
1146                         fprintf(stderr, "error: invalid path '%s': %s\n",
1147                                 path, strerror(-rc));
1148                         return rc;
1149                 }
1150
1151                 rc = llapi_search_mounts(rpath, 0, mntdir, fsname);
1152                 if (rc == 0 && mntdir[0] != '\0') {
1153                         rc = mntdf(mntdir, fsname, pool_name, ishow, cooked);
1154                         printf("\n");
1155                 }
1156         } else {
1157                 int index = 0;
1158
1159                 while (!llapi_search_mounts(NULL, index++, mntdir, fsname)) {
1160                         rc = mntdf(mntdir, fsname, pool_name, ishow, cooked);
1161                         if (rc)
1162                                 break;
1163                         printf("\n");
1164                 }
1165         }
1166
1167         free(mntdir);
1168         return rc;
1169 }
1170
1171 static int lfs_check(int argc, char **argv)
1172 {
1173         int rc;
1174         int num_types = 1;
1175         char mntdir[PATH_MAX] = {'\0'};
1176         char *obd_types[2];
1177         char obd_type1[4];
1178         char obd_type2[4];
1179
1180         if (argc != 2)
1181                 return CMD_HELP;
1182
1183         obd_types[0] = obd_type1;
1184         obd_types[1] = obd_type2;
1185
1186         if (strcmp(argv[1], "osts") == 0) {
1187                 strcpy(obd_types[0], "osc");
1188         } else if (strcmp(argv[1], "mds") == 0) {
1189                 strcpy(obd_types[0], "mdc");
1190         } else if (strcmp(argv[1], "servers") == 0) {
1191                 num_types = 2;
1192                 strcpy(obd_types[0], "osc");
1193                 strcpy(obd_types[1], "mdc");
1194         } else {
1195                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1196                                 argv[0], argv[1]);
1197                         return CMD_HELP;
1198         }
1199
1200         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1201         if (rc < 0 || mntdir[0] == '\0') {
1202                 fprintf(stderr, "No suitable Lustre mount found\n");
1203                 return rc;
1204         }
1205
1206         rc = llapi_target_iterate(num_types, obd_types,
1207                                   mntdir, llapi_ping_target);
1208
1209         if (rc)
1210                 fprintf(stderr, "error: %s: %s status failed\n",
1211                                 argv[0],argv[1]);
1212
1213         return rc;
1214
1215 }
1216
1217 static int lfs_catinfo(int argc, char **argv)
1218 {
1219         char mntdir[PATH_MAX] = {'\0'};
1220         int rc;
1221
1222         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1223                 return CMD_HELP;
1224
1225         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1226                 return CMD_HELP;
1227
1228         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1229         if (rc == 0 && mntdir[0] != '\0') {
1230                 if (argc == 3)
1231                         rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1232                 else
1233                         rc = llapi_catinfo(mntdir, argv[1], NULL);
1234         } else {
1235                 fprintf(stderr, "no lustre_lite mounted.\n");
1236                 rc = -1;
1237         }
1238
1239         return rc;
1240 }
1241
1242 int lfs_join(int argc, char **argv)
1243 {
1244         char *name_head, *name_tail;
1245         int fd, rc;
1246         loff_t size;
1247
1248         if (argc != 3)
1249                 return CMD_HELP;
1250         name_head = argv[1];
1251         fd = open(name_head, O_WRONLY);
1252         if (fd < 0) {
1253                 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1254                         name_head, fd);
1255                 return fd;
1256         }
1257         size = lseek(fd, 0, SEEK_END);
1258         if (size % JOIN_FILE_ALIGN) {
1259                 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1260                         name_head, (long long)size, JOIN_FILE_ALIGN);
1261                 rc = -EINVAL;
1262                 goto out;
1263         }
1264         name_tail = argv[2];
1265         rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1266 out:
1267         close(fd);
1268         if (rc) {
1269                 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1270                         argv[1], argv[2]);
1271         }
1272         return rc;
1273 }
1274
1275 #ifdef HAVE_SYS_QUOTA_H
1276 static int lfs_quotachown(int argc, char **argv)
1277 {
1278
1279         int c,rc;
1280         int flag = 0;
1281
1282         optind = 0;
1283         while ((c = getopt(argc, argv, "i")) != -1) {
1284                 switch (c) {
1285                 case 'i':
1286                         flag++;
1287                         break;
1288                 default:
1289                         fprintf(stderr, "error: %s: option '-%c' "
1290                                         "unrecognized\n", argv[0], c);
1291                         return CMD_HELP;
1292                 }
1293         }
1294         if (optind == argc)
1295                 return CMD_HELP;
1296         rc = llapi_quotachown(argv[optind], flag);
1297         if(rc)
1298                 fprintf(stderr,"error: change file owner/group failed.\n");
1299         return rc;
1300 }
1301
1302 static int lfs_quotacheck(int argc, char **argv)
1303 {
1304         int c, check_type = 0;
1305         char *mnt;
1306         struct if_quotacheck qchk;
1307         struct if_quotactl qctl;
1308         char *obd_type = (char *)qchk.obd_type;
1309         int rc;
1310
1311         memset(&qchk, 0, sizeof(qchk));
1312
1313         optind = 0;
1314         while ((c = getopt(argc, argv, "ug")) != -1) {
1315                 switch (c) {
1316                 case 'u':
1317                         check_type |= 0x01;
1318                         break;
1319                 case 'g':
1320                         check_type |= 0x02;
1321                         break;
1322                 default:
1323                         fprintf(stderr, "error: %s: option '-%c' "
1324                                         "unrecognized\n", argv[0], c);
1325                         return CMD_HELP;
1326                 }
1327         }
1328
1329         if (check_type)
1330                 check_type--;
1331         else    /* do quotacheck for both user & group quota by default */
1332                 check_type = 0x02;
1333
1334         if (argc == optind)
1335                 return CMD_HELP;
1336
1337         mnt = argv[optind];
1338
1339         memset(&qctl, 0, sizeof(qctl));
1340         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1341         qctl.qc_type = check_type;
1342         qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1343                                     * take this parameter into account */
1344         rc = llapi_quotactl(mnt, &qctl);
1345         if (rc) {
1346                 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1347                 return rc;
1348         }
1349
1350         rc = llapi_quotacheck(mnt, check_type);
1351         if (rc) {
1352                 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1353                 return rc;
1354         }
1355
1356         rc = llapi_poll_quotacheck(mnt, &qchk);
1357         if (rc) {
1358                 if (*obd_type)
1359                         fprintf(stderr, "%s %s ", obd_type,
1360                                 obd_uuid2str(&qchk.obd_uuid));
1361                 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1362                 return rc;
1363         }
1364
1365         memset(&qctl, 0, sizeof(qctl));
1366         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1367         qctl.qc_type = check_type;
1368         qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1369                                     * take this parameter into account */
1370         rc = llapi_quotactl(mnt, &qctl);
1371         if (rc) {
1372                 if (*obd_type)
1373                         fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1374                                 obd_uuid2str(&qctl.obd_uuid));
1375                 fprintf(stderr, "%s turn on quota failed: %s\n",
1376                         argv[0], strerror(errno));
1377                 return rc;
1378         }
1379
1380         return 0;
1381 }
1382
1383 static int lfs_quotaon(int argc, char **argv)
1384 {
1385         int c;
1386         char *mnt;
1387         struct if_quotactl qctl;
1388         char *obd_type = (char *)qctl.obd_type;
1389         int rc;
1390
1391         memset(&qctl, 0, sizeof(qctl));
1392         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1393         qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1394                                     * take this parameter into account */
1395
1396         optind = 0;
1397         while ((c = getopt(argc, argv, "ugf")) != -1) {
1398                 switch (c) {
1399                 case 'u':
1400                         qctl.qc_type |= 0x01;
1401                         break;
1402                 case 'g':
1403                         qctl.qc_type |= 0x02;
1404                         break;
1405                 case 'f':
1406                         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1407                         break;
1408                 default:
1409                         fprintf(stderr, "error: %s: option '-%c' "
1410                                         "unrecognized\n", argv[0], c);
1411                         return CMD_HELP;
1412                 }
1413         }
1414
1415         if (qctl.qc_type)
1416                 qctl.qc_type--;
1417         else /* by default, enable quota for both user & group */
1418                 qctl.qc_type = 0x02;
1419
1420         if (argc == optind)
1421                 return CMD_HELP;
1422
1423         mnt = argv[optind];
1424
1425         rc = llapi_quotactl(mnt, &qctl);
1426         if (rc) {
1427                 if (*obd_type)
1428                         fprintf(stderr, "%s %s ", obd_type,
1429                                 obd_uuid2str(&qctl.obd_uuid));
1430                 if (errno == ENOENT)
1431                         fprintf(stderr, "error: cannot find quota database, "
1432                                         "make sure you have run quotacheck\n");
1433                 else
1434                         fprintf(stderr, "error: quotaon failed (%s)\n",
1435                                 strerror(errno));
1436                 return rc;
1437         }
1438
1439         return 0;
1440 }
1441
1442 static int lfs_quotaoff(int argc, char **argv)
1443 {
1444         int c;
1445         char *mnt;
1446         struct if_quotactl qctl;
1447         char *obd_type = (char *)qctl.obd_type;
1448         int rc;
1449
1450         memset(&qctl, 0, sizeof(qctl));
1451         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1452
1453         optind = 0;
1454         while ((c = getopt(argc, argv, "ug")) != -1) {
1455                 switch (c) {
1456                 case 'u':
1457                         qctl.qc_type |= 0x01;
1458                         break;
1459                 case 'g':
1460                         qctl.qc_type |= 0x02;
1461                         break;
1462                 default:
1463                         fprintf(stderr, "error: %s: option '-%c' "
1464                                         "unrecognized\n", argv[0], c);
1465                         return CMD_HELP;
1466                 }
1467         }
1468
1469         if (qctl.qc_type)
1470                 qctl.qc_type--;
1471         else /* by default, disable quota for both user & group */
1472                 qctl.qc_type = 0x02;
1473
1474         if (argc == optind)
1475                 return CMD_HELP;
1476
1477         mnt = argv[optind];
1478
1479         rc = llapi_quotactl(mnt, &qctl);
1480         if (rc == -1 && errno == ESRCH) {
1481                 fprintf(stderr, "\n%s quotas are not enabled.\n",
1482                         qctl.qc_type == 0x00 ? "user" : "group");
1483                 return 0;
1484         }
1485         if (rc) {
1486                 if (*obd_type)
1487                         fprintf(stderr, "%s %s ", obd_type,
1488                                 obd_uuid2str(&qctl.obd_uuid));
1489                 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1490                 return rc;
1491         }
1492
1493         return 0;
1494 }
1495
1496 static int lfs_quotainv(int argc, char **argv)
1497 {
1498         int c;
1499         char *mnt;
1500         struct if_quotactl qctl;
1501         int rc;
1502
1503         memset(&qctl, 0, sizeof(qctl));
1504         qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1505
1506         optind = 0;
1507         while ((c = getopt(argc, argv, "ugf")) != -1) {
1508                 switch (c) {
1509                 case 'u':
1510                         qctl.qc_type |= 0x01;
1511                         break;
1512                 case 'g':
1513                         qctl.qc_type |= 0x02;
1514                         break;
1515                 case 'f':
1516                         qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1517                         break;
1518                 default:
1519                         fprintf(stderr, "error: %s: option '-%c' "
1520                                         "unrecognized\n", argv[0], c);
1521                         return CMD_HELP;
1522                 }
1523         }
1524
1525         if (qctl.qc_type)
1526                 qctl.qc_type--;
1527         else /* by default, invalidate quota for both user & group */
1528                 qctl.qc_type = 0x02;
1529
1530         if (argc == optind)
1531                 return CMD_HELP;
1532
1533         mnt = argv[optind];
1534
1535         rc = llapi_quotactl(mnt, &qctl);
1536         if (rc) {
1537                 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1538                 return rc;
1539         }
1540
1541         return 0;
1542 }
1543
1544 #define ARG2INT(nr, str, msg)                                           \
1545 do {                                                                    \
1546         char *endp;                                                     \
1547         nr = strtol(str, &endp, 0);                                     \
1548         if (*endp) {                                                    \
1549                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
1550                 return CMD_HELP;                                        \
1551         }                                                               \
1552 } while (0)
1553
1554 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1555
1556 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1557  * returns the value or ULONG_MAX on integer overflow or incorrect format
1558  * Notes:
1559  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1560  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1561  *        3. empty integer value is interpreted as 0
1562  */
1563
1564 static unsigned long str2sec(const char* timestr) {
1565         const char spec[] = "smhdw";
1566         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1567         unsigned long val = 0;
1568         char *tail;
1569
1570         if (strpbrk(timestr, spec) == NULL) {
1571                 /* no specifiers inside the time string,
1572                    should treat it as an integer value */
1573                 val = strtoul(timestr, &tail, 10);
1574                 return *tail ? ULONG_MAX : val;
1575         }
1576
1577         /* format string is XXwXXdXXhXXmXXs */
1578         while (*timestr) {
1579                 unsigned long v;
1580                 int ind;
1581                 char* ptr;
1582
1583                 v = strtoul(timestr, &tail, 10);
1584                 if (v == ULONG_MAX || *tail == '\0')
1585                         /* value too large (ULONG_MAX or more)
1586                            or missing specifier */
1587                         goto error;
1588
1589                 ptr = strchr(spec, *tail);
1590                 if (ptr == NULL)
1591                         /* unknown specifier */
1592                         goto error;
1593
1594                 ind = ptr - spec;
1595
1596                 /* check if product will overflow the type */
1597                 if (!(v < ULONG_MAX / mult[ind]))
1598                         goto error;
1599
1600                 ADD_OVERFLOW(val, mult[ind] * v);
1601                 if (val == ULONG_MAX)
1602                         goto error;
1603
1604                 timestr = tail + 1;
1605         }
1606
1607         return val;
1608
1609 error:
1610         return ULONG_MAX;
1611 }
1612
1613 #define ARG2ULL(nr, str, defscale)                                      \
1614 do {                                                                    \
1615         unsigned long long limit, units = 0;                            \
1616         int rc;                                                         \
1617                                                                         \
1618         rc = parse_size(str, &limit, &units, 1);                        \
1619         if (rc < 0) {                                                   \
1620                 fprintf(stderr, "error: bad limit value %s\n", str);    \
1621                 return CMD_HELP;                                        \
1622         }                                                               \
1623         nr = ((units == 0) ? (defscale) : 1) * limit;                   \
1624 } while (0)
1625
1626 static inline int has_times_option(int argc, char **argv)
1627 {
1628         int i;
1629
1630         for (i = 1; i < argc; i++)
1631                 if (!strcmp(argv[i], "-t"))
1632                         return 1;
1633
1634         return 0;
1635 }
1636
1637 int lfs_setquota_times(int argc, char **argv)
1638 {
1639         int c, rc;
1640         struct if_quotactl qctl;
1641         char *mnt, *obd_type = (char *)qctl.obd_type;
1642         struct obd_dqblk *dqb = &qctl.qc_dqblk;
1643         struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1644         struct option long_opts[] = {
1645                 {"user",            no_argument,       0, 'u'},
1646                 {"group",           no_argument,       0, 'g'},
1647                 {"block-grace",     required_argument, 0, 'b'},
1648                 {"inode-grace",     required_argument, 0, 'i'},
1649                 {"times",           no_argument,       0, 't'},
1650                 {0, 0, 0, 0}
1651         };
1652
1653         memset(&qctl, 0, sizeof(qctl));
1654         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
1655         qctl.qc_type = UGQUOTA;
1656
1657 #if 1
1658         /* compatibility syntax: setquota -t -[u|g] t1 t2 mnt */
1659         if (argc == 6 && !strcmp(argv[1], "-t") &&
1660             (!strcmp(argv[2], "-u") || !strcmp(argv[2], "-g")) &&
1661             argv[3][0] != '-' && argv[4][0] != '-') {
1662                 fprintf(stderr, "warning: using compatibility syntax, it may not"
1663                                 " be available in future releases!\n");
1664
1665                 qctl.qc_type = !strcmp(argv[2], "-u") ? USRQUOTA : GRPQUOTA;
1666
1667                 if ((dqi->dqi_bgrace = str2sec(argv[3])) == ULONG_MAX) {
1668                         fprintf(stderr, "error: bad block-grace: %s\n", argv[3]);
1669                         return CMD_HELP;
1670                 }
1671                 if ((dqi->dqi_igrace = str2sec(argv[4])) == ULONG_MAX) {
1672                         fprintf(stderr, "error: bad inode-grace: %s\n", argv[4]);
1673                         return CMD_HELP;
1674                 }
1675                 dqb->dqb_valid = QIF_TIMES;
1676                 mnt = argv[argc - 1];
1677                 goto quotactl;
1678         }
1679 #endif
1680
1681         optind = 0;
1682         while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1683                 switch (c) {
1684                 case 'u':
1685                 case 'g':
1686                         if (qctl.qc_type != UGQUOTA) {
1687                                 fprintf(stderr, "error: -u and -g can't be used "
1688                                                 "more than once\n");
1689                                 return CMD_HELP;
1690                         }
1691                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1692                         break;
1693                 case 'b':
1694                         if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1695                                 fprintf(stderr, "error: bad block-grace: %s\n",
1696                                         optarg);
1697                                 return CMD_HELP;
1698                         }
1699                         dqb->dqb_valid |= QIF_BTIME;
1700                         break;
1701                 case 'i':
1702                         if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1703                                 fprintf(stderr, "error: bad inode-grace: %s\n",
1704                                         optarg);
1705                                 return CMD_HELP;
1706                         }
1707                         dqb->dqb_valid |= QIF_ITIME;
1708                         break;
1709                 case 't': /* Yes, of course! */
1710                         break;
1711                 default: /* getopt prints error message for us when opterr != 0 */
1712                         return CMD_HELP;
1713                 }
1714         }
1715
1716         if (qctl.qc_type == UGQUOTA) {
1717                 fprintf(stderr, "error: neither -u nor -g specified\n");
1718                 return CMD_HELP;
1719         }
1720
1721         if (optind != argc - 1) {
1722                 fprintf(stderr, "error: unexpected parameters encountered\n");
1723                 return CMD_HELP;
1724         }
1725
1726         mnt = argv[optind];
1727
1728 #if 1
1729 quotactl:
1730 #endif
1731         rc = llapi_quotactl(mnt, &qctl);
1732         if (rc) {
1733                 if (*obd_type)
1734                         fprintf(stderr, "%s %s ", obd_type,
1735                                 obd_uuid2str(&qctl.obd_uuid));
1736                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1737                 return rc;
1738         }
1739
1740         return 0;
1741 }
1742
1743 #define BSLIMIT (1 << 0)
1744 #define BHLIMIT (1 << 1)
1745 #define ISLIMIT (1 << 2)
1746 #define IHLIMIT (1 << 3)
1747
1748 int lfs_setquota(int argc, char **argv)
1749 {
1750         int c, rc;
1751         struct if_quotactl qctl;
1752         char *mnt, *obd_type = (char *)qctl.obd_type;
1753         struct obd_dqblk *dqb = &qctl.qc_dqblk;
1754         struct option long_opts[] = {
1755                 {"user",            required_argument, 0, 'u'},
1756                 {"group",           required_argument, 0, 'g'},
1757                 {"block-softlimit", required_argument, 0, 'b'},
1758                 {"block-hardlimit", required_argument, 0, 'B'},
1759                 {"inode-softlimit", required_argument, 0, 'i'},
1760                 {"inode-hardlimit", required_argument, 0, 'I'},
1761                 {0, 0, 0, 0}
1762         };
1763         unsigned limit_mask = 0;
1764         char *endptr;
1765
1766         if (has_times_option(argc, argv))
1767                 return lfs_setquota_times(argc, argv);
1768
1769         memset(&qctl, 0, sizeof(qctl));
1770         qctl.qc_cmd  = LUSTRE_Q_SETQUOTA;
1771         qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1772                                  * so it can be used as a marker that qc_type
1773                                  * isn't reinitialized from command line */
1774
1775 #if 1
1776         /* compatibility syntax: [-u|-g] <user|group> b B i I mount
1777          * will be removed in the future */
1778         if (argc == 8 && (!strcmp(argv[1], "-u") || !strcmp(argv[1], "-g")) &&
1779             argv[3][0] != '-' && argv[4][0] != '-' && argv[5][0] != '-' &&
1780             argv[6][0] != '-') {
1781                 fprintf(stderr, "warning: using compatibility syntax, it may not"
1782                                 " be available in future releases!\n");
1783
1784                 qctl.qc_type = !strcmp(argv[1], "-u") ? USRQUOTA : GRPQUOTA;
1785                 rc = name2id(&qctl.qc_id, argv[2],
1786                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1787                 if (rc) {
1788                         fprintf(stderr, "error: unknown id %s\n", argv[2]);
1789                         return CMD_HELP;
1790                 }
1791
1792                 ARG2ULL(dqb->dqb_bsoftlimit, argv[3], 1024);
1793                 dqb->dqb_bsoftlimit >>= 10;
1794                 ARG2ULL(dqb->dqb_bhardlimit, argv[4], 1024);
1795                 dqb->dqb_bhardlimit >>= 10;
1796                 ARG2ULL(dqb->dqb_isoftlimit, argv[5], 1);
1797                 ARG2ULL(dqb->dqb_ihardlimit, argv[6], 1);
1798
1799                 dqb->dqb_valid = QIF_LIMITS;
1800                 mnt = argv[argc - 1];
1801                 goto quotactl;
1802         }
1803 #endif
1804
1805         optind = 0;
1806         while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1807                 switch (c) {
1808                 case 'u':
1809                 case 'g':
1810                         if (qctl.qc_type != UGQUOTA) {
1811                                 fprintf(stderr, "error: -u and -g can't be used"
1812                                                 " more than once\n");
1813                                 return CMD_HELP;
1814                         }
1815                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1816                         rc = name2id(&qctl.qc_id, optarg,
1817                                      (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1818                         if (rc) {
1819                                 qctl.qc_id = strtoul(optarg, &endptr, 10);
1820                                 if (*endptr != '\0') {
1821                                         fprintf(stderr, "error: can't find id "
1822                                                 "for name %s\n", optarg); 
1823                                         return CMD_HELP;
1824                                 }
1825                         }
1826                         break;
1827                 case 'b':
1828                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1829                         dqb->dqb_bsoftlimit >>= 10;
1830                         limit_mask |= BSLIMIT;
1831                         break;
1832                 case 'B':
1833                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1834                         dqb->dqb_bhardlimit >>= 10;
1835                         limit_mask |= BHLIMIT;
1836                         break;
1837                 case 'i':
1838                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1839                         limit_mask |= ISLIMIT;
1840                         break;
1841                 case 'I':
1842                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1843                         limit_mask |= IHLIMIT;
1844                         break;
1845                 default: /* getopt prints error message for us when opterr != 0 */
1846                         return CMD_HELP;
1847                 }
1848         }
1849
1850         if (qctl.qc_type == UGQUOTA) {
1851                 fprintf(stderr, "error: neither -u nor -g was specified\n");
1852                 return CMD_HELP;
1853         }
1854
1855         if (limit_mask == 0) {
1856                 fprintf(stderr, "error: at least one limit must be specified\n");
1857                 return CMD_HELP;
1858         }
1859
1860         if (optind != argc - 1) {
1861                 fprintf(stderr, "error: unexpected parameters encountered\n");
1862                 return CMD_HELP;
1863         }
1864
1865         mnt = argv[optind];
1866
1867         if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1868             (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1869                 /* sigh, we can't just set blimits/ilimits */
1870                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
1871                                                .qc_type = qctl.qc_type,
1872                                                .qc_id   = qctl.qc_id};
1873
1874                 rc = llapi_quotactl(mnt, &tmp_qctl);
1875                 if (rc < 0) {
1876                         fprintf(stderr, "error: setquota failed while retrieving"
1877                                         " current quota settings (%s)\n",
1878                                         strerror(errno));
1879                         return rc;
1880                 }
1881
1882                 if (!(limit_mask & BHLIMIT))
1883                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1884                 if (!(limit_mask & BSLIMIT))
1885                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1886                 if (!(limit_mask & IHLIMIT))
1887                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1888                 if (!(limit_mask & ISLIMIT))
1889                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1890
1891                 /* Keep grace times if we have got no softlimit arguments */
1892                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
1893                         dqb->dqb_valid |= QIF_BTIME;
1894                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
1895                 }
1896
1897                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
1898                         dqb->dqb_valid |= QIF_ITIME;
1899                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
1900                 }
1901         }
1902
1903         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1904         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1905
1906 #if 1
1907 quotactl:
1908 #endif
1909         rc = llapi_quotactl(mnt, &qctl);
1910         if (rc) {
1911                 if (*obd_type)
1912                         fprintf(stderr, "%s %s ", obd_type,
1913                                 obd_uuid2str(&qctl.obd_uuid));
1914                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1915                 return rc;
1916         }
1917
1918         return 0;
1919 }
1920
1921 static inline char *type2name(int check_type)
1922 {
1923         if (check_type == USRQUOTA)
1924                 return "user";
1925         else if (check_type == GRPQUOTA)
1926                 return "group";
1927         else
1928                 return "unknown";
1929 }
1930
1931
1932 /* Converts seconds value into format string
1933  * result is returned in buf
1934  * Notes:
1935  *        1. result is in descenting order: 1w2d3h4m5s
1936  *        2. zero fields are not filled (except for p. 3): 5d1s
1937  *        3. zero seconds value is presented as "0s"
1938  */
1939 static void sec2str(time_t seconds, char *buf)
1940 {
1941         const char spec[] = "smhdw";
1942         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1943         unsigned long c;
1944         char* tail = buf;
1945         int i;
1946
1947         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1948                 c = seconds / mult[i];
1949
1950                 if (c > 0 || (i == 0 && buf == tail))
1951                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1952
1953                 seconds %= mult[i];
1954         }
1955 }
1956
1957
1958 static void diff2str(time_t seconds, char *buf, time_t now)
1959 {
1960
1961         buf[0] = 0;
1962         if (!seconds)
1963                 return;
1964         if (seconds <= now) {
1965                 strcpy(buf, "none");
1966                 return;
1967         }
1968         sec2str(seconds - now, buf);
1969 }
1970
1971 static void print_quota_title(char *name, struct if_quotactl *qctl)
1972 {
1973         printf("Disk quotas for %s %s (%cid %u):\n",
1974                type2name(qctl->qc_type), name,
1975                *type2name(qctl->qc_type), qctl->qc_id);
1976         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1977                "Filesystem",
1978                "kbytes", "quota", "limit", "grace",
1979                "files", "quota", "limit", "grace");
1980 }
1981
1982 #define GENERAL_QUOTA_INFO 1
1983 #define MDS_QUOTA_INFO     2
1984 #define OST_QUOTA_INFO     3
1985
1986 static void print_quota(char *mnt, struct if_quotactl *qctl, int type)
1987 {
1988         time_t now;
1989
1990         time(&now);
1991
1992         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1993                 int bover = 0, iover = 0;
1994                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1995
1996                 if (dqb->dqb_bhardlimit &&
1997                     toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1998                         bover = 1;
1999                 } else if (dqb->dqb_bsoftlimit &&
2000                            toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
2001                         if (dqb->dqb_btime > now) {
2002                                 bover = 2;
2003                         } else {
2004                                 bover = 3;
2005                         }
2006                 }
2007
2008                 if (dqb->dqb_ihardlimit &&
2009                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2010                         iover = 1;
2011                 } else if (dqb->dqb_isoftlimit &&
2012                            dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
2013                         if (dqb->dqb_btime > now) {
2014                                 iover = 2;
2015                         } else {
2016                                 iover = 3;
2017                         }
2018                 }
2019
2020 #if 0           /* XXX: always print quotas even when no usages */
2021                 if (dqb->dqb_curspace || dqb->dqb_curinodes)
2022 #endif
2023                 {
2024                         char numbuf[3][32];
2025                         char timebuf[40];
2026
2027                         if (strlen(mnt) > 15)
2028                                 printf("%s\n%15s", mnt, "");
2029                         else
2030                                 printf("%15s", mnt);
2031
2032                         if (bover)
2033                                 diff2str(dqb->dqb_btime, timebuf, now);
2034                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2035                                 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
2036                         if (type == GENERAL_QUOTA_INFO)
2037                                 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2038                                         ? LPU64 : "["LPU64"]",
2039                                         dqb->dqb_bsoftlimit);
2040                         else
2041                                 sprintf(numbuf[1], "%s", "");
2042                         sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2043                                 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2044                         printf(" %7s%c %6s %7s %7s",
2045                                numbuf[0], bover ? '*' : ' ', numbuf[1],
2046                                numbuf[2], bover > 1 ? timebuf : "");
2047
2048                         if (iover)
2049                                 diff2str(dqb->dqb_itime, timebuf, now);
2050
2051                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2052                                 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2053                        if (type == GENERAL_QUOTA_INFO)
2054                                 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2055                                         ? LPU64 : "["LPU64"]",
2056                                         dqb->dqb_isoftlimit);
2057                         else
2058                                 sprintf(numbuf[1], "%s", "");
2059                         sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2060                                 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2061                         if (type != OST_QUOTA_INFO)
2062                                 printf(" %7s%c %6s %7s %7s",
2063                                        numbuf[0], iover ? '*' : ' ', numbuf[1],
2064                                        numbuf[2], iover > 1 ? timebuf : "");
2065                         printf("\n");
2066                 }
2067         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2068                    qctl->qc_cmd == Q_GETOINFO) {
2069                 char bgtimebuf[40];
2070                 char igtimebuf[40];
2071
2072                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2073                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2074                 printf("Block grace time: %s; Inode grace time: %s\n",
2075                        bgtimebuf, igtimebuf);
2076         }
2077 }
2078
2079 static int print_mds_quota(char *mnt, struct if_quotactl *qctl)
2080 {
2081         int rc;
2082
2083         /* XXX: this is a flag to mark that only mds quota is wanted */
2084         qctl->qc_dqblk.dqb_valid = 1;
2085         rc = llapi_quotactl(mnt, qctl);
2086         if (rc) {
2087                 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
2088                 return rc;
2089         }
2090
2091         print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, MDS_QUOTA_INFO);
2092         return 0;
2093 }
2094
2095 static int print_lov_quota(char *mnt, struct if_quotactl *qctl)
2096 {
2097         DIR *dir;
2098         struct obd_uuid *uuids = NULL, *uuidp;
2099         int obdcount = 1024;
2100         int i, rc = 0, rc1 = 0;
2101
2102         dir = opendir(mnt);
2103         if (!dir) {
2104                 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
2105                 return -ENOENT;
2106         }
2107
2108         uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
2109                                           sizeof(struct obd_uuid));
2110         if (uuids == NULL)
2111                 goto out;
2112
2113 retry_get_uuids:
2114         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
2115         if (rc != 0) {
2116                 struct obd_uuid *uuids_temp;
2117
2118                 if (rc == -EOVERFLOW) {
2119                         uuids_temp = realloc(uuids, obdcount *
2120                                              sizeof(struct obd_uuid));
2121                         if (uuids_temp != NULL)
2122                                 goto retry_get_uuids;
2123                         else
2124                                 rc = -ENOMEM;
2125                 }
2126
2127                 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
2128                 goto out;
2129         }
2130
2131         for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
2132                 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
2133
2134                 /* XXX clear this flag to get quota from osts */
2135                 qctl->qc_dqblk.dqb_valid = 0;
2136                 rc = llapi_quotactl(mnt, qctl);
2137                 if (rc) {
2138                         if (!rc1)
2139                                 rc1 = rc;
2140                         fprintf(stderr, "%s quotactl failed: %s\n",
2141                                 uuidp->uuid, strerror(errno));
2142                         continue;
2143                 }
2144
2145                 print_quota((char *)uuidp->uuid, qctl, OST_QUOTA_INFO);
2146         }
2147
2148 out:
2149         closedir(dir);
2150         if (!rc)
2151                 rc = rc1;
2152         return rc;
2153 }
2154
2155 static int lfs_quota(int argc, char **argv)
2156 {
2157         int c;
2158         char *mnt, *name = NULL;
2159         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2160                                     .qc_type = UGQUOTA };
2161         char *obd_type = (char *)qctl.obd_type;
2162         char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2163         int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0;
2164         int pass = 0;
2165         char *endptr;
2166
2167         optind = 0;
2168         while ((c = getopt(argc, argv, "ugto:v")) != -1) {
2169                 switch (c) {
2170                 case 'u':
2171                         if (qctl.qc_type != UGQUOTA) {
2172                                 fprintf(stderr, "error: use either -u or -g\n");
2173                                 return CMD_HELP;
2174                         }
2175                         qctl.qc_type = USRQUOTA;
2176                         break;
2177                 case 'g':
2178                         if (qctl.qc_type != UGQUOTA) {
2179                                 fprintf(stderr, "error: use either -u or -g\n");
2180                                 return CMD_HELP;
2181                         }
2182                         qctl.qc_type = GRPQUOTA;
2183                         break;
2184                 case 't':
2185                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
2186                         break;
2187                 case 'o':
2188                         strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2189                         break;
2190                 case 'v':
2191                         verbose = 1;
2192                         break;
2193                 default:
2194                         fprintf(stderr, "error: %s: option '-%c' "
2195                                         "unrecognized\n", argv[0], c);
2196                         return CMD_HELP;
2197                 }
2198         }
2199
2200         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2201         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2202             optind == argc - 1) {
2203 ug_output:
2204                 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2205                 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2206                 if (pass++ == 0) {
2207                         qctl.qc_type = USRQUOTA;
2208                         qctl.qc_id = geteuid();
2209                 } else {
2210                         qctl.qc_type = GRPQUOTA;
2211                         qctl.qc_id = getegid();
2212                 }
2213                 rc = id2name(&name, qctl.qc_id,
2214                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2215                 if (rc)
2216                         name = "<unknown>";
2217         /* lfs quota -u username /path/to/lustre/mount */
2218         } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2219                 /* options should be followed by u/g-name and mntpoint */
2220                 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2221                         fprintf(stderr, "error: missing quota argument(s)\n");
2222                         return CMD_HELP;
2223                 }
2224
2225                 name = argv[optind++];
2226                 rc = name2id(&qctl.qc_id, name,
2227                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2228                 if (rc) {
2229                         qctl.qc_id = strtoul(name, &endptr, 10);
2230                         if (*endptr != '\0') {
2231                                 fprintf(stderr, "error: can't find id for name "
2232                                         "%s\n", name);
2233                                 return CMD_HELP;
2234                         }
2235                 }
2236         } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2237                 fprintf(stderr, "error: missing quota info argument(s)\n");
2238                 return CMD_HELP;
2239         }
2240
2241         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2242                 print_quota_title(name, &qctl);
2243
2244         mnt = argv[optind];
2245
2246         rc1 = llapi_quotactl(mnt, &qctl);
2247         if (rc1 == -1 && errno == ESRCH) {
2248                 fprintf(stderr, "\n%s quotas are not enabled.\n",
2249                         qctl.qc_type == USRQUOTA ? "user" : "group");
2250                 goto out;
2251         }
2252         if (rc1 && *obd_type)
2253                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2254
2255         if (*obd_uuid)
2256                 mnt = "";
2257
2258         print_quota(mnt, &qctl, GENERAL_QUOTA_INFO);
2259
2260         if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2261                 rc2 = print_mds_quota(mnt, &qctl);
2262                 rc3 = print_lov_quota(mnt, &qctl);
2263         }
2264
2265         if (rc1 || rc2 || rc3)
2266                 printf("Some errors happened when getting quota info. "
2267                        "Some devices may be not working or deactivated. "
2268                        "The data in \"[]\" is inaccurate.\n");
2269
2270 out:
2271         if (pass == 1)
2272                 goto ug_output;
2273
2274         return 0;
2275 }
2276 #endif /* HAVE_SYS_QUOTA_H! */
2277
2278 int main(int argc, char **argv)
2279 {
2280         int rc;
2281
2282         setlinebuf(stdout);
2283
2284         ptl_initialize(argc, argv);
2285         if (obd_initialize(argc, argv) < 0)
2286                 exit(2);
2287         if (dbg_initialize(argc, argv) < 0)
2288                 exit(3);
2289
2290         Parser_init("lfs > ", cmdlist);
2291
2292         if (argc > 1) {
2293                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2294         } else {
2295                 rc = Parser_commands();
2296         }
2297
2298         obd_finalize(argc, argv);
2299         return rc < 0 ? -rc : rc;
2300 }