1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
56 #include <sys/types.h>
58 #include <sys/param.h>
63 #ifdef HAVE_SYS_QUOTA_H
64 #include <sys/quota.h>
67 #include <lnet/api-support.h>
68 #include <lnet/lnetctl.h>
70 #include <liblustre.h>
71 #include <lustre/lustre_idl.h>
72 #include <lustre/liblustreapi.h>
73 #include <lustre/lustre_user.h>
74 #include <lustre_quota.h>
79 unsigned int libcfs_subsystem_debug = 0;
82 static int lfs_setstripe(int argc, char **argv);
83 static int lfs_find(int argc, char **argv);
84 static int lfs_getstripe(int argc, char **argv);
85 static int lfs_osts(int argc, char **argv);
86 static int lfs_df(int argc, char **argv);
87 static int lfs_check(int argc, char **argv);
88 static int lfs_catinfo(int argc, char **argv);
89 #ifdef HAVE_SYS_QUOTA_H
90 static int lfs_quotachown(int argc, char **argv);
91 static int lfs_quotacheck(int argc, char **argv);
92 static int lfs_quotaon(int argc, char **argv);
93 static int lfs_quotaoff(int argc, char **argv);
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_quotainv(int argc, char **argv);
98 static int lfs_join(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
101 /* all avaialable commands */
102 command_t cmdlist[] = {
103 {"setstripe", lfs_setstripe, 0,
104 "Create a new file with a specific striping pattern or\n"
105 "set the default striping pattern on an existing directory or\n"
106 "delete the default striping pattern from an existing directory\n"
107 "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
108 " [--count|-c stripe_count] [--pool|-p pool_name]\n"
111 " setstripe -d <dir> (to delete default striping)\n"
112 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
113 "\t Can be specified with k, m or g (in KB, MB and GB\n"
115 "\tstart_ost: OST index of first stripe (-1 filesystem default)\n"
116 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
117 "\tpool_name: Name of OST pool"},
118 {"getstripe", lfs_getstripe, 0,
119 "To list the striping info for a given file or files in a\n"
120 "directory or recursively for all files in a directory tree.\n"
121 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
122 " [--recursive | -r] <dir|file> ..."},
123 {"pool_list", lfs_poollist, 0,
124 "List pools or pool OSTs\n"
125 "usage: pool_list <fsname>[.<poolname>] | <pathname>\n"},
126 {"find", lfs_find, 0,
127 "To find files that match given parameters recursively in a directory tree.\n"
128 "usage: find <dir|file> ... \n"
129 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
130 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
131 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
132 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
133 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
134 " [[!] --pool <name>]\n"
135 "\t !: used before an option indicates 'NOT' the requested attribute\n"
136 "\t -: used before an value indicates 'AT MOST' the requested value\n"
137 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
138 {"check", lfs_check, 0,
139 "Display the status of MDS or OSTs (as specified in the command)\n"
140 "or all the servers (MDS and OSTs).\n"
141 "usage: check <osts|mds|servers>"},
142 {"catinfo", lfs_catinfo, 0,
143 "Show information of specified type logs.\n"
144 "usage: catinfo {keyword} [node name]\n"
145 "\tkeywords are one of followings: config, deletions.\n"
146 "\tnode name must be provided when use keyword config."},
147 {"join", lfs_join, 0,
148 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
149 "usage: join <filename_A> <filename_B>\n"},
150 {"osts", lfs_osts, 0, "osts"},
152 "report filesystem disk space usage or inodes usage"
154 "Usage: df [-i] [-h] [path]"},
155 #ifdef HAVE_SYS_QUOTA_H
156 {"quotachown",lfs_quotachown, 0,
157 "Change files' owner or group on the specified filesystem.\n"
158 "usage: quotachown [-i] <filesystem>\n"
159 "\t-i: ignore error if file is not exist\n"},
160 {"quotacheck", lfs_quotacheck, 0,
161 "Scan the specified filesystem for disk usage, and create,\n"
162 "or update quota files.\n"
163 "usage: quotacheck [ -ug ] <filesystem>"},
164 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
165 "usage: quotaon [ -ugf ] <filesystem>"},
166 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
167 "usage: quotaoff [ -ug ] <filesystem>"},
168 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
169 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
170 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>\n"
171 " setquota [ -u | --user | -g | --group ] <name>\n"
172 " [--block-softlimit <block-softlimit>]\n"
173 " [--block-hardlimit <block-hardlimit>]\n"
174 " [--inode-softlimit <inode-softlimit>]\n"
175 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
176 " setquota [-t] [ -u | --user | -g | --group ]\n"
177 " [--block-grace <block-grace>]\n"
178 " [--inode-grace <inode-grace>] <filesystem>\n"
179 " -b can be used instead of --block-softlimit/--block-grace\n"
180 " -B can be used instead of --block-hardlimit\n"
181 " -i can be used instead of --inode-softlimit/--inode-grace\n"
182 " -I can be used instead of --inode-hardlimit"},
183 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
184 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
185 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
186 "usage: quotainv [-u|-g] <filesystem>"},
188 {"help", Parser_help, 0, "help"},
189 {"exit", Parser_quit, 0, "quit"},
190 {"quit", Parser_quit, 0, "quit"},
194 static int isnumber(const char *str)
198 if (str[0] != '-' && !isdigit(str[0]))
201 for (ptr = str + 1; *ptr != '\0'; ptr++) {
210 static int lfs_setstripe(int argc, char **argv)
214 unsigned long long st_size;
215 int st_offset, st_count;
219 char *stripe_size_arg = NULL;
220 char *stripe_off_arg = NULL;
221 char *stripe_count_arg = NULL;
222 char *pool_name_arg = NULL;
223 unsigned long long size_units;
225 struct option long_opts[] = {
226 {"size", required_argument, 0, 's'},
227 {"count", required_argument, 0, 'c'},
228 {"index", required_argument, 0, 'i'},
229 {"offset", required_argument, 0, 'o'},
230 {"pool", required_argument, 0, 'p'},
231 {"delete", no_argument, 0, 'd'},
239 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
240 if (argc == 5 && argv[1][0] != '-' &&
241 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
242 fprintf(stderr, "warning: deprecated usage of setstripe "
243 "positional parameters. Use -c, -i, -s instead.\n");
244 /* for compatibility with the existing positional parameter
247 stripe_size_arg = argv[2];
248 stripe_off_arg = argv[3];
249 stripe_count_arg = argv[4];
253 #warning "remove obsolete positional parameter code"
257 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
258 long_opts, NULL)) >= 0) {
264 stripe_count_arg = optarg;
267 /* delete the default striping pattern */
272 stripe_off_arg = optarg;
275 stripe_size_arg = optarg;
278 pool_name_arg = optarg;
283 fprintf(stderr, "error: %s: option '%s' "
285 argv[0], argv[optind - 1]);
290 fname = argv[optind];
293 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
294 stripe_count_arg != NULL || pool_name_arg != NULL)) {
295 fprintf(stderr, "error: %s: cannot specify -d with "
296 "-s, -c -o or -p options\n",
302 if (optind == argc) {
303 fprintf(stderr, "error: %s: missing filename|dirname\n",
308 /* get the stripe size */
309 if (stripe_size_arg != NULL) {
310 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
312 fprintf(stderr, "error: %s: bad size '%s'\n",
313 argv[0], stripe_size_arg);
317 /* get the stripe offset */
318 if (stripe_off_arg != NULL) {
319 st_offset = strtoul(stripe_off_arg, &end, 0);
321 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
322 argv[0], stripe_off_arg);
326 /* get the stripe count */
327 if (stripe_count_arg != NULL) {
328 st_count = strtoul(stripe_count_arg, &end, 0);
330 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
331 argv[0], stripe_count_arg);
337 result = llapi_file_create_pool(fname, st_size, st_offset,
338 st_count, 0, pool_name_arg);
340 fprintf(stderr,"error: %s: create stripe file '%s' "
341 "failed\n", argv[0], fname);
344 fname = argv[++optind];
345 } while (fname != NULL);
350 static int lfs_poollist(int argc, char **argv)
355 return llapi_poollist(argv[1]);
358 static int set_time(time_t *time, time_t *set, char *str)
365 else if (str[0] == '-')
371 t = strtol(str, NULL, 0);
372 if (*time < t * 24 * 60 * 60) {
375 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
379 *set = *time - t * 24 * 60 * 60;
386 static int name2id(unsigned int *id, char *name, int type)
389 struct passwd *entry;
391 if (!(entry = getpwnam(name))) {
401 if (!(entry = getgrnam(name))) {
413 static int id2name(char **name, unsigned int id, int type)
416 struct passwd *entry;
418 if (!(entry = getpwuid(id))) {
424 *name = entry->pw_name;
428 if (!(entry = getgrgid(id))) {
434 *name = entry->gr_name;
440 #define FIND_POOL_OPT 3
441 static int lfs_find(int argc, char **argv)
446 struct find_param param = { .maxdepth = -1 };
448 struct option long_opts[] = {
449 /* New find options. */
450 {"atime", required_argument, 0, 'A'},
451 {"ctime", required_argument, 0, 'C'},
452 {"mtime", required_argument, 0, 'M'},
453 {"maxdepth", required_argument, 0, 'D'},
454 {"gid", required_argument, 0, 'g'},
455 {"group", required_argument, 0, 'G'},
456 {"uid", required_argument, 0, 'u'},
457 {"user", required_argument, 0, 'U'},
458 {"name", required_argument, 0, 'n'},
459 /* no short option for pool, p/P already used */
460 {"pool", required_argument, 0, FIND_POOL_OPT},
461 /* --obd is considered as a new option. */
462 {"obd", required_argument, 0, 'O'},
463 {"ost", required_argument, 0, 'O'},
464 {"print", no_argument, 0, 'P'},
465 {"print0", no_argument, 0, 'p'},
466 /* Old find options. */
467 {"quiet", no_argument, 0, 'q'},
468 {"recursive", no_argument, 0, 'r'},
469 {"size", required_argument, 0, 's'},
470 {"type", required_argument, 0, 't'},
471 {"verbose", no_argument, 0, 'v'},
485 /* when getopt_long_only() hits '!' it returns 1 and puts "!" in optarg */
486 while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
487 long_opts, NULL)) >= 0) {
492 /* '!' is part of option */
493 /* when getopt_long_only() finds a string which is not
494 * an option nor a known option argument it returns 1
495 * in that case if we already have found pathstart and pathend
496 * (i.e. we have the list of pathnames),
497 * the only supported value is "!"
499 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
500 if (!isoption && pathend != -1) {
501 fprintf(stderr, "err: %s: filename|dirname must either "
502 "precede options or follow options\n",
506 if (!isoption && pathstart == -1)
507 pathstart = optind - 1;
508 if (isoption && pathstart != -1 && pathend == -1) {
509 pathend = optind - 2;
510 if ((c == 1 && strcmp(optarg, "!") == 0) ||
511 c == 'P' || c == 'p' || c == 'O' ||
512 c == 'q' || c == 'r' || c == 'v')
513 pathend = optind - 1;
520 /* unknown; opt is "!" or path component,
521 * checking done above.
523 if (strcmp(optarg, "!") == 0)
527 xtime = ¶m.atime;
528 xsign = ¶m.asign;
531 xtime = ¶m.ctime;
532 xsign = ¶m.csign;
536 xtime = ¶m.mtime;
537 xsign = ¶m.msign;
541 if (optarg[0] == '-')
543 else if (optarg[0] == '+')
552 ret = set_time(&t, xtime, optarg);
560 param.maxdepth = strtol(optarg, 0, 0);
564 param.gid = strtol(optarg, &endptr, 10);
565 if (optarg == endptr) {
566 fprintf(stderr, "Bad gid: %s\n", optarg);
569 param.exclude_gid = !!neg_opt;
574 param.gid = strtol(optarg, &endptr, 10);
575 if (optarg == endptr) {
576 ret = name2id(¶m.gid, optarg, GROUP);
578 fprintf(stderr, "Group/GID: %s cannot "
579 "be found.\n", optarg);
583 param.exclude_gid = !!neg_opt;
588 param.uid = strtol(optarg, &endptr, 10);
589 if (optarg == endptr) {
590 fprintf(stderr, "Bad uid: %s\n", optarg);
593 param.exclude_uid = !!neg_opt;
598 param.uid = strtol(optarg, &endptr, 10);
599 if (optarg == endptr) {
600 ret = name2id(¶m.uid, optarg, USER);
602 fprintf(stderr, "User/UID: %s cannot "
603 "be found.\n", optarg);
607 param.exclude_uid = !!neg_opt;
612 if (strlen(optarg) > LOV_MAXPOOLNAME) {
614 "Pool name %s is too long"
615 " (max is %d)\n", optarg,
619 /* we do check for empty pool because empty pool
620 * is used to find V1 lov attributes */
621 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
622 param.poolname[LOV_MAXPOOLNAME] = '\0';
623 param.exclude_pool = !!neg_opt;
624 param.check_pool = 1;
628 param.pattern = (char *)optarg;
629 param.exclude_pattern = !!neg_opt;
632 char *buf, *token, *next, *p;
635 len = strlen((char *)optarg);
639 strcpy(buf, (char *)optarg);
641 if (param.num_alloc_obds == 0) {
642 param.obduuid = malloc(FIND_MAX_OSTS *
643 sizeof(struct obd_uuid));
644 if (param.obduuid == NULL)
646 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
649 for (token = buf; token && *token; token = next) {
650 p = strchr(token, ',');
656 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
680 param.exclude_type = !!neg_opt;
682 case 'b': param.type = S_IFBLK; break;
683 case 'c': param.type = S_IFCHR; break;
684 case 'd': param.type = S_IFDIR; break;
685 case 'f': param.type = S_IFREG; break;
686 case 'l': param.type = S_IFLNK; break;
687 case 'p': param.type = S_IFIFO; break;
688 case 's': param.type = S_IFSOCK; break;
689 #ifdef S_IFDOOR /* Solaris only */
690 case 'D': param.type = S_IFDOOR; break;
692 default: fprintf(stderr, "error: %s: bad type '%s'\n",
699 if (optarg[0] == '-')
701 else if (optarg[0] == '+')
710 if (optarg[0] == '+')
711 param.size_sign = -1;
712 else if (optarg[0] == '-')
713 param.size_sign = +1;
717 ret = parse_size(optarg, ¶m.size,
718 ¶m.size_units, 0);
720 fprintf(stderr,"error: bad size '%s'\n",
724 param.size_check = 1;
734 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
735 argv[0], argv[optind - 1]);
740 if (pathstart == -1) {
741 fprintf(stderr, "error: %s: no filename|pathname\n",
744 } else if (pathend == -1) {
752 static int deprecated_warning;
753 if (!deprecated_warning) {
754 fprintf(stderr, "lfs find: -q, -r, -v options "
755 "deprecated. Use 'lfs getstripe' instead.\n");
756 deprecated_warning = 1;
758 if (!param.recursive && param.maxdepth == -1)
764 ret = llapi_find(argv[pathstart], ¶m);
766 ret = llapi_getstripe(argv[pathstart], ¶m);
767 } while (++pathstart < pathend && !ret);
770 fprintf(stderr, "error: %s failed for %s.\n",
771 argv[0], argv[optind - 1]);
773 if (param.obduuid && param.num_alloc_obds)
779 static int lfs_getstripe(int argc, char **argv)
781 struct option long_opts[] = {
783 {"quiet", 0, 0, 'q'},
784 {"recursive", 0, 0, 'r'},
785 {"verbose", 0, 0, 'v'},
788 char short_opts[] = "hO:qrv";
790 struct find_param param = { 0 };
793 while ((c = getopt_long(argc, argv, short_opts,
794 long_opts, NULL)) != -1) {
799 "error: %s: only one obduuid allowed",
803 param.obduuid = (struct obd_uuid *)optarg;
819 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
820 argv[0], argv[optind - 1]);
828 param.maxdepth = param.recursive ? -1 : 1;
831 rc = llapi_getstripe(argv[optind], ¶m);
832 } while (++optind < argc && !rc);
835 fprintf(stderr, "error: %s failed for %s.\n",
836 argv[0], argv[optind - 1]);
840 static int lfs_osts(int argc, char **argv)
842 char mntdir[PATH_MAX] = {'\0'};
843 struct find_param param;
844 int index = 0, rc = 0;
849 while (llapi_search_mounts(NULL, index++, mntdir, NULL) == 0) {
850 memset(¶m, 0, sizeof(param));
851 rc = llapi_getstripe(mntdir, ¶m);
853 fprintf(stderr, "error: %s: failed on %s\n",
856 memset(mntdir, 0, PATH_MAX);
862 #define COOK(value) \
865 while (value > 1024) { \
879 static int showdf(char *mntdir, struct obd_statfs *stat,
880 char *uuid, int ishow, int cooked,
881 char *type, int index, int rc)
883 long long avail, used, total;
885 char *suffix = "KMGTPEZY";
886 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
887 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
895 avail = stat->os_ffree;
896 used = stat->os_files - stat->os_ffree;
897 total = stat->os_files;
899 int shift = cooked ? 0 : 10;
901 avail = (stat->os_bavail * stat->os_bsize) >> shift;
902 used = ((stat->os_blocks - stat->os_bfree) *
903 stat->os_bsize) >> shift;
904 total = (stat->os_blocks * stat->os_bsize) >> shift;
908 ratio = (double)used / (double)total;
914 cook_val = (double)total;
917 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
919 sprintf(tbuf, CDF, total);
921 cook_val = (double)used;
924 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
926 sprintf(ubuf, CDF, used);
928 cook_val = (double)avail;
931 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
933 sprintf(abuf, CDF, avail);
935 sprintf(tbuf, CDF, total);
936 sprintf(ubuf, CDF, used);
937 sprintf(abuf, CDF, avail);
940 sprintf(rbuf, RDF, (int)(ratio * 100));
941 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
942 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
944 printf("[%s:%d]\n", type, index);
950 printf(UUF": inactive device\n", uuid);
953 printf(UUF": %s\n", uuid, strerror(-rc));
960 static int mntdf(char *mntdir, int ishow, int cooked)
962 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
963 struct obd_uuid uuid_buf;
968 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
969 "UUID", "Inodes", "IUsed", "IFree",
970 "IUse%", "Mounted on");
972 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
973 "UUID", cooked ? "bytes" : "1K-blocks",
974 "Used", "Available", "Use%", "Mounted on");
976 for (index = 0; ; index++) {
977 memset(&stat_buf, 0, sizeof(struct obd_statfs));
978 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
979 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
980 &stat_buf, &uuid_buf);
987 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
988 rc == -ENODATA || rc == 0) {
989 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
990 ishow, cooked, "MDT", index, rc);
993 "error: llapi_obd_statfs(%s): %s (%d)\n",
994 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
998 sum.os_ffree += stat_buf.os_ffree;
999 sum.os_files += stat_buf.os_files;
1003 for (index = 0; ; index++) {
1004 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1005 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1006 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1007 &stat_buf, &uuid_buf);
1014 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1015 rc == -ENODATA || rc == 0) {
1016 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1017 ishow, cooked, "OST", index, rc);
1020 "error: llapi_obd_statfs failed: %s (%d)\n",
1025 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1026 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1027 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1032 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1037 static int lfs_df(int argc, char **argv)
1040 char *mntdir = NULL;
1041 int ishow = 0, cooked = 0;
1045 while ((c = getopt(argc, argv, "ih")) != -1) {
1058 path = argv[optind];
1060 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1061 fprintf(stderr, "error: cannot allocate %d bytes\n",
1065 memset(mntdir, 0, PATH_MAX);
1068 char rpath[PATH_MAX] = {'\0'};
1070 if (!realpath(path, rpath)) {
1072 fprintf(stderr, "error: invalid path '%s': %s\n",
1073 path, strerror(-rc));
1077 rc = llapi_search_mounts(rpath, 0, mntdir, NULL);
1078 if (rc == 0 && mntdir[0] != '\0') {
1079 rc = mntdf(mntdir, ishow, cooked);
1085 while (llapi_search_mounts(NULL, index++, mntdir, NULL) == 0) {
1086 rc = mntdf(mntdir, ishow, cooked);
1097 static int lfs_check(int argc, char **argv)
1101 char mntdir[PATH_MAX] = {'\0'};
1109 obd_types[0] = obd_type1;
1110 obd_types[1] = obd_type2;
1112 if (strcmp(argv[1], "osts") == 0) {
1113 strcpy(obd_types[0], "osc");
1114 } else if (strcmp(argv[1], "mds") == 0) {
1115 strcpy(obd_types[0], "mdc");
1116 } else if (strcmp(argv[1], "servers") == 0) {
1118 strcpy(obd_types[0], "osc");
1119 strcpy(obd_types[1], "mdc");
1121 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1126 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1127 if (rc < 0 || mntdir[0] == '\0') {
1128 fprintf(stderr, "No suitable Lustre mount found\n");
1132 rc = llapi_target_iterate(num_types, obd_types,
1133 mntdir, llapi_ping_target);
1136 fprintf(stderr, "error: %s: %s status failed\n",
1143 static int lfs_catinfo(int argc, char **argv)
1145 char mntdir[PATH_MAX] = {'\0'};
1148 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1151 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1154 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1155 if (rc == 0 && mntdir[0] != '\0') {
1157 rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1159 rc = llapi_catinfo(mntdir, argv[1], NULL);
1161 fprintf(stderr, "no lustre_lite mounted.\n");
1168 int lfs_join(int argc, char **argv)
1170 char *name_head, *name_tail;
1176 name_head = argv[1];
1177 fd = open(name_head, O_WRONLY);
1179 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1183 size = lseek(fd, 0, SEEK_END);
1184 if (size % JOIN_FILE_ALIGN) {
1185 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1186 name_head, (long long)size, JOIN_FILE_ALIGN);
1190 name_tail = argv[2];
1191 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1195 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1201 #ifdef HAVE_SYS_QUOTA_H
1202 static int lfs_quotachown(int argc, char **argv)
1209 while ((c = getopt(argc, argv, "i")) != -1) {
1215 fprintf(stderr, "error: %s: option '-%c' "
1216 "unrecognized\n", argv[0], c);
1222 rc = llapi_quotachown(argv[optind], flag);
1224 fprintf(stderr,"error: change file owner/group failed.\n");
1228 static int lfs_quotacheck(int argc, char **argv)
1230 int c, check_type = 0;
1232 struct if_quotacheck qchk;
1233 struct if_quotactl qctl;
1234 char *obd_type = (char *)qchk.obd_type;
1237 memset(&qchk, 0, sizeof(qchk));
1240 while ((c = getopt(argc, argv, "ug")) != -1) {
1249 fprintf(stderr, "error: %s: option '-%c' "
1250 "unrecognized\n", argv[0], c);
1257 else /* do quotacheck for both user & group quota by default */
1265 memset(&qctl, 0, sizeof(qctl));
1266 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1267 qctl.qc_type = check_type;
1268 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1269 * take this parameter into account */
1270 rc = llapi_quotactl(mnt, &qctl);
1272 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1276 rc = llapi_quotacheck(mnt, check_type);
1278 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1282 rc = llapi_poll_quotacheck(mnt, &qchk);
1285 fprintf(stderr, "%s %s ", obd_type,
1286 obd_uuid2str(&qchk.obd_uuid));
1287 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1291 memset(&qctl, 0, sizeof(qctl));
1292 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1293 qctl.qc_type = check_type;
1294 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1295 * take this parameter into account */
1296 rc = llapi_quotactl(mnt, &qctl);
1299 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1300 obd_uuid2str(&qctl.obd_uuid));
1301 fprintf(stderr, "%s turn on quota failed: %s\n",
1302 argv[0], strerror(errno));
1309 static int lfs_quotaon(int argc, char **argv)
1313 struct if_quotactl qctl;
1314 char *obd_type = (char *)qctl.obd_type;
1317 memset(&qctl, 0, sizeof(qctl));
1318 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1319 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1320 * take this parameter into account */
1323 while ((c = getopt(argc, argv, "ugf")) != -1) {
1326 qctl.qc_type |= 0x01;
1329 qctl.qc_type |= 0x02;
1332 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1335 fprintf(stderr, "error: %s: option '-%c' "
1336 "unrecognized\n", argv[0], c);
1343 else /* by default, enable quota for both user & group */
1344 qctl.qc_type = 0x02;
1351 rc = llapi_quotactl(mnt, &qctl);
1354 fprintf(stderr, "%s %s ", obd_type,
1355 obd_uuid2str(&qctl.obd_uuid));
1356 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1363 static int lfs_quotaoff(int argc, char **argv)
1367 struct if_quotactl qctl;
1368 char *obd_type = (char *)qctl.obd_type;
1371 memset(&qctl, 0, sizeof(qctl));
1372 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1375 while ((c = getopt(argc, argv, "ug")) != -1) {
1378 qctl.qc_type |= 0x01;
1381 qctl.qc_type |= 0x02;
1384 fprintf(stderr, "error: %s: option '-%c' "
1385 "unrecognized\n", argv[0], c);
1392 else /* by default, disable quota for both user & group */
1393 qctl.qc_type = 0x02;
1400 rc = llapi_quotactl(mnt, &qctl);
1401 if (rc == -1 && errno == ESRCH) {
1402 fprintf(stderr, "\n%s quotas are not enabled.\n",
1403 qctl.qc_type == 0x00 ? "user" : "group");
1408 fprintf(stderr, "%s %s ", obd_type,
1409 obd_uuid2str(&qctl.obd_uuid));
1410 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1417 static int lfs_quotainv(int argc, char **argv)
1421 struct if_quotactl qctl;
1424 memset(&qctl, 0, sizeof(qctl));
1425 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1428 while ((c = getopt(argc, argv, "ugf")) != -1) {
1431 qctl.qc_type |= 0x01;
1434 qctl.qc_type |= 0x02;
1437 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1440 fprintf(stderr, "error: %s: option '-%c' "
1441 "unrecognized\n", argv[0], c);
1448 else /* by default, invalidate quota for both user & group */
1449 qctl.qc_type = 0x02;
1456 rc = llapi_quotactl(mnt, &qctl);
1458 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1465 #define ARG2INT(nr, str, msg) \
1468 nr = strtol(str, &endp, 0); \
1470 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1475 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1477 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1478 * returns the value or ULONG_MAX on integer overflow or incorrect format
1480 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1481 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1482 * 3. empty integer value is interpreted as 0
1485 static unsigned long str2sec(const char* timestr) {
1486 const char spec[] = "smhdw";
1487 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1488 unsigned long val = 0;
1491 if (strpbrk(timestr, spec) == NULL) {
1492 /* no specifiers inside the time string,
1493 should treat it as an integer value */
1494 val = strtoul(timestr, &tail, 10);
1495 return *tail ? ULONG_MAX : val;
1498 /* format string is XXwXXdXXhXXmXXs */
1504 v = strtoul(timestr, &tail, 10);
1505 if (v == ULONG_MAX || *tail == '\0')
1506 /* value too large (ULONG_MAX or more)
1507 or missing specifier */
1510 ptr = strchr(spec, *tail);
1512 /* unknown specifier */
1517 /* check if product will overflow the type */
1518 if (!(v < ULONG_MAX / mult[ind]))
1521 ADD_OVERFLOW(val, mult[ind] * v);
1522 if (val == ULONG_MAX)
1534 #define ARG2ULL(nr, str, defscale) \
1536 unsigned long long limit, units = 0; \
1539 rc = parse_size(str, &limit, &units, 1); \
1541 fprintf(stderr, "error: bad limit value %s\n", str); \
1544 nr = ((units == 0) ? (defscale) : 1) * limit; \
1547 static inline int has_times_option(int argc, char **argv)
1551 for (i = 1; i < argc; i++)
1552 if (!strcmp(argv[i], "-t"))
1558 int lfs_setquota_times(int argc, char **argv)
1561 struct if_quotactl qctl;
1562 char *mnt, *obd_type = (char *)qctl.obd_type;
1563 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1564 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1565 struct option long_opts[] = {
1566 {"user", no_argument, 0, 'u'},
1567 {"group", no_argument, 0, 'g'},
1568 {"block-grace", required_argument, 0, 'b'},
1569 {"inode-grace", required_argument, 0, 'i'},
1570 {"times", no_argument, 0, 't'},
1574 memset(&qctl, 0, sizeof(qctl));
1575 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1576 qctl.qc_type = UGQUOTA;
1579 /* compatibility syntax: setquota -t -[u|g] t1 t2 mnt */
1580 if (argc == 6 && !strcmp(argv[1], "-t") &&
1581 (!strcmp(argv[2], "-u") || !strcmp(argv[2], "-g")) &&
1582 argv[3][0] != '-' && argv[4][0] != '-') {
1583 fprintf(stderr, "warning: using compatibility syntax, it may not"
1584 " be available in future releases!\n");
1586 qctl.qc_type = !strcmp(argv[2], "-u") ? USRQUOTA : GRPQUOTA;
1588 if ((dqi->dqi_bgrace = str2sec(argv[3])) == ULONG_MAX) {
1589 fprintf(stderr, "error: bad block-grace: %s\n", argv[3]);
1592 if ((dqi->dqi_igrace = str2sec(argv[4])) == ULONG_MAX) {
1593 fprintf(stderr, "error: bad inode-grace: %s\n", argv[4]);
1596 dqb->dqb_valid = QIF_TIMES;
1597 mnt = argv[argc - 1];
1603 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1607 if (qctl.qc_type != UGQUOTA) {
1608 fprintf(stderr, "error: -u and -g can't be used "
1609 "more than once\n");
1612 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1615 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1616 fprintf(stderr, "error: bad block-grace: %s\n",
1620 dqb->dqb_valid |= QIF_BTIME;
1623 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1624 fprintf(stderr, "error: bad inode-grace: %s\n",
1628 dqb->dqb_valid |= QIF_ITIME;
1630 case 't': /* Yes, of course! */
1632 default: /* getopt prints error message for us when opterr != 0 */
1637 if (qctl.qc_type == UGQUOTA) {
1638 fprintf(stderr, "error: neither -u nor -g specified\n");
1642 if (optind != argc - 1) {
1643 fprintf(stderr, "error: unexpected parameters encountered\n");
1652 rc = llapi_quotactl(mnt, &qctl);
1655 fprintf(stderr, "%s %s ", obd_type,
1656 obd_uuid2str(&qctl.obd_uuid));
1657 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1664 #define BSLIMIT (1 << 0)
1665 #define BHLIMIT (1 << 1)
1666 #define ISLIMIT (1 << 2)
1667 #define IHLIMIT (1 << 3)
1669 int lfs_setquota(int argc, char **argv)
1672 struct if_quotactl qctl;
1673 char *mnt, *obd_type = (char *)qctl.obd_type;
1674 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1675 struct option long_opts[] = {
1676 {"user", required_argument, 0, 'u'},
1677 {"group", required_argument, 0, 'g'},
1678 {"block-softlimit", required_argument, 0, 'b'},
1679 {"block-hardlimit", required_argument, 0, 'B'},
1680 {"inode-softlimit", required_argument, 0, 'i'},
1681 {"inode-hardlimit", required_argument, 0, 'I'},
1684 unsigned limit_mask = 0;
1686 if (has_times_option(argc, argv))
1687 return lfs_setquota_times(argc, argv);
1689 memset(&qctl, 0, sizeof(qctl));
1690 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1691 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1692 * so it can be used as a marker that qc_type
1693 * isn't reinitialized from command line */
1696 /* compatibility syntax: [-u|-g] <user|group> b B i I mount
1697 * will be removed in the future */
1698 if (argc == 8 && (!strcmp(argv[1], "-u") || !strcmp(argv[1], "-g")) &&
1699 argv[3][0] != '-' && argv[4][0] != '-' && argv[5][0] != '-' &&
1700 argv[6][0] != '-') {
1701 fprintf(stderr, "warning: using compatibility syntax, it may not"
1702 " be available in future releases!\n");
1704 qctl.qc_type = !strcmp(argv[1], "-u") ? USRQUOTA : GRPQUOTA;
1705 rc = name2id(&qctl.qc_id, argv[2],
1706 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1708 fprintf(stderr, "error: unknown id %s\n", argv[2]);
1712 ARG2ULL(dqb->dqb_bsoftlimit, argv[3], 1024);
1713 dqb->dqb_bsoftlimit >>= 10;
1714 ARG2ULL(dqb->dqb_bhardlimit, argv[4], 1024);
1715 dqb->dqb_bhardlimit >>= 10;
1716 ARG2ULL(dqb->dqb_isoftlimit, argv[5], 1);
1717 ARG2ULL(dqb->dqb_ihardlimit, argv[6], 1);
1719 dqb->dqb_valid = QIF_LIMITS;
1720 mnt = argv[argc - 1];
1726 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1730 if (qctl.qc_type != UGQUOTA) {
1731 fprintf(stderr, "error: -u and -g can't be used"
1732 " more than once\n");
1735 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1736 rc = name2id(&qctl.qc_id, optarg,
1737 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1739 fprintf(stderr, "error: unknown id %s\n",
1745 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1746 dqb->dqb_bsoftlimit >>= 10;
1747 limit_mask |= BSLIMIT;
1750 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1751 dqb->dqb_bhardlimit >>= 10;
1752 limit_mask |= BHLIMIT;
1755 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1756 limit_mask |= ISLIMIT;
1759 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1760 limit_mask |= IHLIMIT;
1762 default: /* getopt prints error message for us when opterr != 0 */
1767 if (qctl.qc_type == UGQUOTA) {
1768 fprintf(stderr, "error: neither -u nor -g are specified\n");
1772 if (limit_mask == 0) {
1773 fprintf(stderr, "error: at least one limit must be specified\n");
1777 if (optind != argc - 1) {
1778 fprintf(stderr, "error: unexpected parameters encountered\n");
1784 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1785 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1786 /* sigh, we can't just set blimits/ilimits */
1787 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1788 .qc_type = qctl.qc_type,
1789 .qc_id = qctl.qc_id};
1791 rc = llapi_quotactl(mnt, &tmp_qctl);
1793 fprintf(stderr, "error: getquota failed\n");
1797 if (!(limit_mask & BHLIMIT))
1798 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1799 if (!(limit_mask & BSLIMIT))
1800 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1801 if (!(limit_mask & IHLIMIT))
1802 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1803 if (!(limit_mask & ISLIMIT))
1804 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1807 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1808 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1813 rc = llapi_quotactl(mnt, &qctl);
1816 fprintf(stderr, "%s %s ", obd_type,
1817 obd_uuid2str(&qctl.obd_uuid));
1818 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1825 static inline char *type2name(int check_type)
1827 if (check_type == USRQUOTA)
1829 else if (check_type == GRPQUOTA)
1836 /* Converts seconds value into format string
1837 * result is returned in buf
1839 * 1. result is in descenting order: 1w2d3h4m5s
1840 * 2. zero fields are not filled (except for p. 3): 5d1s
1841 * 3. zero seconds value is presented as "0s"
1843 static void sec2str(time_t seconds, char *buf)
1845 const char spec[] = "smhdw";
1846 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1851 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1852 c = seconds / mult[i];
1854 if (c > 0 || (i == 0 && buf == tail))
1855 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1862 static void diff2str(time_t seconds, char *buf, time_t now)
1868 if (seconds <= now) {
1869 strcpy(buf, "none");
1872 sec2str(seconds - now, buf);
1875 static void print_quota_title(char *name, struct if_quotactl *qctl)
1877 printf("Disk quotas for %s %s (%cid %u):\n",
1878 type2name(qctl->qc_type), name,
1879 *type2name(qctl->qc_type), qctl->qc_id);
1880 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1882 "kbytes", "quota", "limit", "grace",
1883 "files", "quota", "limit", "grace");
1886 #define GENERAL_QUOTA_INFO 1
1887 #define MDS_QUOTA_INFO 2
1888 #define OST_QUOTA_INFO 3
1890 static void print_quota(char *mnt, struct if_quotactl *qctl, int type)
1896 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1897 int bover = 0, iover = 0;
1898 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1900 if (dqb->dqb_bhardlimit &&
1901 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1903 } else if (dqb->dqb_bsoftlimit &&
1904 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1905 if (dqb->dqb_btime > now) {
1912 if (dqb->dqb_ihardlimit &&
1913 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1915 } else if (dqb->dqb_isoftlimit &&
1916 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1917 if (dqb->dqb_btime > now) {
1924 #if 0 /* XXX: always print quotas even when no usages */
1925 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1931 if (strlen(mnt) > 15)
1932 printf("%s\n%15s", mnt, "");
1934 printf("%15s", mnt);
1937 diff2str(dqb->dqb_btime, timebuf, now);
1938 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
1939 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
1940 if (type == GENERAL_QUOTA_INFO)
1941 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
1942 ? LPU64 : "["LPU64"]",
1943 dqb->dqb_bsoftlimit);
1945 sprintf(numbuf[1], "%s", "");
1946 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
1947 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
1948 printf(" %7s%c %6s %7s %7s",
1949 numbuf[0], bover ? '*' : ' ', numbuf[1],
1950 numbuf[2], bover > 1 ? timebuf : "");
1953 diff2str(dqb->dqb_itime, timebuf, now);
1955 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
1956 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
1957 if (type == GENERAL_QUOTA_INFO)
1958 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
1959 ? LPU64 : "["LPU64"]",
1960 dqb->dqb_isoftlimit);
1962 sprintf(numbuf[1], "%s", "");
1963 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
1964 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
1965 if (type != OST_QUOTA_INFO)
1966 printf(" %7s%c %6s %7s %7s",
1967 numbuf[0], iover ? '*' : ' ', numbuf[1],
1968 numbuf[2], iover > 1 ? timebuf : "");
1971 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1972 qctl->qc_cmd == Q_GETOINFO) {
1976 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1977 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1978 printf("Block grace time: %s; Inode grace time: %s\n",
1979 bgtimebuf, igtimebuf);
1983 static int print_mds_quota(char *mnt, struct if_quotactl *qctl)
1987 /* XXX: this is a flag to mark that only mds quota is wanted */
1988 qctl->qc_dqblk.dqb_valid = 1;
1989 rc = llapi_quotactl(mnt, qctl);
1991 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1995 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, MDS_QUOTA_INFO);
1999 static int print_lov_quota(char *mnt, struct if_quotactl *qctl)
2002 struct obd_uuid *uuids = NULL, *uuidp;
2003 int obdcount = 1024;
2004 int i, rc = 0, rc1 = 0;
2008 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
2012 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
2013 sizeof(struct obd_uuid));
2018 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
2020 struct obd_uuid *uuids_temp;
2022 if (rc == -EOVERFLOW) {
2023 uuids_temp = realloc(uuids, obdcount *
2024 sizeof(struct obd_uuid));
2025 if (uuids_temp != NULL)
2026 goto retry_get_uuids;
2031 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
2035 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
2036 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
2038 /* XXX clear this flag to get quota from osts */
2039 qctl->qc_dqblk.dqb_valid = 0;
2040 rc = llapi_quotactl(mnt, qctl);
2044 fprintf(stderr, "%s quotactl failed: %s\n",
2045 uuidp->uuid, strerror(errno));
2049 print_quota((char *)uuidp->uuid, qctl, OST_QUOTA_INFO);
2059 static int lfs_quota(int argc, char **argv)
2062 char *mnt, *name = NULL;
2063 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2064 .qc_type = UGQUOTA };
2065 char *obd_type = (char *)qctl.obd_type;
2066 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2067 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0;
2071 while ((c = getopt(argc, argv, "ugto:v")) != -1) {
2074 if (qctl.qc_type != UGQUOTA) {
2075 fprintf(stderr, "error: use either -u or -g\n");
2078 qctl.qc_type = USRQUOTA;
2081 if (qctl.qc_type != UGQUOTA) {
2082 fprintf(stderr, "error: use either -u or -g\n");
2085 qctl.qc_type = GRPQUOTA;
2088 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2091 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2097 fprintf(stderr, "error: %s: option '-%c' "
2098 "unrecognized\n", argv[0], c);
2103 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2104 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2105 optind == argc - 1) {
2107 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2108 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2110 qctl.qc_type = USRQUOTA;
2111 qctl.qc_id = geteuid();
2113 qctl.qc_type = GRPQUOTA;
2114 qctl.qc_id = getegid();
2116 rc = id2name(&name, qctl.qc_id,
2117 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2120 /* lfs quota -u username /path/to/lustre/mount */
2121 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2122 /* options should be followed by u/g-name and mntpoint */
2123 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2124 fprintf(stderr, "error: missing quota argument(s)\n");
2128 name = argv[optind++];
2129 rc = name2id(&qctl.qc_id, name,
2130 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2132 fprintf(stderr,"error: can't find id for name %s: %s\n",
2133 name, strerror(errno));
2136 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2137 fprintf(stderr, "error: missing quota info argument(s)\n");
2141 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2142 print_quota_title(name, &qctl);
2146 rc1 = llapi_quotactl(mnt, &qctl);
2147 if (rc1 == -1 && errno == ESRCH) {
2148 fprintf(stderr, "\n%s quotas are not enabled.\n",
2149 qctl.qc_type == USRQUOTA ? "user" : "group");
2152 if (rc1 && *obd_type)
2153 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2158 print_quota(mnt, &qctl, GENERAL_QUOTA_INFO);
2160 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2161 rc2 = print_mds_quota(mnt, &qctl);
2162 rc3 = print_lov_quota(mnt, &qctl);
2165 if (rc1 || rc2 || rc3)
2166 printf("Some errors happened when getting quota info. "
2167 "Some devices may be not working or deactivated. "
2168 "The data in \"[]\" is inaccurate.\n");
2176 #endif /* HAVE_SYS_QUOTA_H! */
2178 int main(int argc, char **argv)
2184 ptl_initialize(argc, argv);
2185 if (obd_initialize(argc, argv) < 0)
2187 if (dbg_initialize(argc, argv) < 0)
2190 Parser_init("lfs > ", cmdlist);
2193 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2195 rc = Parser_commands();
2198 obd_finalize(argc, argv);