Whamcloud - gitweb
1c831c9b273c7c658960c8e84e5cde8e98fc9d1b
[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 [sun.com URL with a
20  * copy of GPLv2].
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 #include <stdlib.h>
44 #include <stdio.h>
45 #include <getopt.h>
46 #include <string.h>
47 #include <mntent.h>
48 #include <errno.h>
49 #include <pwd.h>
50 #include <grp.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <dirent.h>
55 #include <time.h>
56 #include <ctype.h>
57 /* For dirname() */
58 #include <libgen.h>
59
60 #include <lnet/api-support.h>
61 #include <lnet/lnetctl.h>
62
63 #include <liblustre.h>
64 #include <lustre/lustre_idl.h>
65 #include <lustre/liblustreapi.h>
66 #include <lustre/lustre_user.h>
67
68 #include "parser.h"
69 #include "obdctl.h"
70
71 unsigned int libcfs_subsystem_debug = 0;
72
73 /* all functions */
74 static int lfs_setstripe(int argc, char **argv);
75 static int lfs_find(int argc, char **argv);
76 static int lfs_getstripe(int argc, char **argv);
77 static int lfs_osts(int argc, char **argv);
78 static int lfs_df(int argc, char **argv);
79 static int lfs_check(int argc, char **argv);
80 static int lfs_catinfo(int argc, char **argv);
81 #ifdef HAVE_QUOTA_SUPPORT
82 static int lfs_quotachown(int argc, char **argv);
83 static int lfs_quotacheck(int argc, char **argv);
84 static int lfs_quotaon(int argc, char **argv);
85 static int lfs_quotaoff(int argc, char **argv);
86 static int lfs_setquota(int argc, char **argv);
87 static int lfs_quota(int argc, char **argv);
88 #endif
89 static int lfs_flushctx(int argc, char **argv);
90 static int lfs_join(int argc, char **argv);
91 static int lfs_lsetfacl(int argc, char **argv);
92 static int lfs_lgetfacl(int argc, char **argv);
93 static int lfs_rsetfacl(int argc, char **argv);
94 static int lfs_rgetfacl(int argc, char **argv);
95 static int lfs_cp(int argc, char **argv);
96 static int lfs_ls(int argc, char **argv);
97
98 /* all avaialable commands */
99 command_t cmdlist[] = {
100         {"setstripe", lfs_setstripe, 0,
101          "Create a new file with a specific striping pattern or\n"
102          "set the default striping pattern on an existing directory or\n"
103          "delete the default striping pattern from an existing directory\n"
104          "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
105          "       or \n"
106          "       setstripe <filename|dirname> [--size|-s stripe_size]\n"
107          "                                    [--index|-i stripe_index]\n"
108          "                                    [--count|-c stripe_count]\n"
109          "       or \n"
110          "       setstripe -d <dirname>   (to delete default striping)\n"
111          "\tstripe_size:  Number of bytes on each OST (0 filesystem default)\n"
112          "\t              Can be specified with k, m or g (in KB, MB and GB respectively)\n"
113          "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
114          "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
115         {"getstripe", lfs_getstripe, 0,
116          "To list the striping info for a given filename or files in a\n"
117          "directory or recursively for all files in a directory tree.\n"
118          "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
119          "                 [--recursive | -r] <dir|file> ..."},
120         {"find", lfs_find, 0,
121          "To find files that match given parameters recursively in a directory tree.\n"
122          "usage: find <dir/file> ... \n"
123          "     [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
124          "     [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
125          "     [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
126          "     [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
127          "     [[!] --uid|-u N] [[!] --user|-U <name>]\n"
128          "\t !: used before an option indicates 'NOT' the requested attribute\n"
129          "\t -: used before an value indicates 'AT MOST' the requested value\n"
130          "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
131         {"check", lfs_check, 0,
132          "Display the status of MDS or OSTs (as specified in the command)\n"
133          "or all the servers (MDS and OSTs).\n"
134          "usage: check <osts|mds|servers>"},
135         {"catinfo", lfs_catinfo, 0,
136          "Show information of specified type logs.\n"
137          "usage: catinfo {keyword} [node name]\n"
138          "\tkeywords are one of followings: config, deletions.\n"
139          "\tnode name must be provided when use keyword config."},
140         {"join", lfs_join, 0,
141          "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
142          "usage: join <filename_A> <filename_B>\n"},
143         {"osts", lfs_osts, 0, "osts"},
144         {"df", lfs_df, 0,
145          "report filesystem disk space usage or inodes usage"
146          "of each MDS/OSD.\n"
147          "Usage: df [-i] [-h] [path]"},
148 #ifdef HAVE_QUOTA_SUPPORT
149         {"quotachown",lfs_quotachown, 0,
150          "Change files' owner or group on the specified filesystem.\n"
151          "usage: quotachown [-i] <filesystem>\n"
152          "\t-i: ignore error if file is not exist\n"},
153         {"quotacheck", lfs_quotacheck, 0,
154          "Scan the specified filesystem for disk usage, and create,\n"
155          "or update quota files.\n"
156          "usage: quotacheck [ -ug ] <filesystem>"},
157         {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
158          "usage: quotaon [ -ugf ] <filesystem>"},
159         {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
160          "usage: quotaoff [ -ug ] <filesystem>"},
161         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
162          "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
163          "       setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
164         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
165          "usage: quota [ -o obd_uuid ] [{-u|-g  <name>}|-t] <filesystem>"},
166 #endif
167         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
168          "usage: flushctx [-k] [mountpoint...]"},
169         {"lsetfacl", lfs_lsetfacl, 0,
170          "Remote user setfacl for user/group on the same remote client.\n"
171          "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
172         {"lgetfacl", lfs_lgetfacl, 0,
173          "Remote user getfacl for user/group on the same remote client.\n"
174          "usage: lgetfacl [-dRLPvh] file ..."},
175         {"rsetfacl", lfs_rsetfacl, 0,
176          "Remote user setfacl for user/group on other clients.\n"
177          "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
178         {"rgetfacl", lfs_rgetfacl, 0,
179          "Remote user getfacl for user/group on other clients.\n"
180          "usage: rgetfacl [-dRLPvh] file ..."},
181         {"cp", lfs_cp, 0,
182          "Remote user copy files and directories.\n"
183          "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
184         {"ls", lfs_ls, 0,
185          "Remote user list directory contents.\n"
186          "usage: ls [OPTION]... [FILE]..."},
187         {"help", Parser_help, 0, "help"},
188         {"exit", Parser_quit, 0, "quit"},
189         {"quit", Parser_quit, 0, "quit"},
190         { 0, 0, 0, NULL }
191 };
192
193 /* functions */
194 static int lfs_setstripe(int argc, char **argv)
195 {
196         char *fname;
197         int result;
198         unsigned long long st_size;
199         int  st_offset, st_count;
200         char *end;
201         int c;
202         int delete = 0;
203         char *stripe_size_arg = NULL;
204         char *stripe_off_arg = NULL;
205         char *stripe_count_arg = NULL;
206         unsigned long long size_units;
207
208         struct option long_opts[] = {
209                 {"size",        required_argument, 0, 's'},
210                 {"count",       required_argument, 0, 'c'},
211                 {"index",       required_argument, 0, 'i'},
212                 {"delete",      no_argument,       0, 'd'},
213                 {0, 0, 0, 0}
214         };
215
216         st_size = 0;
217         st_offset = -1;
218         st_count = 0;
219         if (argc == 3 && strcmp(argv[1], "-d") == 0) {
220                 /* for compatibility with the existing positional parameter
221                  * usage */
222                 fname = argv[2];
223                 optind = 2;
224         } else if (argc == 5  && 
225                    (argv[2][0] != '-' || isdigit(argv[2][1])) &&
226                    (argv[3][0] != '-' || isdigit(argv[3][1])) &&
227                    (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
228                 /* for compatibility with the existing positional parameter
229                  * usage */
230                 fname = argv[1];
231                 stripe_size_arg = argv[2];
232                 stripe_off_arg = argv[3];
233                 stripe_count_arg = argv[4];
234                 optind = 4;
235         } else {
236                 optind = 0;
237                 while ((c = getopt_long(argc, argv, "c:di:s:",
238                                                 long_opts, NULL)) >= 0) {
239                         switch (c) {
240                         case 0:
241                                 /* Long options. */
242                                 break;
243                         case 'c':
244                                 stripe_count_arg = optarg;
245                                 break;
246                         case 'd':
247                                 /* delete the default striping pattern */
248                                 delete = 1;
249                                 break;
250                         case 'i':
251                                 stripe_off_arg = optarg;
252                                 break;
253                         case 's':
254                                 stripe_size_arg = optarg;
255                                 break;
256                         case '?':
257                                 return CMD_HELP;
258                         default:
259                                 fprintf(stderr, "error: %s: option '%s' "
260                                                 "unrecognized\n",
261                                                 argv[0], argv[optind - 1]);
262                                 return CMD_HELP;
263                         }
264                 }
265                 if (optind < argc)
266                         fname = argv[optind];
267                 else
268                         return CMD_HELP;
269
270
271                 if (delete && 
272                     (stripe_size_arg != NULL || stripe_off_arg != NULL || 
273                      stripe_count_arg != NULL)) {
274                         fprintf(stderr, "error: %s: cannot specify -d with "
275                                         "-s, -c or -i options\n",
276                                         argv[0]);
277                         return CMD_HELP;
278                 }
279         }
280         if (optind != argc - 1) {
281                 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
282                                 "specified: '%s'\n",
283                                 argv[0], argv[argc - 1]);
284                 return CMD_HELP;
285         }
286
287         /* get the stripe size */
288         if (stripe_size_arg != NULL) {
289                 result = parse_size(stripe_size_arg, &st_size, &size_units);
290                 if (result) {
291                         fprintf(stderr,"error: bad size '%s'\n",
292                                 stripe_size_arg);
293                         return result;
294                 }
295         }
296         /* get the stripe offset */
297         if (stripe_off_arg != NULL) {
298                 st_offset = strtoul(stripe_off_arg, &end, 0);
299                 if (*end != '\0') {
300                         fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
301                                 argv[0], stripe_off_arg);
302                         return CMD_HELP;
303                 }
304         }
305         /* get the stripe count */
306         if (stripe_count_arg != NULL) {
307                 st_count = strtoul(stripe_count_arg, &end, 0);
308                 if (*end != '\0') {
309                         fprintf(stderr, "error: %s: bad stripe count '%s'\n",
310                                 argv[0], stripe_count_arg);
311                         return CMD_HELP;
312                 }
313         }
314
315         result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
316         if (result)
317                 fprintf(stderr, "error: %s: create stripe file failed\n",
318                                 argv[0]);
319
320         return result;
321 }
322
323 static int set_time(time_t *time, time_t *set, char *str)
324 {
325         time_t t;
326         int res = 0;
327         
328         if (str[0] == '+')
329                 res = 1;
330         else if (str[0] == '-')
331                 res = -1;
332
333         if (res)
334                 str++;
335
336         t = strtol(str, NULL, 0);
337         if (*time < t * 24 * 60 * 60) {
338                 if (res)
339                         str--;
340                 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
341                 return INT_MAX;
342         }
343
344         *set = *time - t * 24 * 60 * 60;
345         return res;
346 }
347
348 static int name2id(unsigned int *id, char *name, int type)
349 {
350         if (type == USRQUOTA) {
351                 struct passwd *entry;
352
353                 if (!(entry = getpwnam(name))) {
354                         if (!errno)
355                                 errno = ENOENT;
356                         return -1;
357                 }
358
359                 *id = entry->pw_uid;
360         } else {
361                 struct group *entry;
362
363                 if (!(entry = getgrnam(name))) {
364                         if (!errno)
365                                 errno = ENOENT;
366                         return -1;
367                 }
368
369                 *id = entry->gr_gid;
370         }
371
372         return 0;
373 }
374
375 static int id2name(char **name, unsigned int id, int type)
376 {
377         if (type == USRQUOTA) {
378                 struct passwd *entry;
379
380                 if (!(entry = getpwuid(id))) {
381                         if (!errno)
382                                 errno = ENOENT;
383                         return -1;
384                 }
385
386                 *name = entry->pw_name;
387         } else {
388                 struct group *entry;
389
390                 if (!(entry = getgrgid(id))) {
391                         if (!errno)
392                                 errno = ENOENT;
393                         return -1;
394                 }
395
396                 *name = entry->gr_name;
397         }
398
399         return 0;
400 }
401
402 static int lfs_find(int argc, char **argv)
403 {
404         int new_fashion = 1;
405         int c, ret;
406         time_t t;
407         struct find_param param = { .maxdepth = -1 };
408         char str[1024];
409         struct option long_opts[] = {
410                 /* New find options. */
411                 {"atime",     required_argument, 0, 'A'},
412                 {"ctime",     required_argument, 0, 'C'},
413                 {"mtime",     required_argument, 0, 'M'},
414                 {"maxdepth",  required_argument, 0, 'D'},
415                 {"gid",       required_argument, 0, 'g'},
416                 {"group",     required_argument, 0, 'G'},
417                 {"uid",       required_argument, 0, 'u'},
418                 {"user",      required_argument, 0, 'U'},
419                 {"name",      required_argument, 0, 'n'},
420                 /* --obd is considered as a new option. */
421                 {"obd",       required_argument, 0, 'O'},
422                 {"ost",       required_argument, 0, 'O'},
423                 {"print",     no_argument,       0, 'P'},
424                 {"print0",    no_argument,       0, 'p'},
425                 /* Old find options. */
426                 {"quiet",     no_argument,       0, 'q'},
427                 {"recursive", no_argument,       0, 'r'},
428                 {"size",      required_argument, 0, 's'},
429                 {"type",      required_argument, 0, 't'},
430                 {"verbose",   no_argument,       0, 'v'},
431                 {0, 0, 0, 0}
432         };
433         int pathstart = -1;
434         int pathend = -1;
435         int neg_opt = 0;
436         time_t *xtime;
437         int *xsign;
438         int isoption;
439         char *endptr;
440
441         time(&t);
442
443         optind = 0;
444         while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
445                                      long_opts, NULL)) >= 0) {
446                 xtime = NULL;
447                 xsign = NULL;
448                 if (neg_opt)
449                         --neg_opt;
450                 /* '!' is part of option */
451                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
452                 if (!isoption && pathend != -1) {
453                         fprintf(stderr, "err: %s: filename|dirname must either "
454                                         "precede options or follow options\n",
455                                         argv[0]);
456                         return CMD_HELP;
457                 }
458                 if (!isoption && pathstart == -1)
459                         pathstart = optind - 1;
460                 if (isoption && pathstart != -1 && pathend == -1) {
461                         pathend = optind - 2;
462                         if ((c == 1 && strcmp(optarg, "!") == 0) ||
463                             c == 'P' || c == 'p' || c == 'O' ||
464                             c == 'q' || c == 'r' || c == 'v')
465                                 pathend = optind - 1;
466                 }
467                 switch (c) {
468                 case 0:
469                         /* Long options. */
470                         break;
471                 case 1:
472                         if (strcmp(optarg, "!") == 0)
473                                 neg_opt = 2;
474                         break;
475                 case 'A':
476                         xtime = &param.atime;
477                         xsign = &param.asign;
478                 case 'C':
479                         if (c == 'C') {
480                                 xtime = &param.ctime;
481                                 xsign = &param.csign;
482                         }
483                 case 'M':
484                         if (c == 'M') {
485                                 xtime = &param.mtime;
486                                 xsign = &param.msign;
487                         }
488                         new_fashion = 1;
489                         if (neg_opt) {
490                                 if (optarg[0] == '-')
491                                         optarg[0] = '+';
492                                 else if (optarg[0] == '+')
493                                         optarg[0] = '-';
494                                 else {
495                                         str[0] = '-';
496                                         str[1] = '\0';
497                                         strcat(str, optarg);
498                                         optarg = str;
499                                 }
500                         }
501                         ret = set_time(&t, xtime, optarg);
502                         if (ret == INT_MAX)
503                                 return -1;
504                         if (ret)
505                                 *xsign = ret;
506                         break;
507                 case 'D':
508                         new_fashion = 1;
509                         param.maxdepth = strtol(optarg, 0, 0);
510                         break;
511                 case 'g':
512                         new_fashion = 1;
513                         param.gid = strtol(optarg, &endptr, 10);
514                         if (optarg == endptr) {
515                                 fprintf(stderr, "Bad gid: %s\n", optarg);
516                                 return CMD_HELP;
517                         }
518                         param.exclude_gid = !!neg_opt;
519                         param.check_gid = 1;
520                         break;
521                 case 'G':
522                         new_fashion = 1;
523                         param.gid = strtol(optarg, &endptr, 10);
524                         if (optarg == endptr) {
525                                 ret = name2id(&param.gid, optarg, GRPQUOTA);
526                                 if (ret != 0) {
527                                         fprintf(stderr, "Group/GID: %s cannot "
528                                                 "be found.\n", optarg);
529                                         return -1;
530                                 }
531                         }
532                         param.exclude_gid = !!neg_opt;
533                         param.check_gid = 1;
534                         break;
535                 case 'u':
536                         new_fashion = 1;
537                         param.uid = strtol(optarg, &endptr, 10);
538                         if (optarg == endptr) {
539                                 fprintf(stderr, "Bad uid: %s\n", optarg);
540                                 return CMD_HELP;
541                         }
542                         param.exclude_uid = !!neg_opt;
543                         param.check_uid = 1;
544                         break;
545                 case 'U':
546                         new_fashion = 1;
547                         param.uid = strtol(optarg, &endptr, 10);
548                         if (optarg == endptr) {
549                                 ret = name2id(&param.uid, optarg, USRQUOTA);
550                                 if (ret != 0) {
551                                         fprintf(stderr, "User/UID: %s cannot "
552                                                 "be found.\n", optarg);
553                                         return -1;
554                                 }
555                         }
556                         param.exclude_uid = !!neg_opt;
557                         param.check_uid = 1;
558                         break;
559                 case 'n':
560                         new_fashion = 1;
561                         param.pattern = (char *)optarg;
562                         param.exclude_pattern = !!neg_opt;
563                         break;
564                 case 'O': {
565                         char *buf, *token, *next, *p;
566                         int len;
567
568                         len = strlen((char *)optarg);
569                         buf = malloc(len+1);
570                         if (buf == NULL)
571                                 return -ENOMEM;
572                         strcpy(buf, (char *)optarg);
573
574                         if (param.num_alloc_obds == 0) {
575                                 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
576                                                        sizeof(struct obd_uuid));
577                                 if (param.obduuid == NULL)
578                                         return -ENOMEM;
579                                 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
580                         }
581
582                         for (token = buf; token && *token; token = next) {
583                                 p = strchr(token, ',');
584                                 next = 0;
585                                 if (p) {
586                                         *p = 0;
587                                         next = p+1;
588                                 }
589                                 strcpy((char *)&param.obduuid[param.num_obds++].uuid,
590                                        token);
591                         }
592
593                         if (buf)
594                                 free(buf);
595                         break;
596                 }
597                 case 'p':
598                         new_fashion = 1;
599                         param.zeroend = 1;
600                         break;
601                 case 'P':
602                         break;
603                 case 'q':
604                         new_fashion = 0;
605                         param.quiet++;
606                         param.verbose = 0;
607                         break;
608                 case 'r':
609                         new_fashion = 0;
610                         param.recursive = 1;
611                         break;
612                 case 't':
613                         param.exclude_type = !!neg_opt;
614                         switch(optarg[0]) {
615                         case 'b': param.type = S_IFBLK; break;
616                         case 'c': param.type = S_IFCHR; break;
617                         case 'd': param.type = S_IFDIR; break;
618                         case 'f': param.type = S_IFREG; break;
619                         case 'l': param.type = S_IFLNK; break;
620                         case 'p': param.type = S_IFIFO; break;
621                         case 's': param.type = S_IFSOCK; break;
622 #ifdef S_IFDOOR /* Solaris only */
623                         case 'D': param.type = S_IFDOOR; break;
624 #endif
625                         default: fprintf(stderr, "error: %s: bad type '%s'\n",
626                                          argv[0], optarg);
627                                  return CMD_HELP;
628                         };
629                         break;
630                 case 's':
631                         if (neg_opt) {
632                                 if (optarg[0] == '-')
633                                         optarg[0] = '+';
634                                 else if (optarg[0] == '+')
635                                         optarg[0] = '-';
636                                 else {
637                                         str[0] = '-';
638                                         str[1] = '\0';
639                                         strcat(str, optarg);
640                                         optarg = str;
641                                 }
642                         }
643                         if (optarg[0] == '+')
644                                 param.size_sign = -1;
645                         else if (optarg[0] == '-')
646                                 param.size_sign = +1;
647
648                         if (param.size_sign)
649                                 optarg++;
650                         ret = parse_size(optarg, &param.size,&param.size_units);
651                         if (ret) {
652                                 fprintf(stderr,"error: bad size '%s'\n",
653                                         optarg);
654                                 return ret;
655                         }
656                         break;
657                 case 'v':
658                         new_fashion = 0;
659                         param.verbose++;
660                         param.quiet = 0;
661                         break;
662                 case '?':
663                         return CMD_HELP;
664                 default:
665                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
666                                 argv[0], argv[optind - 1]);
667                         return CMD_HELP;
668                 };
669         }
670         
671         if (pathstart == -1) {
672                 fprintf(stderr, "error: %s: no filename|pathname\n",
673                         argv[0]);
674                 return CMD_HELP;
675         } else if (pathend == -1) {
676                 /* no options */
677                 pathend = argc;
678         }
679
680         if (new_fashion) {
681                 param.quiet = 1;
682         } else {
683                 static int deprecated_warning;
684                 if (!deprecated_warning) {
685                         fprintf(stderr, "lfs find: -q, -r, -v options "
686                                 "deprecated.  Use 'lfs getstripe' instead.\n");
687                         deprecated_warning = 1;
688                 }
689                 if (!param.recursive && param.maxdepth == -1)
690                         param.maxdepth = 1;
691         }
692         
693         do {
694                 if (new_fashion)
695                         ret = llapi_find(argv[pathstart], &param);
696                 else
697                         ret = llapi_getstripe(argv[pathstart], &param);
698         } while (++pathstart < pathend && !ret);
699
700         if (ret)
701                 fprintf(stderr, "error: %s failed for %s.\n",
702                         argv[0], argv[optind - 1]);
703
704         if (param.obduuid && param.num_alloc_obds)
705                 free(param.obduuid);
706
707         return ret;
708 }
709
710 static int lfs_getstripe(int argc, char **argv)
711 {
712         struct option long_opts[] = {
713                 {"obd", 1, 0, 'O'},
714                 {"quiet", 0, 0, 'q'},
715                 {"recursive", 0, 0, 'r'},
716                 {"verbose", 0, 0, 'v'},
717                 {0, 0, 0, 0}
718         };
719         char short_opts[] = "hO:qrv";
720         int c, rc;
721         struct find_param param = { 0 };
722
723         optind = 0;
724         while ((c = getopt_long(argc, argv, short_opts,
725                                 long_opts, NULL)) != -1) {
726                 switch (c) {
727                 case 'O':
728                         if (param.obduuid) {
729                                 fprintf(stderr,
730                                         "error: %s: only one obduuid allowed",
731                                         argv[0]);
732                                 return CMD_HELP;
733                         }
734                         param.obduuid = (struct obd_uuid *)optarg;
735                         break;
736                 case 'q':
737                         param.quiet++;
738                         param.verbose = 0;
739                         break;
740                 case 'r':
741                         param.recursive = 1;
742                         break;
743                 case 'v':
744                         param.verbose++;
745                         param.quiet = 0;
746                         break;
747                 case '?':
748                         return CMD_HELP;
749                 default:
750                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
751                                 argv[0], argv[optind - 1]);
752                         return CMD_HELP;
753                 }
754         }
755
756         if (optind >= argc)
757                 return CMD_HELP;
758
759         param.maxdepth = param.recursive ? -1 : 1;
760
761         do {
762                 rc = llapi_getstripe(argv[optind], &param);
763         } while (++optind < argc && !rc);
764
765         if (rc)
766                 fprintf(stderr, "error: %s failed for %s.\n", 
767                         argv[0], argv[optind - 1]);
768         return rc;
769 }
770
771 static int lfs_osts(int argc, char **argv)
772 {
773         FILE *fp;
774         struct mntent *mnt = NULL;
775         struct find_param param;
776         int rc=0;
777
778         if (argc != 1)
779                 return CMD_HELP;
780
781         fp = setmntent(MOUNTED, "r");
782
783         if (fp == NULL) {
784                  fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
785                         strerror (errno));
786         } else {
787                 mnt = getmntent(fp);
788                 while (feof(fp) == 0 && ferror(fp) ==0) {
789                         memset(&param, 0, sizeof(param));
790                         if (llapi_is_lustre_mnt(mnt)) {
791                                 rc = llapi_getstripe(mnt->mnt_dir, &param);
792                                 if (rc)
793                                         fprintf(stderr,
794                                                "error: %s: failed on %s\n",
795                                                argv[0], mnt->mnt_dir);
796                         }
797                         mnt = getmntent(fp);
798                 }
799                 endmntent(fp);
800         }
801
802         return rc;
803 }
804
805 #define COOK(value)                                                     \
806 ({                                                                      \
807         int radix = 0;                                                  \
808         while (value > 1024) {                                          \
809                 value /= 1024;                                          \
810                 radix++;                                                \
811         }                                                               \
812         radix;                                                          \
813 })
814 #define UUF     "%-20s"
815 #define CSF     "%9s"
816 #define CDF     "%9llu"
817 #define HSF     "%8s"
818 #define HDF     "%6.1f"
819 #define RSF     "%5s"
820 #define RDF     "%4d%%"
821
822 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
823 {
824         char rpath[PATH_MAX] = {'\0'};
825         struct mntent *mnt;
826         int rc, len, out_len = 0;
827
828         if (!realpath(path, rpath)) {
829                 rc = -errno;
830                 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
831                         path, strerror(-rc));
832                 return rc;
833         }
834
835         len = 0;
836         mnt = getmntent(fp);
837         while (feof(fp) == 0 && ferror(fp) == 0) {
838                 if (llapi_is_lustre_mnt(mnt)) {
839                         len = strlen(mnt->mnt_dir);
840                         if (len > out_len &&
841                             !strncmp(rpath, mnt->mnt_dir, len)) {
842                                 out_len = len;
843                                 memset(mntdir, 0, dir_len);
844                                 strncpy(mntdir, mnt->mnt_dir, dir_len);
845                         }
846                 }
847                 mnt = getmntent(fp);
848         }
849
850         if (out_len > 0)
851                 return 0;
852
853         fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
854         return -EINVAL;
855 }
856
857 static int showdf(char *mntdir, struct obd_statfs *stat,
858                   char *uuid, int ishow, int cooked,
859                   char *type, int index, int rc)
860 {
861         long long avail, used, total;
862         double ratio = 0;
863         char *suffix = "KMGTPEZY";
864         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
865         char tbuf[20], ubuf[20], abuf[20], rbuf[20];
866
867         if (!uuid || !stat)
868                 return -EINVAL;
869
870         switch (rc) {
871         case 0:
872                 if (ishow) {
873                         avail = stat->os_ffree;
874                         used = stat->os_files - stat->os_ffree;
875                         total = stat->os_files;
876                 } else {
877                         int shift = cooked ? 0 : 10;
878
879                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
880                         used  = ((stat->os_blocks - stat->os_bfree) *
881                                  stat->os_bsize) >> shift;
882                         total = (stat->os_blocks * stat->os_bsize) >> shift;
883                 }
884
885                 if (total > 0)
886                         ratio = (double)used / (double)total;
887
888                 if (cooked) {
889                         int i;
890                         double cook_val;
891
892                         cook_val = (double)total;
893                         i = COOK(cook_val);
894                         if (i > 0)
895                                 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
896                         else
897                                 sprintf(tbuf, CDF, total);
898
899                         cook_val = (double)used;
900                         i = COOK(cook_val);
901                         if (i > 0)
902                                 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
903                         else
904                                 sprintf(ubuf, CDF, used);
905
906                         cook_val = (double)avail;
907                         i = COOK(cook_val);
908                         if (i > 0)
909                                 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
910                         else
911                                 sprintf(abuf, CDF, avail);
912                 } else {
913                         sprintf(tbuf, CDF, total);
914                         sprintf(ubuf, CDF, used);
915                         sprintf(abuf, CDF, avail);
916                 }
917
918                 sprintf(rbuf, RDF, (int)(ratio * 100));
919                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
920                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
921                 if (type)
922                         printf("[%s:%d]\n", type, index);
923                 else
924                         printf("\n");
925
926                 break;
927         case -ENODATA:
928                 printf(UUF": inactive device\n", uuid);
929                 break;
930         default:
931                 printf(UUF": %s\n", uuid, strerror(-rc));
932                 break;
933         }
934
935         return 0;
936 }
937
938 static int mntdf(char *mntdir, int ishow, int cooked)
939 {
940         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
941         struct obd_uuid uuid_buf;
942         __u32 index;
943         int rc;
944
945         if (ishow)
946                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
947                        "UUID", "Inodes", "IUsed", "IFree",
948                        "IUse%", "Mounted on");
949         else
950                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
951                        "UUID", cooked ? "bytes" : "1K-blocks",
952                        "Used", "Available", "Use%", "Mounted on");
953
954         for (index = 0; ; index++) {
955                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
956                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
957                 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
958                                       &stat_buf, &uuid_buf);
959                 if (rc == -ENODEV)
960                         break;
961
962                 if (rc == -EAGAIN)
963                         continue;
964
965                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
966                     rc == -ENODATA || rc == 0) {
967                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
968                                "MDT", index, rc);
969                 } else {
970                         fprintf(stderr,
971                                 "error: llapi_obd_statfs(%s): %s (%d)\n",
972                                 uuid_buf.uuid, strerror(-rc), rc);
973                         return rc;
974                 }
975                 if (rc == 0) {
976                         sum.os_ffree += stat_buf.os_ffree;
977                         sum.os_files += stat_buf.os_files;
978                 }
979         }
980
981         for (index = 0; ; index++) {
982                 memset(&stat_buf, 0, sizeof(struct obd_statfs));
983                 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
984                 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
985                                       &stat_buf, &uuid_buf);
986                 if (rc == -ENODEV)
987                         break;
988
989                 if (rc == -EAGAIN)
990                         continue;
991
992                 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
993                     rc == -ENODATA || rc == 0) {
994                         showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
995                                "OST", index, rc);
996                 } else {
997                         fprintf(stderr,
998                                 "error: llapi_obd_statfs failed: %s (%d)\n",
999                                 strerror(-rc), rc);
1000                         return rc;
1001                 }
1002                 if (rc == 0) {
1003                         sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1004                         sum.os_bfree  += stat_buf.os_bfree * stat_buf.os_bsize;
1005                         sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1006                 }
1007         }
1008
1009         printf("\n");
1010         showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1011
1012         return 0;
1013 }
1014
1015 static int lfs_df(int argc, char **argv)
1016 {
1017         FILE *fp;
1018         char *path = NULL;
1019         struct mntent *mnt = NULL;
1020         char *mntdir = NULL;
1021         int ishow = 0, cooked = 0;
1022         int c, rc = 0;
1023
1024         optind = 0;
1025         while ((c = getopt(argc, argv, "ih")) != -1) {
1026                 switch (c) {
1027                 case 'i':
1028                         ishow = 1;
1029                         break;
1030                 case 'h':
1031                         cooked = 1;
1032                         break;
1033                 default:
1034                         return CMD_HELP;
1035                 }
1036         }
1037         if (optind < argc )
1038                 path = argv[optind];
1039
1040         fp = setmntent(MOUNTED, "r");
1041         if (fp == NULL) {
1042                 rc = -errno;
1043                 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1044                         argv[0], MOUNTED, strerror(errno));
1045                 return rc;
1046         }
1047
1048         if ((mntdir = malloc(PATH_MAX)) == NULL) {
1049                 fprintf(stderr, "error: cannot allocate %d bytes\n",
1050                         PATH_MAX);
1051                 return -ENOMEM;
1052         }
1053         memset(mntdir, 0, PATH_MAX);
1054
1055         if (path) {
1056                 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1057                 if (rc) {
1058                         endmntent(fp);
1059                         free(mntdir);
1060                         return rc;
1061                 }
1062
1063                 rc = mntdf(mntdir, ishow, cooked);
1064                 printf("\n");
1065                 endmntent(fp);
1066         } else {
1067                 mnt = getmntent(fp);
1068                 while (feof(fp) == 0 && ferror(fp) == 0) {
1069                         if (llapi_is_lustre_mnt(mnt)) {
1070                                 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1071                                 if (rc)
1072                                         break;
1073                                 printf("\n");
1074                         }
1075                         mnt = getmntent(fp);
1076                 }
1077                 endmntent(fp);
1078         }
1079
1080         free(mntdir);
1081         return rc;
1082 }
1083
1084 static int lfs_check(int argc, char **argv)
1085 {
1086         int rc;
1087         FILE *fp;
1088         struct mntent *mnt = NULL;
1089         int num_types = 1;
1090         char *obd_types[2];
1091         char obd_type1[4];
1092         char obd_type2[4];
1093
1094         if (argc != 2)
1095                 return CMD_HELP;
1096
1097         obd_types[0] = obd_type1;
1098         obd_types[1] = obd_type2;
1099
1100         if (strcmp(argv[1], "osts") == 0) {
1101                 strcpy(obd_types[0], "osc");
1102         } else if (strcmp(argv[1], "mds") == 0) {
1103                 strcpy(obd_types[0], "mdc");
1104         } else if (strcmp(argv[1], "servers") == 0) {
1105                 num_types = 2;
1106                 strcpy(obd_types[0], "osc");
1107                 strcpy(obd_types[1], "mdc");
1108         } else {
1109                 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1110                                 argv[0], argv[1]);
1111                         return CMD_HELP;
1112         }
1113
1114         fp = setmntent(MOUNTED, "r");
1115         if (fp == NULL) {
1116                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1117                         strerror (errno));
1118         } else {
1119                 mnt = getmntent(fp);
1120                 while (feof(fp) == 0 && ferror(fp) ==0) {
1121                         if (llapi_is_lustre_mnt(mnt))
1122                                 break;
1123                         mnt = getmntent(fp);
1124                 }
1125                 endmntent(fp);
1126         }
1127
1128         if (!mnt) {
1129                 fprintf(stderr, "No suitable Lustre mount found\n");
1130                 return -1;
1131         }
1132
1133         rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1134
1135         if (rc)
1136                 fprintf(stderr, "error: %s: %s status failed\n",
1137                                 argv[0],argv[1]);
1138
1139         return rc;
1140
1141 }
1142
1143 static int lfs_catinfo(int argc, char **argv)
1144 {
1145         FILE *fp;
1146         struct mntent *mnt = NULL;
1147         int rc;
1148
1149         if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1150                 return CMD_HELP;
1151
1152         if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1153                 return CMD_HELP;
1154
1155         fp = setmntent(MOUNTED, "r");
1156         if (fp == NULL) {
1157                  fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1158                          strerror(errno));
1159         } else {
1160                 mnt = getmntent(fp);
1161                 while (feof(fp) == 0 && ferror(fp) == 0) {
1162                         if (llapi_is_lustre_mnt(mnt))
1163                                 break;
1164                         mnt = getmntent(fp);
1165                 }
1166                 endmntent(fp);
1167         }
1168
1169         if (mnt) {
1170                 if (argc == 3)
1171                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1172                 else
1173                         rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1174         } else {
1175                 fprintf(stderr, "no lustre_lite mounted.\n");
1176                 rc = -1;
1177         }
1178
1179         return rc;
1180 }
1181
1182 int lfs_join(int argc, char **argv)
1183 {
1184         char *name_head, *name_tail;
1185         int fd, rc;
1186         loff_t size;
1187
1188         if (argc != 3)
1189                 return CMD_HELP;
1190         name_head = argv[1];
1191         fd = open(name_head, O_WRONLY);
1192         if (fd < 0) {
1193                 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1194                         name_head, fd);
1195                 return fd;
1196         }
1197         size = lseek(fd, 0, SEEK_END);
1198         if (size % JOIN_FILE_ALIGN) {
1199                 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1200                         name_head, (long long)size, JOIN_FILE_ALIGN);
1201                 rc = -EINVAL;
1202                 goto out;
1203         }
1204         name_tail = argv[2];
1205         rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1206 out:
1207         close(fd);
1208         if (rc) {
1209                 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1210                         argv[1], argv[2]);
1211         }
1212         return rc;
1213 }
1214
1215 #ifdef HAVE_QUOTA_SUPPORT
1216 static int lfs_quotachown(int argc, char **argv)
1217 {
1218
1219         int c,rc;
1220         int flag = 0;
1221
1222         optind = 0;
1223         while ((c = getopt(argc, argv, "i")) != -1) {
1224                 switch (c) {
1225                 case 'i':
1226                         flag++;
1227                         break;
1228                 default:
1229                         fprintf(stderr, "error: %s: option '-%c' "
1230                                         "unrecognized\n", argv[0], c);
1231                         return CMD_HELP;
1232                 }
1233         }
1234         if (optind == argc)
1235                 return CMD_HELP;
1236         rc = llapi_quotachown(argv[optind], flag);
1237         if(rc)
1238                 fprintf(stderr,"error: change file owner/group failed.\n");
1239         return rc;
1240 }
1241
1242
1243 static int lfs_quotacheck(int argc, char **argv)
1244 {
1245         int c, check_type = 0;
1246         char *mnt;
1247         struct if_quotacheck qchk;
1248         struct if_quotactl qctl;
1249         char *obd_type = qchk.obd_type;
1250         char *obd_uuid = qchk.obd_uuid.uuid;
1251         int rc;
1252
1253         memset(&qchk, 0, sizeof(qchk));
1254
1255         optind = 0;
1256         while ((c = getopt(argc, argv, "ug")) != -1) {
1257                 switch (c) {
1258                 case 'u':
1259                         check_type |= 0x01;
1260                         break;
1261                 case 'g':
1262                         check_type |= 0x02;
1263                         break;
1264                 default:
1265                         fprintf(stderr, "error: %s: option '-%c' "
1266                                         "unrecognized\n", argv[0], c);
1267                         return CMD_HELP;
1268                 }
1269         }
1270
1271         if (check_type)
1272                 check_type--;
1273         else    /* do quotacheck for both user & group quota by default */
1274                 check_type = 0x02;
1275
1276         if (argc == optind)
1277                 return CMD_HELP;
1278
1279         mnt = argv[optind];
1280
1281         memset(&qctl, 0, sizeof(qctl));
1282         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1283         qctl.qc_id = QFMT_LDISKFS;
1284         qctl.qc_type = check_type;
1285         rc = llapi_quotactl(mnt, &qctl);
1286         if (rc) {
1287                 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1288                 return rc;
1289         }
1290
1291         rc = llapi_quotacheck(mnt, check_type);
1292         if (rc) {
1293                 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1294                 return rc;
1295         }
1296
1297         rc = llapi_poll_quotacheck(mnt, &qchk);
1298         if (rc) {
1299                 if (*obd_type)
1300                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1301                 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1302                 return rc;
1303         }
1304
1305         memset(&qctl, 0, sizeof(qctl));
1306         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1307         qctl.qc_id = QFMT_LDISKFS;
1308         qctl.qc_type = check_type;
1309         rc = llapi_quotactl(mnt, &qctl);
1310         if (rc) {
1311                 if (*obd_type)
1312                         fprintf(stderr, "%s %s ",
1313                                 qctl.obd_type, qctl.obd_uuid.uuid);
1314                 fprintf(stderr, "%s turn on quota failed: %s\n",
1315                         argv[0], strerror(errno));
1316                 return rc;
1317         }
1318
1319         return 0;
1320 }
1321
1322 static int lfs_quotaon(int argc, char **argv)
1323 {
1324         int c;
1325         char *mnt;
1326         struct if_quotactl qctl;
1327         char *obd_type = qctl.obd_type;
1328         char *obd_uuid = qctl.obd_uuid.uuid;
1329         int rc;
1330
1331         memset(&qctl, 0, sizeof(qctl));
1332         qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1333         qctl.qc_id = QFMT_LDISKFS;
1334
1335         optind = 0;
1336         while ((c = getopt(argc, argv, "ugf")) != -1) {
1337                 switch (c) {
1338                 case 'u':
1339                         qctl.qc_type |= 0x01;
1340                         break;
1341                 case 'g':
1342                         qctl.qc_type |= 0x02;
1343                         break;
1344                 case 'f':
1345                         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1346                         break;
1347                 default:
1348                         fprintf(stderr, "error: %s: option '-%c' "
1349                                         "unrecognized\n", argv[0], c);
1350                         return CMD_HELP;
1351                 }
1352         }
1353
1354         if (qctl.qc_type)
1355                 qctl.qc_type--;
1356         else /* by default, enable quota for both user & group */
1357                 qctl.qc_type = 0x02;
1358
1359         if (argc == optind)
1360                 return CMD_HELP;
1361
1362         mnt = argv[optind];
1363
1364         rc = llapi_quotactl(mnt, &qctl);
1365         if (rc) {
1366                 if (*obd_type)
1367                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1368                 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1369                 return rc;
1370         }
1371
1372         return 0;
1373 }
1374
1375 static int lfs_quotaoff(int argc, char **argv)
1376 {
1377         int c;
1378         char *mnt;
1379         struct if_quotactl qctl;
1380         char *obd_type = qctl.obd_type;
1381         char *obd_uuid = qctl.obd_uuid.uuid;
1382         int rc;
1383
1384         memset(&qctl, 0, sizeof(qctl));
1385         qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1386
1387         optind = 0;
1388         while ((c = getopt(argc, argv, "ug")) != -1) {
1389                 switch (c) {
1390                 case 'u':
1391                         qctl.qc_type |= 0x01;
1392                         break;
1393                 case 'g':
1394                         qctl.qc_type |= 0x02;
1395                         break;
1396                 default:
1397                         fprintf(stderr, "error: %s: option '-%c' "
1398                                         "unrecognized\n", argv[0], c);
1399                         return CMD_HELP;
1400                 }
1401         }
1402
1403         if (qctl.qc_type)
1404                 qctl.qc_type--;
1405         else /* by default, disable quota for both user & group */
1406                 qctl.qc_type = 0x02;
1407
1408         if (argc == optind)
1409                 return CMD_HELP;
1410
1411         mnt = argv[optind];
1412
1413         rc = llapi_quotactl(mnt, &qctl);
1414         if (rc) {
1415                 if (*obd_type)
1416                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1417                 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1418                 return rc;
1419         }
1420
1421         return 0;
1422 }
1423
1424 #define ARG2INT(nr, str, msg)                                           \
1425 do {                                                                    \
1426         char *endp;                                                     \
1427         nr = strtol(str, &endp, 0);                                     \
1428         if (*endp) {                                                    \
1429                 fprintf(stderr, "error: bad %s: %s\n", msg, str);       \
1430                 return CMD_HELP;                                        \
1431         }                                                               \
1432 } while (0)
1433
1434 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1435
1436 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1437  * returns the value or ULONG_MAX on integer overflow or incorrect format
1438  * Notes:
1439  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1440  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1441  *        3. empty integer value is interpreted as 0
1442  */
1443  
1444 static unsigned long str2sec(const char* timestr) {
1445         const char spec[] = "smhdw";
1446         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1447         unsigned long val = 0;
1448         char *tail;
1449
1450         if (strpbrk(timestr, spec) == NULL) {
1451                 /* no specifiers inside the time string,
1452                    should treat it as an integer value */
1453                 val = strtoul(timestr, &tail, 10);
1454                 return *tail ? ULONG_MAX : val;
1455         }
1456
1457         /* format string is XXwXXdXXhXXmXXs */
1458         while (*timestr) {
1459                 unsigned long v;
1460                 int ind;
1461                 char* ptr;
1462
1463                 v = strtoul(timestr, &tail, 10);
1464                 if (v == ULONG_MAX || *tail == '\0')
1465                         /* value too large (ULONG_MAX or more) 
1466                            or missing specifier */
1467                         goto error;
1468
1469                 ptr = strchr(spec, *tail);
1470                 if (ptr == NULL)
1471                         /* unknown specifier */
1472                         goto error;
1473
1474                 ind = ptr - spec;
1475
1476                 /* check if product will overflow the type */
1477                 if (!(v < ULONG_MAX / mult[ind]))
1478                         goto error;
1479
1480                 ADD_OVERFLOW(val, mult[ind] * v);
1481                 if (val == ULONG_MAX)
1482                         goto error;
1483
1484                 timestr = tail + 1;
1485         }
1486
1487         return val;
1488
1489 error:
1490         return ULONG_MAX;
1491 }
1492
1493 int lfs_setquota(int argc, char **argv)
1494 {
1495         int c;
1496         char *mnt;
1497         struct if_quotactl qctl;
1498         char *obd_type = qctl.obd_type;
1499         char *obd_uuid = qctl.obd_uuid.uuid;
1500         int rc;
1501
1502         memset(&qctl, 0, sizeof(qctl));
1503         qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1504
1505         optind = 0;
1506         while ((c = getopt(argc, argv, "ugt")) != -1) {
1507                 switch (c) {
1508                 case 'u':
1509                         qctl.qc_type |= 0x01;
1510                         break;
1511                 case 'g':
1512                         qctl.qc_type |= 0x02;
1513                         break;
1514                 case 't':
1515                         qctl.qc_cmd = LUSTRE_Q_SETINFO;
1516                         break;
1517                 default:
1518                         fprintf(stderr, "error: %s: option '-%c' "
1519                                         "unrecognized\n", argv[0], c);
1520                         return CMD_HELP;
1521                 }
1522         }
1523
1524         if (qctl.qc_type)
1525                 qctl.qc_type--;
1526
1527         if (qctl.qc_type == UGQUOTA) {
1528                 fprintf(stderr, "error: user and group quotas can't be set "
1529                                 "both\n");
1530                 return CMD_HELP;
1531         }
1532
1533         if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1534                 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1535
1536                 if (optind + 6 != argc)
1537                         return CMD_HELP;
1538
1539                 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1540                 if (rc) {
1541                         fprintf(stderr, "error: find id for name %s failed: %s\n",
1542                                 argv[optind - 1], strerror(errno));
1543                         return CMD_HELP;
1544                 }
1545
1546                 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1547                 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1548                 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1549                 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1550
1551                 dqb->dqb_valid = QIF_LIMITS;
1552         } else {
1553                 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1554
1555                 if (optind + 3 != argc)
1556                         return CMD_HELP;
1557
1558                 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1559                         fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1560                         return CMD_HELP;
1561                 }
1562                 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1563                         fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1564                         return CMD_HELP;
1565                 }
1566         }
1567
1568         mnt = argv[optind];
1569
1570         rc = llapi_quotactl(mnt, &qctl);
1571         if (rc) {
1572                 if (*obd_type)
1573                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1574                 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1575                 return rc;
1576         }
1577
1578         return 0;
1579 }
1580
1581 static inline char *type2name(int check_type)
1582 {
1583         if (check_type == USRQUOTA)
1584                 return "user";
1585         else if (check_type == GRPQUOTA)
1586                 return "group";
1587         else
1588                 return "unknown";
1589 }
1590
1591
1592 /* Converts seconds value into format string
1593  * result is returned in buf
1594  * Notes:
1595  *        1. result is in descenting order: 1w2d3h4m5s
1596  *        2. zero fields are not filled (except for p. 3): 5d1s
1597  *        3. zero seconds value is presented as "0s"
1598  */
1599 static void sec2str(time_t seconds, char *buf)
1600 {
1601         const char spec[] = "smhdw";
1602         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1603         unsigned long c;
1604         char* tail = buf;
1605         int i;
1606
1607         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1608                 c = seconds / mult[i];
1609
1610                 if (c > 0 || (i == 0 && buf == tail))
1611                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1612
1613                 seconds %= mult[i];
1614         }
1615 }
1616
1617
1618 static void diff2str(time_t seconds, char *buf, time_t now)
1619 {
1620
1621         buf[0] = 0;
1622         if (!seconds)
1623                 return;
1624         if (seconds <= now) {
1625                 strcpy(buf, "none");
1626                 return;
1627         }
1628         sec2str(seconds - now, buf);
1629 }
1630
1631 static void print_quota_title(char *name, struct if_quotactl *qctl)
1632 {
1633         printf("Disk quotas for %s %s (%cid %u):\n",
1634                type2name(qctl->qc_type), name,
1635                *type2name(qctl->qc_type), qctl->qc_id);
1636         printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1637                "Filesystem",
1638                "kbytes", "quota", "limit", "grace",
1639                "files", "quota", "limit", "grace");
1640 }
1641
1642 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1643 {
1644         time_t now;
1645
1646         time(&now);
1647
1648         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1649                 int bover = 0, iover = 0;
1650                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1651
1652                 if (dqb->dqb_bhardlimit &&
1653                     toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1654                         bover = 1;
1655                 } else if (dqb->dqb_bsoftlimit &&
1656                            toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1657                         if (dqb->dqb_btime > now) {
1658                                 bover = 2;
1659                         } else {
1660                                 bover = 3;
1661                         }
1662                 }
1663
1664                 if (dqb->dqb_ihardlimit &&
1665                     dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1666                         iover = 1;
1667                 } else if (dqb->dqb_isoftlimit &&
1668                            dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1669                         if (dqb->dqb_btime > now) {
1670                                 iover = 2;
1671                         } else {
1672                                 iover = 3;
1673                         }
1674                 }
1675
1676 #if 0           /* XXX: always print quotas even when no usages */
1677                 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1678 #endif
1679                 {
1680                         char numbuf[3][32];
1681                         char timebuf[40];
1682
1683                         if (strlen(mnt) > 15)
1684                                 printf("%s\n%15s", mnt, "");
1685                         else
1686                                 printf("%15s", mnt);
1687
1688                         if (bover)
1689                                 diff2str(dqb->dqb_btime, timebuf, now);
1690
1691                         sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1692                         sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1693                         sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1694                         printf(" %7s%c %6s %7s %7s",
1695                                numbuf[0], bover ? '*' : ' ', numbuf[1],
1696                                numbuf[2], bover > 1 ? timebuf : "");
1697
1698                         if (iover)
1699                                 diff2str(dqb->dqb_itime, timebuf, now);
1700
1701                         sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1702                         sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1703                         sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1704                         if (!ost_only)
1705                                 printf(" %7s%c %6s %7s %7s",
1706                                        numbuf[0], iover ? '*' : ' ', numbuf[1],
1707                                        numbuf[2], iover > 1 ? timebuf : "");
1708                         printf("\n");
1709                 }
1710         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1711                    qctl->qc_cmd == Q_GETOINFO) {
1712                 char bgtimebuf[40];
1713                 char igtimebuf[40];
1714
1715                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1716                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1717                 printf("Block grace time: %s; Inode grace time: %s\n",
1718                        bgtimebuf, igtimebuf);
1719         }
1720 }
1721
1722 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1723 {
1724         int rc;
1725
1726         /* XXX: this is a flag to mark that only mds quota is wanted */
1727         qctl->qc_dqblk.dqb_valid = 1;
1728         rc = llapi_quotactl(mnt, qctl);
1729         if (rc) {
1730                 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1731                 return;
1732         }
1733         qctl->qc_dqblk.dqb_valid = 0;
1734
1735         print_quota(qctl->obd_uuid.uuid, qctl, 0);
1736 }
1737
1738 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1739 {
1740         DIR *dir;
1741         struct obd_uuid *uuids = NULL, *uuidp;
1742         int obdcount = 1024;
1743         int i, rc;
1744
1745         dir = opendir(mnt);
1746         if (!dir) {
1747                 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1748                 return;
1749         }
1750
1751         uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1752                                           sizeof(struct obd_uuid));
1753         if (uuids == NULL)
1754                 goto out;
1755
1756 retry_get_uuids:
1757         rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1758         if (rc != 0) {
1759                 struct obd_uuid *uuids_temp;
1760
1761                 if (rc == -EOVERFLOW) {
1762                         uuids_temp = realloc(uuids, obdcount *
1763                                              sizeof(struct obd_uuid));
1764                         if (uuids_temp != NULL)
1765                                 goto retry_get_uuids;
1766                         else
1767                                 rc = -ENOMEM;
1768                 }
1769
1770                 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1771                 goto out;
1772         }
1773
1774         for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1775                 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1776
1777                 /* XXX clear this flag to get quota from osts */
1778                 qctl->qc_dqblk.dqb_valid = 0;
1779                 rc = llapi_quotactl(mnt, qctl);
1780                 if (rc) {
1781                         fprintf(stderr, "%s quotactl failed: %s\n",
1782                                 uuidp->uuid, strerror(errno));
1783                         continue;
1784                 }
1785
1786                 print_quota(uuidp->uuid, qctl, 1);
1787         }
1788
1789 out:
1790         closedir(dir);
1791         return;
1792 }
1793
1794 static int lfs_quota(int argc, char **argv)
1795 {
1796         int c;
1797         char *name = NULL, *mnt;
1798         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1799                                     .qc_type = 0x01 };
1800         char *obd_type = qctl.obd_type;
1801         char *obd_uuid = qctl.obd_uuid.uuid;
1802         int rc;
1803
1804         optind = 0;
1805         while ((c = getopt(argc, argv, "ugto:")) != -1) {
1806                 switch (c) {
1807                 case 'u':
1808                         qctl.qc_type = 0x01;
1809                         break;
1810                 case 'g':
1811                         qctl.qc_type = 0x02;
1812                         break;
1813                 case 't':
1814                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
1815                         break;
1816                 case 'o':
1817                         strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1818                         break;
1819                 default:
1820                         fprintf(stderr, "error: %s: option '-%c' "
1821                                         "unrecognized\n", argv[0], c);
1822                         return CMD_HELP;
1823                 }
1824         }
1825
1826         if (qctl.qc_type)
1827                 qctl.qc_type--;
1828
1829
1830         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1831                 if (optind + 2 != argc) {
1832                         fprintf(stderr, "error: missing quota argument(s)\n");
1833                         return CMD_HELP;
1834                 }
1835
1836                 name = argv[optind++];
1837                 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1838                 if (rc) {
1839                         fprintf(stderr,"error: can't find id for name %s: %s\n",
1840                                 name, strerror(errno));
1841                         return CMD_HELP;
1842                 }
1843                 print_quota_title(name, &qctl);
1844         } else if (optind + 1 != argc) {
1845                 fprintf(stderr, "error: missing quota info argument(s)\n");
1846                 return CMD_HELP;
1847         }
1848
1849         mnt = argv[optind];
1850
1851         rc = llapi_quotactl(mnt, &qctl);
1852         if (rc) {
1853                 if (*obd_type)
1854                         fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1855                 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1856                 return rc;
1857         }
1858
1859         if (!name)
1860                 rc = id2name(&name, getuid(), qctl.qc_type);
1861
1862         if (*obd_uuid) {
1863                 mnt = "";
1864                 name = obd_uuid;
1865         }
1866
1867         print_quota(mnt, &qctl, 0);
1868
1869         if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1870                 print_mds_quota(mnt, &qctl);
1871                 print_lov_quota(mnt, &qctl);
1872         }
1873
1874         return 0;
1875 }
1876 #endif /* HAVE_QUOTA_SUPPORT */
1877
1878 static int flushctx_ioctl(char *mp)
1879 {
1880         int fd, rc;
1881
1882         fd = open(mp, O_RDONLY);
1883         if (fd == -1) {
1884                 fprintf(stderr, "flushctx: error open %s: %s\n",
1885                         mp, strerror(errno));
1886                 return -1;
1887         }
1888
1889         rc = ioctl(fd, LL_IOC_FLUSHCTX);
1890         if (rc == -1)
1891                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1892                         mp, strerror(errno));
1893
1894         close(fd);
1895         return rc;
1896 }
1897
1898 static int lfs_flushctx(int argc, char **argv)
1899 {
1900         int     kdestroy = 0, c;
1901         FILE   *proc;
1902         char    procline[PATH_MAX], *line;
1903         int     rc = 0;
1904
1905         optind = 0;
1906         while ((c = getopt(argc, argv, "k")) != -1) {
1907                 switch (c) {
1908                 case 'k':
1909                         kdestroy = 1;
1910                         break;
1911                 default:
1912                         fprintf(stderr, "error: %s: option '-%c' "
1913                                         "unrecognized\n", argv[0], c);
1914                         return CMD_HELP;
1915                 }
1916         }
1917
1918         if (kdestroy)
1919                 system("kdestroy > /dev/null");
1920
1921         if (optind >= argc) {
1922                 /* flush for all mounted lustre fs. */
1923                 proc = fopen("/proc/mounts", "r");
1924                 if (!proc) {
1925                         fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1926                                 argv[0]);
1927                         return -1;
1928                 }
1929
1930                 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1931                         char dev[PATH_MAX];
1932                         char mp[PATH_MAX];
1933                         char fs[PATH_MAX];
1934
1935                         if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1936                                 fprintf(stderr, "%s: unexpected format in "
1937                                                 "/proc/mounts\n",
1938                                         argv[0]);
1939                                 return -1;
1940                         }
1941
1942                         if (strcmp(fs, "lustre") != 0)
1943                                 continue;
1944                         /* we use '@' to determine it's a client. are there
1945                          * any other better way?
1946                          */
1947                         if (strchr(dev, '@') == NULL)
1948                                 continue;
1949
1950                         if (flushctx_ioctl(mp))
1951                                 rc = -1;
1952                 }
1953         } else {
1954                 /* flush fs as specified */
1955                 while (optind < argc) {
1956                         if (flushctx_ioctl(argv[optind++]))
1957                                 rc = -1;
1958                 }
1959         }
1960
1961         return rc;
1962 }
1963
1964 static int lfs_lsetfacl(int argc, char **argv)
1965 {
1966         argv[0]++;
1967         return(llapi_lsetfacl(argc, argv));
1968 }
1969
1970 static int lfs_lgetfacl(int argc, char **argv)
1971 {
1972         argv[0]++;
1973         return(llapi_lgetfacl(argc, argv));
1974 }
1975
1976 static int lfs_rsetfacl(int argc, char **argv)
1977 {
1978         argv[0]++;
1979         return(llapi_rsetfacl(argc, argv));
1980 }
1981
1982 static int lfs_rgetfacl(int argc, char **argv)
1983 {
1984         argv[0]++;
1985         return(llapi_rgetfacl(argc, argv));
1986 }
1987
1988 static int lfs_cp(int argc, char **argv)
1989 {
1990         return(llapi_cp(argc, argv));
1991 }
1992
1993 static int lfs_ls(int argc, char **argv)
1994 {
1995         return(llapi_ls(argc, argv));
1996 }
1997
1998 int main(int argc, char **argv)
1999 {
2000         int rc;
2001
2002         setlinebuf(stdout);
2003
2004         ptl_initialize(argc, argv);
2005         if (obd_initialize(argc, argv) < 0)
2006                 exit(2);
2007         if (dbg_initialize(argc, argv) < 0)
2008                 exit(3);
2009
2010         Parser_init("lfs > ", cmdlist);
2011
2012         if (argc > 1) {
2013                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2014         } else {
2015                 rc = Parser_commands();
2016         }
2017
2018         obd_finalize(argc, argv);
2019         return rc;
2020 }