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