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);
100 static int lfs_path2fid(int argc, char **argv);
102 /* all avaialable commands */
103 command_t cmdlist[] = {
104 {"setstripe", lfs_setstripe, 0,
105 "Create a new file with a specific striping pattern or\n"
106 "set the default striping pattern on an existing directory or\n"
107 "delete the default striping pattern from an existing directory\n"
108 "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
109 " [--count|-c stripe_count] [--pool|-p <pool>]\n"
112 " setstripe -d <dir> (to delete default striping)\n"
113 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
114 "\t Can be specified with k, m or g (in KB, MB and GB\n"
116 "\tstart_ost: OST index of first stripe (-1 filesystem default)\n"
117 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
118 "\tpool: Name of OST pool"},
119 {"getstripe", lfs_getstripe, 0,
120 "To list the striping info for a given file or files in a\n"
121 "directory or recursively for all files in a directory tree.\n"
122 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
123 " [--count | -c ] [--size | -s ] [--index | -i ]\n"
124 " [--offset | -o ] [--pool | -p ]\n"
125 " [--recursive | -r] <dir|file> ..."},
126 {"pool_list", lfs_poollist, 0,
127 "List pools or pool OSTs\n"
128 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
129 {"find", lfs_find, 0,
130 "To find files that match given parameters recursively in a directory tree.\n"
131 "usage: find <dir|file> ... \n"
132 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
133 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
134 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
135 " [[!] --type|-t <filetype>] [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
136 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
137 " [[!] --pool <pool>]\n"
138 "\t !: used before an option indicates 'NOT' the requested attribute\n"
139 "\t -: used before an value indicates 'AT MOST' the requested value\n"
140 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
141 {"check", lfs_check, 0,
142 "Display the status of MDS or OSTs (as specified in the command)\n"
143 "or all the servers (MDS and OSTs).\n"
144 "usage: check <osts|mds|servers>"},
145 {"catinfo", lfs_catinfo, 0,
146 "Show information of specified type logs.\n"
147 "usage: catinfo {keyword} [node name]\n"
148 "\tkeywords are one of followings: config, deletions.\n"
149 "\tnode name must be provided when use keyword config."},
150 {"join", lfs_join, 0,
151 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
152 "usage: join <filename_A> <filename_B>\n"},
153 {"osts", lfs_osts, 0, "osts"},
155 "report filesystem disk space usage or inodes usage"
156 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
157 "Usage: df [-i] [-h] [--pool|-p <fsname>[.<pool>] [path]"},
158 #ifdef HAVE_SYS_QUOTA_H
159 {"quotachown",lfs_quotachown, 0,
160 "Change files' owner or group on the specified filesystem.\n"
161 "usage: quotachown [-i] <filesystem>\n"
162 "\t-i: ignore error if file is not exist\n"},
163 {"quotacheck", lfs_quotacheck, 0,
164 "Scan the specified filesystem for disk usage, and create,\n"
165 "or update quota files.\n"
166 "usage: quotacheck [ -ug ] <filesystem>"},
167 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
168 "usage: quotaon [ -ugf ] <filesystem>"},
169 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
170 "usage: quotaoff [ -ug ] <filesystem>"},
171 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
172 "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
173 " <block-softlimit> <block-hardlimit>\n"
174 " <inode-softlimit> <inode-hardlimit>\n"
176 " setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
177 " [--block-softlimit <block-softlimit>]\n"
178 " [--block-hardlimit <block-hardlimit>]\n"
179 " [--inode-softlimit <inode-softlimit>]\n"
180 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
181 " setquota -t <-u|-g> <block-grace> <inode-grace> <filesystem>\n"
182 " setquota -t <-u|--user|-g|--group>\n"
183 " [--block-grace <block-grace>]\n"
184 " [--inode-grace <inode-grace>] <filesystem>\n"
185 " -b can be used instead of --block-softlimit/--block-grace\n"
186 " -B can be used instead of --block-hardlimit\n"
187 " -i can be used instead of --inode-softlimit/--inode-grace\n"
188 " -I can be used instead of --inode-hardlimit"},
189 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
190 "usage: quota [-v] [ -o obd_uuid ] [<-u|-g> <uname>|<uid>|<gname>|<gid>]\n"
192 " quota [ -o obd_uuid ] -t <-u|-g> <filesystem>"},
193 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
194 "usage: quotainv [-u|-g] <filesystem>"},
196 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
197 "usage: path2fid <path>"},
198 {"help", Parser_help, 0, "help"},
199 {"exit", Parser_quit, 0, "quit"},
200 {"quit", Parser_quit, 0, "quit"},
204 static int isnumber(const char *str)
208 if (str[0] != '-' && !isdigit(str[0]))
211 for (ptr = str + 1; *ptr != '\0'; ptr++) {
220 static int lfs_setstripe(int argc, char **argv)
224 unsigned long long st_size;
225 int st_offset, st_count;
229 char *stripe_size_arg = NULL;
230 char *stripe_off_arg = NULL;
231 char *stripe_count_arg = NULL;
232 char *pool_name_arg = NULL;
233 unsigned long long size_units;
235 struct option long_opts[] = {
236 {"size", required_argument, 0, 's'},
237 {"count", required_argument, 0, 'c'},
238 {"index", required_argument, 0, 'i'},
239 {"offset", required_argument, 0, 'o'},
240 {"pool", required_argument, 0, 'p'},
241 {"delete", no_argument, 0, 'd'},
249 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
250 if (argc == 5 && argv[1][0] != '-' &&
251 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
252 fprintf(stderr, "warning: deprecated usage of setstripe "
253 "positional parameters. Use -c, -i, -s instead.\n");
254 /* for compatibility with the existing positional parameter
257 stripe_size_arg = argv[2];
258 stripe_off_arg = argv[3];
259 stripe_count_arg = argv[4];
263 #warning "remove obsolete positional parameter code"
267 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
268 long_opts, NULL)) >= 0) {
274 stripe_count_arg = optarg;
277 /* delete the default striping pattern */
282 stripe_off_arg = optarg;
285 stripe_size_arg = optarg;
288 pool_name_arg = optarg;
293 fprintf(stderr, "error: %s: option '%s' "
295 argv[0], argv[optind - 1]);
300 fname = argv[optind];
303 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
304 stripe_count_arg != NULL || pool_name_arg != NULL)) {
305 fprintf(stderr, "error: %s: cannot specify -d with "
306 "-s, -c -o or -p options\n",
312 if (optind == argc) {
313 fprintf(stderr, "error: %s: missing filename|dirname\n",
318 /* get the stripe size */
319 if (stripe_size_arg != NULL) {
320 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
322 fprintf(stderr, "error: %s: bad size '%s'\n",
323 argv[0], stripe_size_arg);
327 /* get the stripe offset */
328 if (stripe_off_arg != NULL) {
329 st_offset = strtoul(stripe_off_arg, &end, 0);
331 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
332 argv[0], stripe_off_arg);
336 /* get the stripe count */
337 if (stripe_count_arg != NULL) {
338 st_count = strtoul(stripe_count_arg, &end, 0);
340 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
341 argv[0], stripe_count_arg);
347 result = llapi_file_create_pool(fname, st_size, st_offset,
348 st_count, 0, pool_name_arg);
350 fprintf(stderr,"error: %s: create stripe file '%s' "
351 "failed\n", argv[0], fname);
354 fname = argv[++optind];
355 } while (fname != NULL);
360 static int lfs_poollist(int argc, char **argv)
365 return llapi_poollist(argv[1]);
368 static int set_time(time_t *time, time_t *set, char *str)
375 else if (str[0] == '-')
381 t = strtol(str, NULL, 0);
382 if (*time < t * 24 * 60 * 60) {
385 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
389 *set = *time - t * 24 * 60 * 60;
396 static int name2id(unsigned int *id, char *name, int type)
399 struct passwd *entry;
401 if (!(entry = getpwnam(name))) {
411 if (!(entry = getgrnam(name))) {
423 static int id2name(char **name, unsigned int id, int type)
426 struct passwd *entry;
428 if (!(entry = getpwuid(id))) {
434 *name = entry->pw_name;
438 if (!(entry = getgrgid(id))) {
444 *name = entry->gr_name;
450 #define FIND_POOL_OPT 3
451 static int lfs_find(int argc, char **argv)
456 struct find_param param = { .maxdepth = -1 };
458 struct option long_opts[] = {
459 /* New find options. */
460 {"atime", required_argument, 0, 'A'},
461 {"ctime", required_argument, 0, 'C'},
462 {"mtime", required_argument, 0, 'M'},
463 {"maxdepth", required_argument, 0, 'D'},
464 {"gid", required_argument, 0, 'g'},
465 {"group", required_argument, 0, 'G'},
466 {"uid", required_argument, 0, 'u'},
467 {"user", required_argument, 0, 'U'},
468 {"name", required_argument, 0, 'n'},
469 /* no short option for pool, p/P already used */
470 {"pool", required_argument, 0, FIND_POOL_OPT},
471 /* --obd is considered as a new option. */
472 {"obd", required_argument, 0, 'O'},
473 {"ost", required_argument, 0, 'O'},
474 {"print", no_argument, 0, 'P'},
475 {"print0", no_argument, 0, 'p'},
476 /* Old find options. */
477 {"quiet", no_argument, 0, 'q'},
478 {"recursive", no_argument, 0, 'r'},
479 {"size", required_argument, 0, 's'},
480 {"type", required_argument, 0, 't'},
481 {"verbose", no_argument, 0, 'v'},
495 /* when getopt_long_only() hits '!' it returns 1 and puts "!" in optarg */
496 while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
497 long_opts, NULL)) >= 0) {
502 /* '!' is part of option */
503 /* when getopt_long_only() finds a string which is not
504 * an option nor a known option argument it returns 1
505 * in that case if we already have found pathstart and pathend
506 * (i.e. we have the list of pathnames),
507 * the only supported value is "!"
509 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
510 if (!isoption && pathend != -1) {
511 fprintf(stderr, "err: %s: filename|dirname must either "
512 "precede options or follow options\n",
516 if (!isoption && pathstart == -1)
517 pathstart = optind - 1;
518 if (isoption && pathstart != -1 && pathend == -1) {
519 pathend = optind - 2;
520 if ((c == 1 && strcmp(optarg, "!") == 0) ||
521 c == 'P' || c == 'p' || c == 'O' ||
522 c == 'q' || c == 'r' || c == 'v')
523 pathend = optind - 1;
530 /* unknown; opt is "!" or path component,
531 * checking done above.
533 if (strcmp(optarg, "!") == 0)
537 xtime = ¶m.atime;
538 xsign = ¶m.asign;
541 xtime = ¶m.ctime;
542 xsign = ¶m.csign;
546 xtime = ¶m.mtime;
547 xsign = ¶m.msign;
551 if (optarg[0] == '-')
553 else if (optarg[0] == '+')
562 ret = set_time(&t, xtime, optarg);
570 param.maxdepth = strtol(optarg, 0, 0);
575 ret = name2id(¶m.gid, optarg, GROUP);
577 param.gid = strtoul(optarg, &endptr, 10);
578 if (*endptr != '\0') {
579 fprintf(stderr, "Group/GID: %s cannot "
580 "be found.\n", optarg);
584 param.exclude_gid = !!neg_opt;
590 ret = name2id(¶m.uid, optarg, USER);
592 param.uid = strtoul(optarg, &endptr, 10);
593 if (*endptr != '\0') {
594 fprintf(stderr, "User/UID: %s cannot "
595 "be found.\n", optarg);
599 param.exclude_uid = !!neg_opt;
604 if (strlen(optarg) > LOV_MAXPOOLNAME) {
606 "Pool name %s is too long"
607 " (max is %d)\n", optarg,
611 /* we do check for empty pool because empty pool
612 * is used to find V1 lov attributes */
613 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
614 param.poolname[LOV_MAXPOOLNAME] = '\0';
615 param.exclude_pool = !!neg_opt;
616 param.check_pool = 1;
620 param.pattern = (char *)optarg;
621 param.exclude_pattern = !!neg_opt;
624 char *buf, *token, *next, *p;
627 len = strlen((char *)optarg);
631 strcpy(buf, (char *)optarg);
633 if (param.num_alloc_obds == 0) {
634 param.obduuid = malloc(FIND_MAX_OSTS *
635 sizeof(struct obd_uuid));
636 if (param.obduuid == NULL)
638 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
641 for (token = buf; token && *token; token = next) {
642 p = strchr(token, ',');
648 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
672 param.exclude_type = !!neg_opt;
674 case 'b': param.type = S_IFBLK; break;
675 case 'c': param.type = S_IFCHR; break;
676 case 'd': param.type = S_IFDIR; break;
677 case 'f': param.type = S_IFREG; break;
678 case 'l': param.type = S_IFLNK; break;
679 case 'p': param.type = S_IFIFO; break;
680 case 's': param.type = S_IFSOCK; break;
681 #ifdef S_IFDOOR /* Solaris only */
682 case 'D': param.type = S_IFDOOR; break;
684 default: fprintf(stderr, "error: %s: bad type '%s'\n",
691 if (optarg[0] == '-')
693 else if (optarg[0] == '+')
702 if (optarg[0] == '+')
703 param.size_sign = -1;
704 else if (optarg[0] == '-')
705 param.size_sign = +1;
709 ret = parse_size(optarg, ¶m.size,
710 ¶m.size_units, 0);
712 fprintf(stderr,"error: bad size '%s'\n",
716 param.size_check = 1;
726 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
727 argv[0], argv[optind - 1]);
732 if (pathstart == -1) {
733 fprintf(stderr, "error: %s: no filename|pathname\n",
736 } else if (pathend == -1) {
744 static int deprecated_warning;
745 if (!deprecated_warning) {
746 fprintf(stderr, "lfs find: -q, -r, -v options "
747 "deprecated. Use 'lfs getstripe' instead.\n");
748 deprecated_warning = 1;
750 if (!param.recursive && param.maxdepth == -1)
756 ret = llapi_find(argv[pathstart], ¶m);
758 ret = llapi_getstripe(argv[pathstart], ¶m);
759 } while (++pathstart < pathend && !ret);
762 fprintf(stderr, "error: %s failed for %s.\n",
763 argv[0], argv[optind - 1]);
765 if (param.obduuid && param.num_alloc_obds)
771 static int lfs_getstripe(int argc, char **argv)
773 struct option long_opts[] = {
775 {"quiet", 0, 0, 'q'},
776 {"recursive", 0, 0, 'r'},
777 {"count", 0, 0, 'c'},
779 {"index", 0, 0, 'i'},
780 {"offset", 0, 0, 'o'},
782 {"verbose", 0, 0, 'v'},
783 {"directory", 0, 0, 'd'},
787 struct find_param param = { 0 };
791 while ((c = getopt_long(argc, argv, "cdhioO:pqrsv",
792 long_opts, NULL)) != -1) {
797 "error: %s: only one obduuid allowed",
801 param.obduuid = (struct obd_uuid *)optarg;
813 param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
816 if (!(param.verbose & VERBOSE_DETAIL)) {
817 param.verbose |= VERBOSE_COUNT;
822 if (!(param.verbose & VERBOSE_DETAIL)) {
823 param.verbose |= VERBOSE_SIZE;
829 if (!(param.verbose & VERBOSE_DETAIL)) {
830 param.verbose |= VERBOSE_OFFSET;
835 param.verbose |= VERBOSE_POOL;
840 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
841 argv[0], argv[optind - 1]);
853 param.verbose = VERBOSE_ALL;
855 param.verbose = VERBOSE_OBJID;
858 rc = llapi_getstripe(argv[optind], ¶m);
859 } while (++optind < argc && !rc);
862 fprintf(stderr, "error: %s failed for %s.\n",
863 argv[0], argv[optind - 1]);
867 static int lfs_osts(int argc, char **argv)
869 char mntdir[PATH_MAX] = {'\0'};
870 struct find_param param;
871 int index = 0, rc = 0;
876 while (llapi_search_mounts(NULL, index++, mntdir, NULL) == 0) {
877 memset(¶m, 0, sizeof(param));
878 rc = llapi_ostlist(mntdir, ¶m);
880 fprintf(stderr, "error: %s: failed on %s\n",
883 memset(mntdir, 0, PATH_MAX);
889 static int lfs_path2fid(int argc, char **argv)
899 rc = llapi_path2fid(path, &fid);
901 fprintf(stderr, "can't get fid for %s: %s\n", path,
902 strerror(errno = -rc));
906 printf(DFID"\n", PFID(&fid));
911 #define COOK(value) \
914 while (value > 1024) { \
928 static int showdf(char *mntdir, struct obd_statfs *stat,
929 char *uuid, int ishow, int cooked,
930 char *type, int index, int rc)
932 long long avail, used, total;
934 char *suffix = "KMGTPEZY";
935 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
936 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
944 avail = stat->os_ffree;
945 used = stat->os_files - stat->os_ffree;
946 total = stat->os_files;
948 int shift = cooked ? 0 : 10;
950 avail = (stat->os_bavail * stat->os_bsize) >> shift;
951 used = ((stat->os_blocks - stat->os_bfree) *
952 stat->os_bsize) >> shift;
953 total = (stat->os_blocks * stat->os_bsize) >> shift;
957 ratio = (double)used / (double)total;
963 cook_val = (double)total;
966 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
968 sprintf(tbuf, CDF, total);
970 cook_val = (double)used;
973 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
975 sprintf(ubuf, CDF, used);
977 cook_val = (double)avail;
980 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
982 sprintf(abuf, CDF, avail);
984 sprintf(tbuf, CDF, total);
985 sprintf(ubuf, CDF, used);
986 sprintf(abuf, CDF, avail);
989 sprintf(rbuf, RDF, (int)(ratio * 100));
990 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
991 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
993 printf("[%s:%d]\n", type, index);
999 printf(UUF": inactive device\n", uuid);
1002 printf(UUF": %s\n", uuid, strerror(-rc));
1009 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow, int cooked)
1011 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1012 struct obd_uuid uuid_buf;
1013 char *poolname = NULL;
1018 poolname = strchr(pool, '.');
1019 if (poolname != NULL) {
1020 if (strncmp(fsname, pool, strlen(fsname))) {
1021 fprintf(stderr, "filesystem name incorrect\n");
1030 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1031 "UUID", "Inodes", "IUsed", "IFree",
1032 "IUse%", "Mounted on");
1034 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1035 "UUID", cooked ? "bytes" : "1K-blocks",
1036 "Used", "Available", "Use%", "Mounted on");
1038 for (index = 0; ; index++) {
1039 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1040 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1041 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1042 &stat_buf, &uuid_buf);
1049 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1050 rc == -ENODATA || rc == 0) {
1051 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1052 ishow, cooked, "MDT", index, rc);
1055 "error: llapi_obd_statfs(%s): %s (%d)\n",
1056 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1060 sum.os_ffree += stat_buf.os_ffree;
1061 sum.os_files += stat_buf.os_files;
1065 for (index = 0; ; index++) {
1066 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1067 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1068 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1069 &stat_buf, &uuid_buf);
1076 if (llapi_search_ost(fsname, poolname,
1077 obd_uuid2str(&uuid_buf)) != 1)
1080 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1081 rc == -ENODATA || rc == 0) {
1082 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1083 ishow, cooked, "OST", index, rc);
1086 "error: llapi_obd_statfs failed: %s (%d)\n",
1091 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1092 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1093 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1098 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1103 static int lfs_df(int argc, char **argv)
1106 char *mntdir = NULL;
1107 int ishow = 0, cooked = 0;
1109 char fsname[PATH_MAX], *pool_name = NULL;
1110 struct option long_opts[] = {
1111 {"pool", required_argument, 0, 'p'},
1116 while ((c = getopt_long(argc, argv, "ihp:", long_opts, NULL)) != -1) {
1132 path = argv[optind];
1134 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1135 fprintf(stderr, "error: cannot allocate %d bytes\n",
1139 memset(mntdir, 0, PATH_MAX);
1142 char rpath[PATH_MAX] = {'\0'};
1144 if (!realpath(path, rpath)) {
1146 fprintf(stderr, "error: invalid path '%s': %s\n",
1147 path, strerror(-rc));
1151 rc = llapi_search_mounts(rpath, 0, mntdir, fsname);
1152 if (rc == 0 && mntdir[0] != '\0') {
1153 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked);
1159 while (!llapi_search_mounts(NULL, index++, mntdir, fsname)) {
1160 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked);
1171 static int lfs_check(int argc, char **argv)
1175 char mntdir[PATH_MAX] = {'\0'};
1183 obd_types[0] = obd_type1;
1184 obd_types[1] = obd_type2;
1186 if (strcmp(argv[1], "osts") == 0) {
1187 strcpy(obd_types[0], "osc");
1188 } else if (strcmp(argv[1], "mds") == 0) {
1189 strcpy(obd_types[0], "mdc");
1190 } else if (strcmp(argv[1], "servers") == 0) {
1192 strcpy(obd_types[0], "osc");
1193 strcpy(obd_types[1], "mdc");
1195 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1200 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1201 if (rc < 0 || mntdir[0] == '\0') {
1202 fprintf(stderr, "No suitable Lustre mount found\n");
1206 rc = llapi_target_iterate(num_types, obd_types,
1207 mntdir, llapi_ping_target);
1210 fprintf(stderr, "error: %s: %s status failed\n",
1217 static int lfs_catinfo(int argc, char **argv)
1219 char mntdir[PATH_MAX] = {'\0'};
1222 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1225 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1228 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1229 if (rc == 0 && mntdir[0] != '\0') {
1231 rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1233 rc = llapi_catinfo(mntdir, argv[1], NULL);
1235 fprintf(stderr, "no lustre_lite mounted.\n");
1242 int lfs_join(int argc, char **argv)
1244 char *name_head, *name_tail;
1250 name_head = argv[1];
1251 fd = open(name_head, O_WRONLY);
1253 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1257 size = lseek(fd, 0, SEEK_END);
1258 if (size % JOIN_FILE_ALIGN) {
1259 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1260 name_head, (long long)size, JOIN_FILE_ALIGN);
1264 name_tail = argv[2];
1265 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1269 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1275 #ifdef HAVE_SYS_QUOTA_H
1276 static int lfs_quotachown(int argc, char **argv)
1283 while ((c = getopt(argc, argv, "i")) != -1) {
1289 fprintf(stderr, "error: %s: option '-%c' "
1290 "unrecognized\n", argv[0], c);
1296 rc = llapi_quotachown(argv[optind], flag);
1298 fprintf(stderr,"error: change file owner/group failed.\n");
1302 static int lfs_quotacheck(int argc, char **argv)
1304 int c, check_type = 0;
1306 struct if_quotacheck qchk;
1307 struct if_quotactl qctl;
1308 char *obd_type = (char *)qchk.obd_type;
1311 memset(&qchk, 0, sizeof(qchk));
1314 while ((c = getopt(argc, argv, "ug")) != -1) {
1323 fprintf(stderr, "error: %s: option '-%c' "
1324 "unrecognized\n", argv[0], c);
1331 else /* do quotacheck for both user & group quota by default */
1339 memset(&qctl, 0, sizeof(qctl));
1340 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1341 qctl.qc_type = check_type;
1342 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1343 * take this parameter into account */
1344 rc = llapi_quotactl(mnt, &qctl);
1346 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1350 rc = llapi_quotacheck(mnt, check_type);
1352 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1356 rc = llapi_poll_quotacheck(mnt, &qchk);
1359 fprintf(stderr, "%s %s ", obd_type,
1360 obd_uuid2str(&qchk.obd_uuid));
1361 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1365 memset(&qctl, 0, sizeof(qctl));
1366 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1367 qctl.qc_type = check_type;
1368 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1369 * take this parameter into account */
1370 rc = llapi_quotactl(mnt, &qctl);
1373 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1374 obd_uuid2str(&qctl.obd_uuid));
1375 fprintf(stderr, "%s turn on quota failed: %s\n",
1376 argv[0], strerror(errno));
1383 static int lfs_quotaon(int argc, char **argv)
1387 struct if_quotactl qctl;
1388 char *obd_type = (char *)qctl.obd_type;
1391 memset(&qctl, 0, sizeof(qctl));
1392 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1393 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1394 * take this parameter into account */
1397 while ((c = getopt(argc, argv, "ugf")) != -1) {
1400 qctl.qc_type |= 0x01;
1403 qctl.qc_type |= 0x02;
1406 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1409 fprintf(stderr, "error: %s: option '-%c' "
1410 "unrecognized\n", argv[0], c);
1417 else /* by default, enable quota for both user & group */
1418 qctl.qc_type = 0x02;
1425 rc = llapi_quotactl(mnt, &qctl);
1428 fprintf(stderr, "%s %s ", obd_type,
1429 obd_uuid2str(&qctl.obd_uuid));
1430 if (errno == ENOENT)
1431 fprintf(stderr, "error: cannot find quota database, "
1432 "make sure you have run quotacheck\n");
1434 fprintf(stderr, "error: quotaon failed (%s)\n",
1442 static int lfs_quotaoff(int argc, char **argv)
1446 struct if_quotactl qctl;
1447 char *obd_type = (char *)qctl.obd_type;
1450 memset(&qctl, 0, sizeof(qctl));
1451 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1454 while ((c = getopt(argc, argv, "ug")) != -1) {
1457 qctl.qc_type |= 0x01;
1460 qctl.qc_type |= 0x02;
1463 fprintf(stderr, "error: %s: option '-%c' "
1464 "unrecognized\n", argv[0], c);
1471 else /* by default, disable quota for both user & group */
1472 qctl.qc_type = 0x02;
1479 rc = llapi_quotactl(mnt, &qctl);
1480 if (rc == -1 && errno == ESRCH) {
1481 fprintf(stderr, "\n%s quotas are not enabled.\n",
1482 qctl.qc_type == 0x00 ? "user" : "group");
1487 fprintf(stderr, "%s %s ", obd_type,
1488 obd_uuid2str(&qctl.obd_uuid));
1489 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1496 static int lfs_quotainv(int argc, char **argv)
1500 struct if_quotactl qctl;
1503 memset(&qctl, 0, sizeof(qctl));
1504 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1507 while ((c = getopt(argc, argv, "ugf")) != -1) {
1510 qctl.qc_type |= 0x01;
1513 qctl.qc_type |= 0x02;
1516 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1519 fprintf(stderr, "error: %s: option '-%c' "
1520 "unrecognized\n", argv[0], c);
1527 else /* by default, invalidate quota for both user & group */
1528 qctl.qc_type = 0x02;
1535 rc = llapi_quotactl(mnt, &qctl);
1537 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1544 #define ARG2INT(nr, str, msg) \
1547 nr = strtol(str, &endp, 0); \
1549 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1554 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1556 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1557 * returns the value or ULONG_MAX on integer overflow or incorrect format
1559 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1560 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1561 * 3. empty integer value is interpreted as 0
1564 static unsigned long str2sec(const char* timestr) {
1565 const char spec[] = "smhdw";
1566 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1567 unsigned long val = 0;
1570 if (strpbrk(timestr, spec) == NULL) {
1571 /* no specifiers inside the time string,
1572 should treat it as an integer value */
1573 val = strtoul(timestr, &tail, 10);
1574 return *tail ? ULONG_MAX : val;
1577 /* format string is XXwXXdXXhXXmXXs */
1583 v = strtoul(timestr, &tail, 10);
1584 if (v == ULONG_MAX || *tail == '\0')
1585 /* value too large (ULONG_MAX or more)
1586 or missing specifier */
1589 ptr = strchr(spec, *tail);
1591 /* unknown specifier */
1596 /* check if product will overflow the type */
1597 if (!(v < ULONG_MAX / mult[ind]))
1600 ADD_OVERFLOW(val, mult[ind] * v);
1601 if (val == ULONG_MAX)
1613 #define ARG2ULL(nr, str, defscale) \
1615 unsigned long long limit, units = 0; \
1618 rc = parse_size(str, &limit, &units, 1); \
1620 fprintf(stderr, "error: bad limit value %s\n", str); \
1623 nr = ((units == 0) ? (defscale) : 1) * limit; \
1626 static inline int has_times_option(int argc, char **argv)
1630 for (i = 1; i < argc; i++)
1631 if (!strcmp(argv[i], "-t"))
1637 int lfs_setquota_times(int argc, char **argv)
1640 struct if_quotactl qctl;
1641 char *mnt, *obd_type = (char *)qctl.obd_type;
1642 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1643 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1644 struct option long_opts[] = {
1645 {"user", no_argument, 0, 'u'},
1646 {"group", no_argument, 0, 'g'},
1647 {"block-grace", required_argument, 0, 'b'},
1648 {"inode-grace", required_argument, 0, 'i'},
1649 {"times", no_argument, 0, 't'},
1653 memset(&qctl, 0, sizeof(qctl));
1654 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1655 qctl.qc_type = UGQUOTA;
1658 /* compatibility syntax: setquota -t -[u|g] t1 t2 mnt */
1659 if (argc == 6 && !strcmp(argv[1], "-t") &&
1660 (!strcmp(argv[2], "-u") || !strcmp(argv[2], "-g")) &&
1661 argv[3][0] != '-' && argv[4][0] != '-') {
1662 fprintf(stderr, "warning: using compatibility syntax, it may not"
1663 " be available in future releases!\n");
1665 qctl.qc_type = !strcmp(argv[2], "-u") ? USRQUOTA : GRPQUOTA;
1667 if ((dqi->dqi_bgrace = str2sec(argv[3])) == ULONG_MAX) {
1668 fprintf(stderr, "error: bad block-grace: %s\n", argv[3]);
1671 if ((dqi->dqi_igrace = str2sec(argv[4])) == ULONG_MAX) {
1672 fprintf(stderr, "error: bad inode-grace: %s\n", argv[4]);
1675 dqb->dqb_valid = QIF_TIMES;
1676 mnt = argv[argc - 1];
1682 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1686 if (qctl.qc_type != UGQUOTA) {
1687 fprintf(stderr, "error: -u and -g can't be used "
1688 "more than once\n");
1691 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1694 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1695 fprintf(stderr, "error: bad block-grace: %s\n",
1699 dqb->dqb_valid |= QIF_BTIME;
1702 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1703 fprintf(stderr, "error: bad inode-grace: %s\n",
1707 dqb->dqb_valid |= QIF_ITIME;
1709 case 't': /* Yes, of course! */
1711 default: /* getopt prints error message for us when opterr != 0 */
1716 if (qctl.qc_type == UGQUOTA) {
1717 fprintf(stderr, "error: neither -u nor -g specified\n");
1721 if (optind != argc - 1) {
1722 fprintf(stderr, "error: unexpected parameters encountered\n");
1731 rc = llapi_quotactl(mnt, &qctl);
1734 fprintf(stderr, "%s %s ", obd_type,
1735 obd_uuid2str(&qctl.obd_uuid));
1736 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1743 #define BSLIMIT (1 << 0)
1744 #define BHLIMIT (1 << 1)
1745 #define ISLIMIT (1 << 2)
1746 #define IHLIMIT (1 << 3)
1748 int lfs_setquota(int argc, char **argv)
1751 struct if_quotactl qctl;
1752 char *mnt, *obd_type = (char *)qctl.obd_type;
1753 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1754 struct option long_opts[] = {
1755 {"user", required_argument, 0, 'u'},
1756 {"group", required_argument, 0, 'g'},
1757 {"block-softlimit", required_argument, 0, 'b'},
1758 {"block-hardlimit", required_argument, 0, 'B'},
1759 {"inode-softlimit", required_argument, 0, 'i'},
1760 {"inode-hardlimit", required_argument, 0, 'I'},
1763 unsigned limit_mask = 0;
1766 if (has_times_option(argc, argv))
1767 return lfs_setquota_times(argc, argv);
1769 memset(&qctl, 0, sizeof(qctl));
1770 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1771 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1772 * so it can be used as a marker that qc_type
1773 * isn't reinitialized from command line */
1776 /* compatibility syntax: [-u|-g] <user|group> b B i I mount
1777 * will be removed in the future */
1778 if (argc == 8 && (!strcmp(argv[1], "-u") || !strcmp(argv[1], "-g")) &&
1779 argv[3][0] != '-' && argv[4][0] != '-' && argv[5][0] != '-' &&
1780 argv[6][0] != '-') {
1781 fprintf(stderr, "warning: using compatibility syntax, it may not"
1782 " be available in future releases!\n");
1784 qctl.qc_type = !strcmp(argv[1], "-u") ? USRQUOTA : GRPQUOTA;
1785 rc = name2id(&qctl.qc_id, argv[2],
1786 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1788 fprintf(stderr, "error: unknown id %s\n", argv[2]);
1792 ARG2ULL(dqb->dqb_bsoftlimit, argv[3], 1024);
1793 dqb->dqb_bsoftlimit >>= 10;
1794 ARG2ULL(dqb->dqb_bhardlimit, argv[4], 1024);
1795 dqb->dqb_bhardlimit >>= 10;
1796 ARG2ULL(dqb->dqb_isoftlimit, argv[5], 1);
1797 ARG2ULL(dqb->dqb_ihardlimit, argv[6], 1);
1799 dqb->dqb_valid = QIF_LIMITS;
1800 mnt = argv[argc - 1];
1806 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1810 if (qctl.qc_type != UGQUOTA) {
1811 fprintf(stderr, "error: -u and -g can't be used"
1812 " more than once\n");
1815 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1816 rc = name2id(&qctl.qc_id, optarg,
1817 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1819 qctl.qc_id = strtoul(optarg, &endptr, 10);
1820 if (*endptr != '\0') {
1821 fprintf(stderr, "error: can't find id "
1822 "for name %s\n", optarg);
1828 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1829 dqb->dqb_bsoftlimit >>= 10;
1830 limit_mask |= BSLIMIT;
1833 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1834 dqb->dqb_bhardlimit >>= 10;
1835 limit_mask |= BHLIMIT;
1838 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1839 limit_mask |= ISLIMIT;
1842 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1843 limit_mask |= IHLIMIT;
1845 default: /* getopt prints error message for us when opterr != 0 */
1850 if (qctl.qc_type == UGQUOTA) {
1851 fprintf(stderr, "error: neither -u nor -g was specified\n");
1855 if (limit_mask == 0) {
1856 fprintf(stderr, "error: at least one limit must be specified\n");
1860 if (optind != argc - 1) {
1861 fprintf(stderr, "error: unexpected parameters encountered\n");
1867 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1868 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1869 /* sigh, we can't just set blimits/ilimits */
1870 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1871 .qc_type = qctl.qc_type,
1872 .qc_id = qctl.qc_id};
1874 rc = llapi_quotactl(mnt, &tmp_qctl);
1876 fprintf(stderr, "error: setquota failed while retrieving"
1877 " current quota settings (%s)\n",
1882 if (!(limit_mask & BHLIMIT))
1883 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1884 if (!(limit_mask & BSLIMIT))
1885 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1886 if (!(limit_mask & IHLIMIT))
1887 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1888 if (!(limit_mask & ISLIMIT))
1889 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1891 /* Keep grace times if we have got no softlimit arguments */
1892 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
1893 dqb->dqb_valid |= QIF_BTIME;
1894 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
1897 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
1898 dqb->dqb_valid |= QIF_ITIME;
1899 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
1903 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1904 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1909 rc = llapi_quotactl(mnt, &qctl);
1912 fprintf(stderr, "%s %s ", obd_type,
1913 obd_uuid2str(&qctl.obd_uuid));
1914 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1921 static inline char *type2name(int check_type)
1923 if (check_type == USRQUOTA)
1925 else if (check_type == GRPQUOTA)
1932 /* Converts seconds value into format string
1933 * result is returned in buf
1935 * 1. result is in descenting order: 1w2d3h4m5s
1936 * 2. zero fields are not filled (except for p. 3): 5d1s
1937 * 3. zero seconds value is presented as "0s"
1939 static void sec2str(time_t seconds, char *buf)
1941 const char spec[] = "smhdw";
1942 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1947 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1948 c = seconds / mult[i];
1950 if (c > 0 || (i == 0 && buf == tail))
1951 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1958 static void diff2str(time_t seconds, char *buf, time_t now)
1964 if (seconds <= now) {
1965 strcpy(buf, "none");
1968 sec2str(seconds - now, buf);
1971 static void print_quota_title(char *name, struct if_quotactl *qctl)
1973 printf("Disk quotas for %s %s (%cid %u):\n",
1974 type2name(qctl->qc_type), name,
1975 *type2name(qctl->qc_type), qctl->qc_id);
1976 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1978 "kbytes", "quota", "limit", "grace",
1979 "files", "quota", "limit", "grace");
1982 #define GENERAL_QUOTA_INFO 1
1983 #define MDS_QUOTA_INFO 2
1984 #define OST_QUOTA_INFO 3
1986 static void print_quota(char *mnt, struct if_quotactl *qctl, int type)
1992 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1993 int bover = 0, iover = 0;
1994 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1996 if (dqb->dqb_bhardlimit &&
1997 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1999 } else if (dqb->dqb_bsoftlimit &&
2000 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
2001 if (dqb->dqb_btime > now) {
2008 if (dqb->dqb_ihardlimit &&
2009 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2011 } else if (dqb->dqb_isoftlimit &&
2012 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
2013 if (dqb->dqb_btime > now) {
2020 #if 0 /* XXX: always print quotas even when no usages */
2021 if (dqb->dqb_curspace || dqb->dqb_curinodes)
2027 if (strlen(mnt) > 15)
2028 printf("%s\n%15s", mnt, "");
2030 printf("%15s", mnt);
2033 diff2str(dqb->dqb_btime, timebuf, now);
2034 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2035 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
2036 if (type == GENERAL_QUOTA_INFO)
2037 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2038 ? LPU64 : "["LPU64"]",
2039 dqb->dqb_bsoftlimit);
2041 sprintf(numbuf[1], "%s", "");
2042 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2043 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2044 printf(" %7s%c %6s %7s %7s",
2045 numbuf[0], bover ? '*' : ' ', numbuf[1],
2046 numbuf[2], bover > 1 ? timebuf : "");
2049 diff2str(dqb->dqb_itime, timebuf, now);
2051 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2052 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2053 if (type == GENERAL_QUOTA_INFO)
2054 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2055 ? LPU64 : "["LPU64"]",
2056 dqb->dqb_isoftlimit);
2058 sprintf(numbuf[1], "%s", "");
2059 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2060 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2061 if (type != OST_QUOTA_INFO)
2062 printf(" %7s%c %6s %7s %7s",
2063 numbuf[0], iover ? '*' : ' ', numbuf[1],
2064 numbuf[2], iover > 1 ? timebuf : "");
2067 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2068 qctl->qc_cmd == Q_GETOINFO) {
2072 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2073 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2074 printf("Block grace time: %s; Inode grace time: %s\n",
2075 bgtimebuf, igtimebuf);
2079 static int print_mds_quota(char *mnt, struct if_quotactl *qctl)
2083 /* XXX: this is a flag to mark that only mds quota is wanted */
2084 qctl->qc_dqblk.dqb_valid = 1;
2085 rc = llapi_quotactl(mnt, qctl);
2087 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
2091 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, MDS_QUOTA_INFO);
2095 static int print_lov_quota(char *mnt, struct if_quotactl *qctl)
2098 struct obd_uuid *uuids = NULL, *uuidp;
2099 int obdcount = 1024;
2100 int i, rc = 0, rc1 = 0;
2104 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
2108 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
2109 sizeof(struct obd_uuid));
2114 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
2116 struct obd_uuid *uuids_temp;
2118 if (rc == -EOVERFLOW) {
2119 uuids_temp = realloc(uuids, obdcount *
2120 sizeof(struct obd_uuid));
2121 if (uuids_temp != NULL)
2122 goto retry_get_uuids;
2127 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
2131 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
2132 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
2134 /* XXX clear this flag to get quota from osts */
2135 qctl->qc_dqblk.dqb_valid = 0;
2136 rc = llapi_quotactl(mnt, qctl);
2140 fprintf(stderr, "%s quotactl failed: %s\n",
2141 uuidp->uuid, strerror(errno));
2145 print_quota((char *)uuidp->uuid, qctl, OST_QUOTA_INFO);
2155 static int lfs_quota(int argc, char **argv)
2158 char *mnt, *name = NULL;
2159 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2160 .qc_type = UGQUOTA };
2161 char *obd_type = (char *)qctl.obd_type;
2162 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2163 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0;
2168 while ((c = getopt(argc, argv, "ugto:v")) != -1) {
2171 if (qctl.qc_type != UGQUOTA) {
2172 fprintf(stderr, "error: use either -u or -g\n");
2175 qctl.qc_type = USRQUOTA;
2178 if (qctl.qc_type != UGQUOTA) {
2179 fprintf(stderr, "error: use either -u or -g\n");
2182 qctl.qc_type = GRPQUOTA;
2185 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2188 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2194 fprintf(stderr, "error: %s: option '-%c' "
2195 "unrecognized\n", argv[0], c);
2200 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2201 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2202 optind == argc - 1) {
2204 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2205 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2207 qctl.qc_type = USRQUOTA;
2208 qctl.qc_id = geteuid();
2210 qctl.qc_type = GRPQUOTA;
2211 qctl.qc_id = getegid();
2213 rc = id2name(&name, qctl.qc_id,
2214 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2217 /* lfs quota -u username /path/to/lustre/mount */
2218 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2219 /* options should be followed by u/g-name and mntpoint */
2220 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2221 fprintf(stderr, "error: missing quota argument(s)\n");
2225 name = argv[optind++];
2226 rc = name2id(&qctl.qc_id, name,
2227 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2229 qctl.qc_id = strtoul(name, &endptr, 10);
2230 if (*endptr != '\0') {
2231 fprintf(stderr, "error: can't find id for name "
2236 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2237 fprintf(stderr, "error: missing quota info argument(s)\n");
2241 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2242 print_quota_title(name, &qctl);
2246 rc1 = llapi_quotactl(mnt, &qctl);
2247 if (rc1 == -1 && errno == ESRCH) {
2248 fprintf(stderr, "\n%s quotas are not enabled.\n",
2249 qctl.qc_type == USRQUOTA ? "user" : "group");
2252 if (rc1 && *obd_type)
2253 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2258 print_quota(mnt, &qctl, GENERAL_QUOTA_INFO);
2260 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2261 rc2 = print_mds_quota(mnt, &qctl);
2262 rc3 = print_lov_quota(mnt, &qctl);
2265 if (rc1 || rc2 || rc3)
2266 printf("Some errors happened when getting quota info. "
2267 "Some devices may be not working or deactivated. "
2268 "The data in \"[]\" is inaccurate.\n");
2276 #endif /* HAVE_SYS_QUOTA_H! */
2278 int main(int argc, char **argv)
2284 ptl_initialize(argc, argv);
2285 if (obd_initialize(argc, argv) < 0)
2287 if (dbg_initialize(argc, argv) < 0)
2290 Parser_init("lfs > ", cmdlist);
2293 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2295 rc = Parser_commands();
2298 obd_finalize(argc, argv);
2299 return rc < 0 ? -rc : rc;