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