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