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