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