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