Whamcloud - gitweb
Branch HEAD
[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 && errno != EALREADY) {
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 && errno != EALREADY) {
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 (errno == EALREADY) {
1481                         fprintf(stderr, "\n%s quotas are enabled already.\n",
1482                                 qctl.qc_type == 0x02 ? "user/group" :
1483                                 (qctl.qc_type == 0x00 ? "user" : "group"));
1484                         rc = 0;
1485                 } else {
1486                         if (*obd_type)
1487                                 fprintf(stderr, "%s %s ", obd_type,
1488                                         obd_uuid2str(&qctl.obd_uuid));
1489                         fprintf(stderr, "%s failed: %s\n", argv[0],
1490                                 strerror(errno));
1491                 }
1492         }
1493
1494         return rc;
1495 }
1496
1497 static int lfs_quotaoff(int argc, char **argv)
1498 {
1499         int c;
1500         char *mnt;
1501         struct if_quotactl qctl;
1502         char *obd_type = (char *)qctl.obd_type;
1503         int rc;
1504
1505         memset(&qctl, 0, sizeof(qctl));
1506         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1507
1508         optind = 0;
1509         while ((c = getopt(argc, argv, "ug")) != -1) {
1510                 switch (c) {
1511                 case 'u':
1512                         qctl.qc_type |= 0x01;
1513                         break;
1514                 case 'g':
1515                         qctl.qc_type |= 0x02;
1516                         break;
1517                 default:
1518                         fprintf(stderr, "error: %s: option '-%c' "
1519                                         "unrecognized\n", argv[0], c);
1520                         return CMD_HELP;
1521                 }
1522         }
1523
1524         if (qctl.qc_type)
1525                 qctl.qc_type--;
1526         else /* by default, disable quota for both user & group */
1527                 qctl.qc_type = 0x02;
1528
1529         if (argc == optind)
1530                 return CMD_HELP;
1531
1532         mnt = argv[optind];
1533
1534         rc = llapi_quotactl(mnt, &qctl);
1535         if (rc) {
1536                 if (errno == EALREADY) {
1537                         fprintf(stderr, "\n%s quotas are disabled already.\n",
1538                                 qctl.qc_type == 0x02 ? "user/group" :
1539                                 (qctl.qc_type == 0x00 ? "user" : "group"));
1540                         rc = 0;
1541                 } else {
1542                         if (*obd_type)
1543                                 fprintf(stderr, "%s %s ", obd_type,
1544                                         obd_uuid2str(&qctl.obd_uuid));
1545                         fprintf(stderr, "quotaoff failed: %s\n",
1546                                 strerror(errno));
1547                 }
1548         }
1549
1550         return rc;
1551 }
1552
1553 static int lfs_quotainv(int argc, char **argv)
1554 {
1555         int c;
1556         char *mnt;
1557         struct if_quotactl qctl;
1558         int rc;
1559
1560         memset(&qctl, 0, sizeof(qctl));
1561         qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1562
1563         optind = 0;
1564         while ((c = getopt(argc, argv, "ugf")) != -1) {
1565                 switch (c) {
1566                 case 'u':
1567                         qctl.qc_type |= 0x01;
1568                         break;
1569                 case 'g':
1570                         qctl.qc_type |= 0x02;
1571                         break;
1572                 case 'f':
1573                         qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1574                         break;
1575                 default:
1576                         fprintf(stderr, "error: %s: option '-%c' "
1577                                         "unrecognized\n", argv[0], c);
1578                         return CMD_HELP;
1579                 }
1580         }
1581
1582         if (qctl.qc_type)
1583                 qctl.qc_type--;
1584         else /* by default, invalidate quota for both user & group */
1585                 qctl.qc_type = 0x02;
1586
1587         if (argc == optind)
1588                 return CMD_HELP;
1589
1590         mnt = argv[optind];
1591
1592         rc = llapi_quotactl(mnt, &qctl);
1593         if (rc) {
1594                 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1595                 return rc;
1596         }
1597
1598         return 0;
1599 }
1600
1601 #define ARG2INT(nr, str, msg)                                           \
1602 do {                                                                    \
1603         char *endp;                                                     \
1604         nr = strtol(str, &endp, 0);                                     \
1605         if (*endp) {                                                    \
1606                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
1607                 return CMD_HELP;                                        \
1608         }                                                               \
1609 } while (0)
1610
1611 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1612
1613 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1614  * returns the value or ULONG_MAX on integer overflow or incorrect format
1615  * Notes:
1616  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1617  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1618  *        3. empty integer value is interpreted as 0
1619  */
1620
1621 static unsigned long str2sec(const char* timestr) {
1622         const char spec[] = "smhdw";
1623         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1624         unsigned long val = 0;
1625         char *tail;
1626
1627         if (strpbrk(timestr, spec) == NULL) {
1628                 /* no specifiers inside the time string,
1629                    should treat it as an integer value */
1630                 val = strtoul(timestr, &tail, 10);
1631                 return *tail ? ULONG_MAX : val;
1632         }
1633
1634         /* format string is XXwXXdXXhXXmXXs */
1635         while (*timestr) {
1636                 unsigned long v;
1637                 int ind;
1638                 char* ptr;
1639
1640                 v = strtoul(timestr, &tail, 10);
1641                 if (v == ULONG_MAX || *tail == '\0')
1642                         /* value too large (ULONG_MAX or more)
1643                            or missing specifier */
1644                         goto error;
1645
1646                 ptr = strchr(spec, *tail);
1647                 if (ptr == NULL)
1648                         /* unknown specifier */
1649                         goto error;
1650
1651                 ind = ptr - spec;
1652
1653                 /* check if product will overflow the type */
1654                 if (!(v < ULONG_MAX / mult[ind]))
1655                         goto error;
1656
1657                 ADD_OVERFLOW(val, mult[ind] * v);
1658                 if (val == ULONG_MAX)
1659                         goto error;
1660
1661                 timestr = tail + 1;
1662         }
1663
1664         return val;
1665
1666 error:
1667         return ULONG_MAX;
1668 }
1669
1670 #define ARG2ULL(nr, str, defscale)                                      \
1671 do {                                                                    \
1672         unsigned long long limit, units = 0;                            \
1673         int rc;                                                         \
1674                                                                         \
1675         rc = parse_size(str, &limit, &units, 1);                        \
1676         if (rc < 0) {                                                   \
1677                 fprintf(stderr, "error: bad limit value %s\n", str);    \
1678                 return CMD_HELP;                                        \
1679         }                                                               \
1680         nr = ((units == 0) ? (defscale) : 1) * limit;                   \
1681 } while (0)
1682
1683 static inline int has_times_option(int argc, char **argv)
1684 {
1685         int i;
1686
1687         for (i = 1; i < argc; i++)
1688                 if (!strcmp(argv[i], "-t"))
1689                         return 1;
1690
1691         return 0;
1692 }
1693
1694 int lfs_setquota_times(int argc, char **argv)
1695 {
1696         int c, rc;
1697         struct if_quotactl qctl;
1698         char *mnt, *obd_type = (char *)qctl.obd_type;
1699         struct obd_dqblk *dqb = &qctl.qc_dqblk;
1700         struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1701         struct option long_opts[] = {
1702                 {"user",            no_argument,       0, 'u'},
1703                 {"group",           no_argument,       0, 'g'},
1704                 {"block-grace",     required_argument, 0, 'b'},
1705                 {"inode-grace",     required_argument, 0, 'i'},
1706                 {"times",           no_argument,       0, 't'},
1707                 {0, 0, 0, 0}
1708         };
1709
1710         memset(&qctl, 0, sizeof(qctl));
1711         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
1712         qctl.qc_type = UGQUOTA;
1713
1714         optind = 0;
1715         while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1716                 switch (c) {
1717                 case 'u':
1718                 case 'g':
1719                         if (qctl.qc_type != UGQUOTA) {
1720                                 fprintf(stderr, "error: -u and -g can't be used "
1721                                                 "more than once\n");
1722                                 return CMD_HELP;
1723                         }
1724                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1725                         break;
1726                 case 'b':
1727                         if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1728                                 fprintf(stderr, "error: bad block-grace: %s\n",
1729                                         optarg);
1730                                 return CMD_HELP;
1731                         }
1732                         dqb->dqb_valid |= QIF_BTIME;
1733                         break;
1734                 case 'i':
1735                         if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1736                                 fprintf(stderr, "error: bad inode-grace: %s\n",
1737                                         optarg);
1738                                 return CMD_HELP;
1739                         }
1740                         dqb->dqb_valid |= QIF_ITIME;
1741                         break;
1742                 case 't': /* Yes, of course! */
1743                         break;
1744                 default: /* getopt prints error message for us when opterr != 0 */
1745                         return CMD_HELP;
1746                 }
1747         }
1748
1749         if (qctl.qc_type == UGQUOTA) {
1750                 fprintf(stderr, "error: neither -u nor -g specified\n");
1751                 return CMD_HELP;
1752         }
1753
1754         if (optind != argc - 1) {
1755                 fprintf(stderr, "error: unexpected parameters encountered\n");
1756                 return CMD_HELP;
1757         }
1758
1759         mnt = argv[optind];
1760         rc = llapi_quotactl(mnt, &qctl);
1761         if (rc) {
1762                 if (*obd_type)
1763                         fprintf(stderr, "%s %s ", obd_type,
1764                                 obd_uuid2str(&qctl.obd_uuid));
1765                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1766                 return rc;
1767         }
1768
1769         return 0;
1770 }
1771
1772 #define BSLIMIT (1 << 0)
1773 #define BHLIMIT (1 << 1)
1774 #define ISLIMIT (1 << 2)
1775 #define IHLIMIT (1 << 3)
1776
1777 int lfs_setquota(int argc, char **argv)
1778 {
1779         int c, rc;
1780         struct if_quotactl qctl;
1781         char *mnt, *obd_type = (char *)qctl.obd_type;
1782         struct obd_dqblk *dqb = &qctl.qc_dqblk;
1783         struct option long_opts[] = {
1784                 {"user",            required_argument, 0, 'u'},
1785                 {"group",           required_argument, 0, 'g'},
1786                 {"block-softlimit", required_argument, 0, 'b'},
1787                 {"block-hardlimit", required_argument, 0, 'B'},
1788                 {"inode-softlimit", required_argument, 0, 'i'},
1789                 {"inode-hardlimit", required_argument, 0, 'I'},
1790                 {0, 0, 0, 0}
1791         };
1792         unsigned limit_mask = 0;
1793
1794         if (has_times_option(argc, argv))
1795                 return lfs_setquota_times(argc, argv);
1796
1797         memset(&qctl, 0, sizeof(qctl));
1798         qctl.qc_cmd  = LUSTRE_Q_SETQUOTA;
1799         qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1800                                  * so it can be used as a marker that qc_type
1801                                  * isn't reinitialized from command line */
1802
1803         optind = 0;
1804         while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1805                 switch (c) {
1806                 case 'u':
1807                 case 'g':
1808                         if (qctl.qc_type != UGQUOTA) {
1809                                 fprintf(stderr, "error: -u and -g can't be used"
1810                                                 " more than once\n");
1811                                 return CMD_HELP;
1812                         }
1813                         qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1814                         rc = name2id(&qctl.qc_id, optarg,
1815                                      (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1816                         if (rc) {
1817                                 fprintf(stderr, "error: unknown id %s\n",
1818                                         optarg);
1819                                 return CMD_HELP;
1820                         }
1821                         break;
1822                 case 'b':
1823                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1824                         dqb->dqb_bsoftlimit >>= 10;
1825                         limit_mask |= BSLIMIT;
1826                         break;
1827                 case 'B':
1828                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1829                         dqb->dqb_bhardlimit >>= 10;
1830                         limit_mask |= BHLIMIT;
1831                         break;
1832                 case 'i':
1833                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1834                         limit_mask |= ISLIMIT;
1835                         break;
1836                 case 'I':
1837                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1838                         limit_mask |= IHLIMIT;
1839                         break;
1840                 default: /* getopt prints error message for us when opterr != 0 */
1841                         return CMD_HELP;
1842                 }
1843         }
1844
1845         if (qctl.qc_type == UGQUOTA) {
1846                 fprintf(stderr, "error: neither -u nor -g are specified\n");
1847                 return CMD_HELP;
1848         }
1849
1850         if (limit_mask == 0) {
1851                 fprintf(stderr, "error: at least one limit must be specified\n");
1852                 return CMD_HELP;
1853         }
1854
1855         if (optind != argc - 1) {
1856                 fprintf(stderr, "error: unexpected parameters encountered\n");
1857                 return CMD_HELP;
1858         }
1859
1860         mnt = argv[optind];
1861
1862         if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1863             (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1864                 /* sigh, we can't just set blimits/ilimits */
1865                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
1866                                                .qc_type = qctl.qc_type,
1867                                                .qc_id   = qctl.qc_id};
1868
1869                 rc = llapi_quotactl(mnt, &tmp_qctl);
1870                 if (rc < 0) {
1871                         fprintf(stderr, "error: getquota failed\n");
1872                         return CMD_HELP;
1873                 }
1874
1875                 if (!(limit_mask & BHLIMIT))
1876                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1877                 if (!(limit_mask & BSLIMIT))
1878                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1879                 if (!(limit_mask & IHLIMIT))
1880                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1881                 if (!(limit_mask & ISLIMIT))
1882                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1883         }
1884
1885         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1886         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1887
1888         rc = llapi_quotactl(mnt, &qctl);
1889         if (rc) {
1890                 if (*obd_type)
1891                         fprintf(stderr, "%s %s ", obd_type,
1892                                 obd_uuid2str(&qctl.obd_uuid));
1893                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1894                 return rc;
1895         }
1896
1897         return 0;
1898 }
1899
1900 static inline char *type2name(int check_type)
1901 {
1902         if (check_type == USRQUOTA)
1903                 return "user";
1904         else if (check_type == GRPQUOTA)
1905                 return "group";
1906         else
1907                 return "unknown";
1908 }
1909
1910
1911 /* Converts seconds value into format string
1912  * result is returned in buf
1913  * Notes:
1914  *        1. result is in descenting order: 1w2d3h4m5s
1915  *        2. zero fields are not filled (except for p. 3): 5d1s
1916  *        3. zero seconds value is presented as "0s"
1917  */
1918 static void sec2str(time_t seconds, char *buf)
1919 {
1920         const char spec[] = "smhdw";
1921         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1922         unsigned long c;
1923         char* tail = buf;
1924         int i;
1925
1926         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1927                 c = seconds / mult[i];
1928
1929                 if (c > 0 || (i == 0 && buf == tail))
1930                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1931
1932                 seconds %= mult[i];
1933         }
1934 }
1935
1936
1937 static void diff2str(time_t seconds, char *buf, time_t now)
1938 {
1939
1940         buf[0] = 0;
1941         if (!seconds)
1942                 return;
1943         if (seconds <= now) {
1944                 strcpy(buf, "none");
1945                 return;
1946         }
1947         sec2str(seconds - now, buf);
1948 }
1949
1950 static void print_quota_title(char *name, struct if_quotactl *qctl)
1951 {
1952         printf("Disk quotas for %s %s (%cid %u):\n",
1953                type2name(qctl->qc_type), name,
1954                *type2name(qctl->qc_type), qctl->qc_id);
1955         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1956                "Filesystem",
1957                "kbytes", "quota", "limit", "grace",
1958                "files", "quota", "limit", "grace");
1959 }
1960
1961 static void print_quota(char *mnt, struct if_quotactl *qctl, int type, int rc)
1962 {
1963         time_t now;
1964
1965         time(&now);
1966
1967         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1968                 int bover = 0, iover = 0;
1969                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1970
1971                 if (dqb->dqb_bhardlimit &&
1972                     toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1973                         bover = 1;
1974                 } else if (dqb->dqb_bsoftlimit &&
1975                            toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1976                         if (dqb->dqb_btime > now) {
1977                                 bover = 2;
1978                         } else {
1979                                 bover = 3;
1980                         }
1981                 }
1982
1983                 if (dqb->dqb_ihardlimit &&
1984                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1985                         iover = 1;
1986                 } else if (dqb->dqb_isoftlimit &&
1987                            dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1988                         if (dqb->dqb_btime > now) {
1989                                 iover = 2;
1990                         } else {
1991                                 iover = 3;
1992                         }
1993                 }
1994
1995 #if 0           /* XXX: always print quotas even when no usages */
1996                 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1997 #endif
1998                 {
1999                         char numbuf[3][32];
2000                         char timebuf[40];
2001
2002                         if (strlen(mnt) > 15)
2003                                 printf("%s\n%15s", mnt, "");
2004                         else
2005                                 printf("%15s", mnt);
2006
2007                         if (bover)
2008                                 diff2str(dqb->dqb_btime, timebuf, now);
2009                         if (rc == -1 && errno == EREMOTEIO)
2010                                 sprintf(numbuf[0], LPU64"*",
2011                                         toqb(dqb->dqb_curspace));
2012                         else
2013                                 sprintf(numbuf[0],
2014                                         (dqb->dqb_valid & QIF_SPACE) ?
2015                                         LPU64 : "["LPU64"]",
2016                                         toqb(dqb->dqb_curspace));
2017                         if (type == QC_GENERAL)
2018                                 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2019                                         ? LPU64 : "["LPU64"]",
2020                                         dqb->dqb_bsoftlimit);
2021                         else
2022                                 sprintf(numbuf[1], "%s", "");
2023                         sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2024                                 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2025                         printf(" %7s%c %6s %7s %7s",
2026                                numbuf[0], bover ? '*' : ' ', numbuf[1],
2027                                numbuf[2], bover > 1 ? timebuf : "");
2028
2029                         if (iover)
2030                                 diff2str(dqb->dqb_itime, timebuf, now);
2031
2032                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2033                                 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2034                        if (type == QC_GENERAL)
2035                                 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2036                                         ? LPU64 : "["LPU64"]",
2037                                         dqb->dqb_isoftlimit);
2038                         else
2039                                 sprintf(numbuf[1], "%s", "");
2040                         sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2041                                 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2042                         if (type != QC_OSTIDX)
2043                                 printf(" %7s%c %6s %7s %7s",
2044                                        numbuf[0], iover ? '*' : ' ', numbuf[1],
2045                                        numbuf[2], iover > 1 ? timebuf : "");
2046                         printf("\n");
2047                 }
2048         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2049                    qctl->qc_cmd == Q_GETOINFO) {
2050                 char bgtimebuf[40];
2051                 char igtimebuf[40];
2052
2053                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2054                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2055                 printf("Block grace time: %s; Inode grace time: %s\n",
2056                        bgtimebuf, igtimebuf);
2057         }
2058 }
2059
2060 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2061 {
2062         int rc = 0, rc1 = 0, count = 0;
2063         __u32 valid = qctl->qc_valid;
2064
2065         rc = llapi_get_obd_count(mnt, &count, is_mdt);
2066         if (rc) {
2067                 fprintf(stderr, "can not get %s count: %s\n",
2068                         is_mdt ? "mdt": "ost", strerror(errno));
2069                 return rc;
2070         }
2071
2072         for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2073                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2074                 rc = llapi_quotactl(mnt, qctl);
2075                 if (rc) {
2076                         /* It is remote client case. */
2077                         if (errno == EOPNOTSUPP) {
2078                                 rc = 0;
2079                                 goto out;
2080                         }
2081
2082                         if (!rc1)
2083                                 rc1 = rc;
2084                         fprintf(stderr, "quotactl %s%d failed.\n",
2085                                 is_mdt ? "mdt": "ost", qctl->qc_idx);
2086                         continue;
2087                 }
2088
2089                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid, 0);
2090         }
2091
2092 out:
2093         qctl->qc_valid = valid;
2094         return rc ? : rc1;
2095 }
2096
2097 static int lfs_quota(int argc, char **argv)
2098 {
2099         int c;
2100         char *mnt, *name = NULL;
2101         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2102                                     .qc_type = UGQUOTA };
2103         char *obd_type = (char *)qctl.obd_type;
2104         char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2105         int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2106         __u32 valid = QC_GENERAL, idx = 0;
2107
2108         optind = 0;
2109         while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2110                 switch (c) {
2111                 case 'u':
2112                         if (qctl.qc_type != UGQUOTA) {
2113                                 fprintf(stderr, "error: use either -u or -g\n");
2114                                 return CMD_HELP;
2115                         }
2116                         qctl.qc_type = USRQUOTA;
2117                         break;
2118                 case 'g':
2119                         if (qctl.qc_type != UGQUOTA) {
2120                                 fprintf(stderr, "error: use either -u or -g\n");
2121                                 return CMD_HELP;
2122                         }
2123                         qctl.qc_type = GRPQUOTA;
2124                         break;
2125                 case 't':
2126                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
2127                         break;
2128                 case 'o':
2129                         valid = qctl.qc_valid = QC_UUID;
2130                         strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2131                         break;
2132                 case 'i':
2133                         valid = qctl.qc_valid = QC_MDTIDX;
2134                         idx = qctl.qc_idx = atoi(optarg);
2135                         break;
2136                 case 'I':
2137                         valid = qctl.qc_valid = QC_OSTIDX;
2138                         idx = qctl.qc_idx = atoi(optarg);
2139                         break;
2140                 case 'v':
2141                         verbose = 1;
2142                         break;
2143                 default:
2144                         fprintf(stderr, "error: %s: option '-%c' "
2145                                         "unrecognized\n", argv[0], c);
2146                         return CMD_HELP;
2147                 }
2148         }
2149
2150         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2151         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2152             optind == argc - 1) {
2153 ug_output:
2154                 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2155                 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2156                 qctl.qc_valid = valid;
2157                 qctl.qc_idx = idx;
2158                 if (pass++ == 0) {
2159                         qctl.qc_type = USRQUOTA;
2160                         qctl.qc_id = geteuid();
2161                 } else {
2162                         qctl.qc_type = GRPQUOTA;
2163                         qctl.qc_id = getegid();
2164                 }
2165                 rc = id2name(&name, qctl.qc_id,
2166                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2167                 if (rc)
2168                         name = "<unknown>";
2169         /* lfs quota -u username /path/to/lustre/mount */
2170         } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2171                 /* options should be followed by u/g-name and mntpoint */
2172                 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2173                         fprintf(stderr, "error: missing quota argument(s)\n");
2174                         return CMD_HELP;
2175                 }
2176
2177                 name = argv[optind++];
2178                 rc = name2id(&qctl.qc_id, name,
2179                              (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2180                 if (rc) {
2181                         fprintf(stderr,"error: can't find id for name %s: %s\n",
2182                                 name, strerror(errno));
2183                         return CMD_HELP;
2184                 }
2185         } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2186                 fprintf(stderr, "error: missing quota info argument(s)\n");
2187                 return CMD_HELP;
2188         }
2189
2190         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2191                 print_quota_title(name, &qctl);
2192
2193         mnt = argv[optind];
2194
2195         rc1 = llapi_quotactl(mnt, &qctl);
2196         if (rc1 == -1 && errno == EALREADY) {
2197                 fprintf(stderr, "\n%s quotas are not enabled.\n",
2198                         qctl.qc_type == USRQUOTA ? "user" : "group");
2199                 goto out;
2200         }
2201         if (rc1 && *obd_type)
2202                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2203
2204         if (qctl.qc_valid != QC_GENERAL)
2205                 mnt = "";
2206
2207         print_quota(mnt, &qctl, QC_GENERAL, rc1);
2208
2209         if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2210                 rc2 = print_obd_quota(mnt, &qctl, 1);
2211                 rc3 = print_obd_quota(mnt, &qctl, 0);
2212         }
2213
2214         if (rc1 || rc2 || rc3)
2215                 printf("Some errors happened when getting quota info. "
2216                        "Some devices may be not working or deactivated. "
2217                        "The data in \"[]\" is inaccurate.\n");
2218
2219 out:
2220         if (pass == 1)
2221                 goto ug_output;
2222
2223         return 0;
2224 }
2225 #endif /* HAVE_SYS_QUOTA_H! */
2226
2227 static int flushctx_ioctl(char *mp)
2228 {
2229         int fd, rc;
2230
2231         fd = open(mp, O_RDONLY);
2232         if (fd == -1) {
2233                 fprintf(stderr, "flushctx: error open %s: %s\n",
2234                         mp, strerror(errno));
2235                 return -1;
2236         }
2237
2238         rc = ioctl(fd, LL_IOC_FLUSHCTX);
2239         if (rc == -1)
2240                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2241                         mp, strerror(errno));
2242
2243         close(fd);
2244         return rc;
2245 }
2246
2247 static int lfs_flushctx(int argc, char **argv)
2248 {
2249         int     kdestroy = 0, c;
2250         FILE   *proc;
2251         char    procline[PATH_MAX], *line;
2252         int     rc = 0;
2253
2254         optind = 0;
2255         while ((c = getopt(argc, argv, "k")) != -1) {
2256                 switch (c) {
2257                 case 'k':
2258                         kdestroy = 1;
2259                         break;
2260                 default:
2261                         fprintf(stderr, "error: %s: option '-%c' "
2262                                         "unrecognized\n", argv[0], c);
2263                         return CMD_HELP;
2264                 }
2265         }
2266
2267         if (kdestroy)
2268                 system("kdestroy > /dev/null");
2269
2270         if (optind >= argc) {
2271                 /* flush for all mounted lustre fs. */
2272                 proc = fopen("/proc/mounts", "r");
2273                 if (!proc) {
2274                         fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2275                                 argv[0]);
2276                         return -1;
2277                 }
2278
2279                 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2280                         char dev[PATH_MAX];
2281                         char mp[PATH_MAX];
2282                         char fs[PATH_MAX];
2283
2284                         if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2285                                 fprintf(stderr, "%s: unexpected format in "
2286                                                 "/proc/mounts\n",
2287                                         argv[0]);
2288                                 return -1;
2289                         }
2290
2291                         if (strcmp(fs, "lustre") != 0)
2292                                 continue;
2293                         /* we use '@' to determine it's a client. are there
2294                          * any other better way?
2295                          */
2296                         if (strchr(dev, '@') == NULL)
2297                                 continue;
2298
2299                         if (flushctx_ioctl(mp))
2300                                 rc = -1;
2301                 }
2302         } else {
2303                 /* flush fs as specified */
2304                 while (optind < argc) {
2305                         if (flushctx_ioctl(argv[optind++]))
2306                                 rc = -1;
2307                 }
2308         }
2309
2310         return rc;
2311 }
2312
2313 static int lfs_lsetfacl(int argc, char **argv)
2314 {
2315         argv[0]++;
2316         return(llapi_lsetfacl(argc, argv));
2317 }
2318
2319 static int lfs_lgetfacl(int argc, char **argv)
2320 {
2321         argv[0]++;
2322         return(llapi_lgetfacl(argc, argv));
2323 }
2324
2325 static int lfs_rsetfacl(int argc, char **argv)
2326 {
2327         argv[0]++;
2328         return(llapi_rsetfacl(argc, argv));
2329 }
2330
2331 static int lfs_rgetfacl(int argc, char **argv)
2332 {
2333         argv[0]++;
2334         return(llapi_rgetfacl(argc, argv));
2335 }
2336
2337 static int lfs_cp(int argc, char **argv)
2338 {
2339         return(llapi_cp(argc, argv));
2340 }
2341
2342 static int lfs_ls(int argc, char **argv)
2343 {
2344         return(llapi_ls(argc, argv));
2345 }
2346
2347 /* A helper function to return single, whole lines delimited by newline.
2348    Returns length of line.  Not reentrant! */
2349 static int get_next_full_line(int fd, char **ptr)
2350 {
2351         static char buf[8192]; /* bigger than MAX_PATH_LENGTH */
2352         static char *sptr = buf, *eptr = buf;
2353         static int len, rem;
2354
2355         if ((*ptr == NULL) /* first time */
2356             || (eptr >= buf + len) /* buffer empty */) {
2357                 sptr = eptr = buf;
2358                 len = read(fd, buf, sizeof(buf));
2359                 if (len <= 0)
2360                         return len;
2361         } else {
2362                 sptr = eptr + 1;
2363         }
2364
2365 full_line:
2366         while (eptr < buf + len) {
2367                 eptr++;
2368                 /* parse full lines */
2369                 if (*eptr == '\n') {
2370                         *eptr = '\0';
2371                         *ptr = sptr;
2372                         return (eptr - sptr);
2373                 }
2374         }
2375
2376         /* partial line; move to front of buf */
2377         rem = buf + len - sptr;
2378         memcpy(buf, sptr, rem);
2379         sptr = buf;
2380         eptr = buf + rem;
2381         len = read(fd, eptr, sizeof(buf) - rem);
2382         if (len <= 0)
2383                 return len;
2384         len += rem;
2385         goto full_line;
2386 }
2387
2388 static int lfs_changelog(int argc, char **argv)
2389 {
2390         long long startrec = 0, endrec = 0, recnum;
2391         int fd, len;
2392         char c, *mdd, *ptr = NULL;
2393         struct option long_opts[] = {
2394                 {"follow", no_argument, 0, 'f'},
2395                 {0, 0, 0, 0}
2396         };
2397         char short_opts[] = "f";
2398         int follow = 0;
2399
2400         optind = 0;
2401         while ((c = getopt_long(argc, argv, short_opts,
2402                                 long_opts, NULL)) != -1) {
2403                 switch (c) {
2404                 case 'f':
2405                         follow++;
2406                         break;
2407                 case '?':
2408                         return CMD_HELP;
2409                 default:
2410                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2411                                 argv[0], argv[optind - 1]);
2412                         return CMD_HELP;
2413                 }
2414         }
2415         if (optind >= argc)
2416                 return CMD_HELP;
2417
2418         mdd = argv[optind++];
2419         if (argc > optind)
2420                 startrec = strtoll(argv[optind++], NULL, 10);
2421         if (argc > optind)
2422                 endrec = strtoll(argv[optind++], NULL, 10);
2423
2424         fd = llapi_changelog_open(mdd, startrec);
2425         if (fd < 0) {
2426                 fprintf(stderr, "%s Can't open changelog: %s\n", argv[0],
2427                         strerror(errno = -fd));
2428                 return fd;
2429         }
2430
2431         while ((len = get_next_full_line(fd, &ptr)) >= 0) {
2432                 if (len == 0) {
2433                         struct pollfd pfds[1];
2434                         int rc;
2435
2436                         if (!follow)
2437                                 break;
2438                         pfds[0].fd = fd;
2439                         pfds[0].events = POLLIN;
2440                         rc = poll(pfds, 1, -1);
2441                         if (rc < 0)
2442                                 break;
2443                         continue;
2444                 }
2445      /* eg. 2 02MKDIR 4405821890 t=[0x100000400/0x5] p=[0x100000400/0x4] pics */
2446                 sscanf(ptr, "%lld *", &recnum);
2447                 if (endrec && recnum > endrec)
2448                         break;
2449                 if (recnum < startrec)
2450                         continue;
2451                 printf("%.*s\n", len, ptr);
2452         }
2453
2454         close(fd);
2455
2456         if (len < 0) {
2457                 fprintf(stderr, "read err %d\n", errno);
2458                 return -errno;
2459         }
2460
2461         return 0;
2462 }
2463
2464 static int lfs_changelog_clear(int argc, char **argv)
2465 {
2466         long long endrec;
2467         int rc;
2468
2469         if (argc != 4)
2470                 return CMD_HELP;
2471
2472         endrec = strtoll(argv[3], NULL, 10);
2473
2474         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
2475         if (rc)
2476                 fprintf(stderr, "%s error: %s\n", argv[0],
2477                         strerror(errno = -rc));
2478         return rc;
2479 }
2480
2481 static int lfs_fid2path(int argc, char **argv)
2482 {
2483         struct option long_opts[] = {
2484                 {"cur", no_argument, 0, 'c'},
2485                 {"link", required_argument, 0, 'l'},
2486                 {"rec", required_argument, 0, 'r'},
2487                 {0, 0, 0, 0}
2488         };
2489         char c, short_opts[] = "cl:r:";
2490         char *device, *fid, *path;
2491         long long recno = -1;
2492         int linkno = -1;
2493         int lnktmp;
2494         int printcur = 0;
2495         int rc;
2496
2497         optind = 0;
2498         while ((c = getopt_long(argc, argv, short_opts,
2499                                 long_opts, NULL)) != -1) {
2500                 switch (c) {
2501                 case 'c':
2502                         printcur++;
2503                         break;
2504                 case 'l':
2505                         linkno = strtol(optarg, NULL, 10);
2506                         break;
2507                 case 'r':
2508                         recno = strtoll(optarg, NULL, 10);
2509                         break;
2510                 case '?':
2511                         return CMD_HELP;
2512                 default:
2513                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2514                                 argv[0], argv[optind - 1]);
2515                         return CMD_HELP;
2516                 }
2517         }
2518
2519         device = argv[optind++];
2520         fid = argv[optind++];
2521         if (optind != argc)
2522                 return CMD_HELP;
2523
2524         path = calloc(1, PATH_MAX);
2525
2526         lnktmp = (linkno >= 0) ? linkno : 0;
2527         while (1) {
2528                 int oldtmp = lnktmp;
2529                 long long rectmp = recno;
2530                 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2531                                     &lnktmp);
2532                 if (rc < 0) {
2533                         fprintf(stderr, "%s error: %s\n", argv[0],
2534                                 strerror(errno = -rc));
2535                         break;
2536                 }
2537
2538                 if (printcur)
2539                         fprintf(stdout, "%lld %s\n", rectmp, path);
2540                 else
2541                         fprintf(stdout, "%s\n", path);
2542
2543                 if (linkno >= 0)
2544                         /* specified linkno */
2545                         break;
2546                 if (oldtmp == lnktmp)
2547                         /* no more links */
2548                         break;
2549         }
2550
2551         free(path);
2552         return rc;
2553 }
2554
2555 static int lfs_path2fid(int argc, char **argv)
2556 {
2557         char *path;
2558         lustre_fid fid;
2559         int rc;
2560
2561         if (argc != 2)
2562                 return CMD_HELP;
2563
2564         path = argv[1];
2565         rc = llapi_path2fid(path, &fid);
2566         if (rc) {
2567                 fprintf(stderr, "can't get fid for %s: %s\n", path,
2568                         strerror(errno = -rc));
2569                 return rc;
2570         }
2571
2572         printf(DFID"\n", PFID(&fid));
2573
2574         return 0;
2575 }
2576
2577 int main(int argc, char **argv)
2578 {
2579         int rc;
2580
2581         setlinebuf(stdout);
2582
2583         ptl_initialize(argc, argv);
2584         if (obd_initialize(argc, argv) < 0)
2585                 exit(2);
2586         if (dbg_initialize(argc, argv) < 0)
2587                 exit(3);
2588
2589         Parser_init("lfs > ", cmdlist);
2590
2591         if (argc > 1) {
2592                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2593         } else {
2594                 rc = Parser_commands();
2595         }
2596
2597         obd_finalize(argc, argv);
2598         return rc;
2599 }
2600