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 (c) 2003, 2010, Oracle and/or its affiliates. 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] [--count|-c stripe_count]\n"
109 " [--index|-i|--offset|-o start_ost_index]\n"
110 " [--pool|-p <pool>] <directory|filename>\n"
112 " setstripe -d <directory> (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_index: OST index of first stripe (-1 default)\n"
117 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
118 "\tpool: Name of OST pool to use (default none)"},
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 ] [--index | -i | --offset | -o]\n"
124 " [--size | -s ] [--pool | -p ] [--directory | -d]\n"
125 " [--recursive | -r] <directory|filename> ..."},
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 <directory|filename> ...\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, "list OSTs connected to client "
154 "[for specified path only]\n" "usage: osts [path]"},
156 "report filesystem disk space usage or inodes usage"
157 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
158 "Usage: df [-i] [-h] [--pool|-p <fsname>[.<pool>] [path]"},
159 #ifdef HAVE_SYS_QUOTA_H
160 {"quotachown",lfs_quotachown, 0,
161 "Change files' owner or group on the specified filesystem.\n"
162 "usage: quotachown [-i] <filesystem>\n"
163 "\t-i: ignore error if file is not exist\n"},
164 {"quotacheck", lfs_quotacheck, 0,
165 "Scan the specified filesystem for disk usage, and create,\n"
166 "or update quota files.\n"
167 "usage: quotacheck [ -ug ] <filesystem>"},
168 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
169 "usage: quotaon [ -ugf ] <filesystem>"},
170 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
171 "usage: quotaoff [ -ug ] <filesystem>"},
172 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
173 "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
174 " <block-softlimit> <block-hardlimit>\n"
175 " <inode-softlimit> <inode-hardlimit>\n"
177 " setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
178 " [--block-softlimit <block-softlimit>]\n"
179 " [--block-hardlimit <block-hardlimit>]\n"
180 " [--inode-softlimit <inode-softlimit>]\n"
181 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
182 " setquota -t <-u|-g> <block-grace> <inode-grace> <filesystem>\n"
183 " setquota -t <-u|--user|-g|--group>\n"
184 " [--block-grace <block-grace>]\n"
185 " [--inode-grace <inode-grace>] <filesystem>\n"
186 " -b can be used instead of --block-softlimit/--block-grace\n"
187 " -B can be used instead of --block-hardlimit\n"
188 " -i can be used instead of --inode-softlimit/--inode-grace\n"
189 " -I can be used instead of --inode-hardlimit"},
190 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
191 "usage: quota [<-u|-g> <uname>|<uid>|<gname>|<gid>]\n"
192 " [-q] [-v] [-o <obd_uuid>] <filesystem>\n"
193 " quota [-o <obd_uuid>] -t <-u|-g> <filesystem>"},
194 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
195 "usage: quotainv [-u|-g] <filesystem>"},
197 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
198 "usage: path2fid <path>"},
199 {"help", Parser_help, 0, "help"},
200 {"exit", Parser_quit, 0, "quit"},
201 {"quit", Parser_quit, 0, "quit"},
205 static int isnumber(const char *str)
209 if (str[0] != '-' && !isdigit(str[0]))
212 for (ptr = str + 1; *ptr != '\0'; ptr++) {
221 static int lfs_setstripe(int argc, char **argv)
225 unsigned long long st_size;
226 int st_offset, st_count;
230 char *stripe_size_arg = NULL;
231 char *stripe_off_arg = NULL;
232 char *stripe_count_arg = NULL;
233 char *pool_name_arg = NULL;
234 unsigned long long size_units;
236 struct option long_opts[] = {
237 {"size", required_argument, 0, 's'},
238 {"count", required_argument, 0, 'c'},
239 {"index", required_argument, 0, 'i'},
240 {"offset", required_argument, 0, 'o'},
241 {"pool", required_argument, 0, 'p'},
242 {"delete", no_argument, 0, 'd'},
250 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
251 if (argc == 5 && argv[1][0] != '-' &&
252 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
253 fprintf(stderr, "warning: deprecated usage of setstripe "
254 "positional parameters. Use -c, -i, -s instead.\n");
255 /* for compatibility with the existing positional parameter
258 stripe_size_arg = argv[2];
259 stripe_off_arg = argv[3];
260 stripe_count_arg = argv[4];
264 #warning "remove obsolete positional parameter code"
268 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
269 long_opts, NULL)) >= 0) {
275 stripe_count_arg = optarg;
278 /* delete the default striping pattern */
283 stripe_off_arg = optarg;
286 stripe_size_arg = optarg;
289 pool_name_arg = optarg;
294 fprintf(stderr, "error: %s: option '%s' "
296 argv[0], argv[optind - 1]);
301 fname = argv[optind];
304 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
305 stripe_count_arg != NULL || pool_name_arg != NULL)) {
306 fprintf(stderr, "error: %s: cannot specify -d with "
307 "-s, -c -o or -p options\n",
313 if (optind == argc) {
314 fprintf(stderr, "error: %s: missing filename|dirname\n",
319 /* get the stripe size */
320 if (stripe_size_arg != NULL) {
321 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
323 fprintf(stderr, "error: %s: bad size '%s'\n",
324 argv[0], stripe_size_arg);
328 /* get the stripe offset */
329 if (stripe_off_arg != NULL) {
330 st_offset = strtol(stripe_off_arg, &end, 0);
332 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
333 argv[0], stripe_off_arg);
337 /* get the stripe count */
338 if (stripe_count_arg != NULL) {
339 st_count = strtoul(stripe_count_arg, &end, 0);
341 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
342 argv[0], stripe_count_arg);
348 result = llapi_file_create_pool(fname, st_size, st_offset,
349 st_count, 0, pool_name_arg);
351 fprintf(stderr,"error: %s: create stripe file '%s' "
352 "failed\n", argv[0], fname);
355 fname = argv[++optind];
356 } while (fname != NULL);
361 static int lfs_poollist(int argc, char **argv)
366 return llapi_poollist(argv[1]);
369 static int set_time(time_t *time, time_t *set, char *str)
376 else if (str[0] == '-')
382 t = strtol(str, NULL, 0);
383 if (*time < t * 24 * 60 * 60) {
386 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
390 *set = *time - t * 24 * 60 * 60;
397 static int name2id(unsigned int *id, char *name, int type)
400 struct passwd *entry;
402 if (!(entry = getpwnam(name))) {
412 if (!(entry = getgrnam(name))) {
424 static int id2name(char **name, unsigned int id, int type)
427 struct passwd *entry;
429 if (!(entry = getpwuid(id))) {
435 *name = entry->pw_name;
439 if (!(entry = getgrgid(id))) {
445 *name = entry->gr_name;
451 #define FIND_POOL_OPT 3
452 static int lfs_find(int argc, char **argv)
457 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;
539 param.exclude_atime = !!neg_opt;
542 xtime = ¶m.ctime;
543 xsign = ¶m.csign;
544 param.exclude_ctime = !!neg_opt;
548 xtime = ¶m.mtime;
549 xsign = ¶m.msign;
550 param.exclude_mtime = !!neg_opt;
553 ret = set_time(&t, xtime, optarg);
561 param.maxdepth = strtol(optarg, 0, 0);
566 ret = name2id(¶m.gid, optarg, GROUP);
568 param.gid = strtoul(optarg, &endptr, 10);
569 if (*endptr != '\0') {
570 fprintf(stderr, "Group/GID: %s cannot "
571 "be found.\n", optarg);
575 param.exclude_gid = !!neg_opt;
581 ret = name2id(¶m.uid, optarg, USER);
583 param.uid = strtoul(optarg, &endptr, 10);
584 if (*endptr != '\0') {
585 fprintf(stderr, "User/UID: %s cannot "
586 "be found.\n", optarg);
590 param.exclude_uid = !!neg_opt;
595 if (strlen(optarg) > LOV_MAXPOOLNAME) {
597 "Pool name %s is too long"
598 " (max is %d)\n", optarg,
602 /* we do check for empty pool because empty pool
603 * is used to find V1 lov attributes */
604 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
605 param.poolname[LOV_MAXPOOLNAME] = '\0';
606 param.exclude_pool = !!neg_opt;
607 param.check_pool = 1;
611 param.pattern = (char *)optarg;
612 param.exclude_pattern = !!neg_opt;
615 char *buf, *token, *next, *p;
618 len = strlen((char *)optarg);
622 strcpy(buf, (char *)optarg);
624 param.exclude_obd = !!neg_opt;
626 if (param.num_alloc_obds == 0) {
627 param.obduuid = malloc(FIND_MAX_OSTS *
628 sizeof(struct obd_uuid));
629 if (param.obduuid == NULL)
631 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
634 for (token = buf; token && *token; token = next) {
635 p = strchr(token, ',');
641 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
665 param.exclude_type = !!neg_opt;
667 case 'b': param.type = S_IFBLK; break;
668 case 'c': param.type = S_IFCHR; break;
669 case 'd': param.type = S_IFDIR; break;
670 case 'f': param.type = S_IFREG; break;
671 case 'l': param.type = S_IFLNK; break;
672 case 'p': param.type = S_IFIFO; break;
673 case 's': param.type = S_IFSOCK; break;
674 #ifdef S_IFDOOR /* Solaris only */
675 case 'D': param.type = S_IFDOOR; break;
677 default: fprintf(stderr, "error: %s: bad type '%s'\n",
683 if (optarg[0] == '+')
684 param.size_sign = -1;
685 else if (optarg[0] == '-')
686 param.size_sign = +1;
690 ret = parse_size(optarg, ¶m.size,
691 ¶m.size_units, 0);
693 fprintf(stderr,"error: bad size '%s'\n",
697 param.exclude_size = !!neg_opt;
698 param.size_check = 1;
708 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
709 argv[0], argv[optind - 1]);
714 if (pathstart == -1) {
715 fprintf(stderr, "error: %s: no filename|pathname\n",
718 } else if (pathend == -1) {
726 static int deprecated_warning;
727 if (!deprecated_warning) {
728 fprintf(stderr, "lfs find: -q, -r, -v options "
729 "deprecated. Use 'lfs getstripe' instead.\n");
730 deprecated_warning = 1;
732 if (!param.recursive && param.maxdepth == -1)
738 ret = llapi_find(argv[pathstart], ¶m);
740 ret = llapi_getstripe(argv[pathstart], ¶m);
741 } while (++pathstart < pathend && !ret);
744 fprintf(stderr, "error: %s failed for %s.\n",
745 argv[0], argv[optind - 1]);
747 if (param.obduuid && param.num_alloc_obds)
753 static int lfs_getstripe(int argc, char **argv)
755 struct option long_opts[] = {
757 {"quiet", 0, 0, 'q'},
758 {"recursive", 0, 0, 'r'},
759 {"count", 0, 0, 'c'},
761 {"index", 0, 0, 'i'},
762 {"offset", 0, 0, 'o'},
764 {"verbose", 0, 0, 'v'},
765 {"directory", 0, 0, 'd'},
769 struct find_param param = { 0 };
773 while ((c = getopt_long(argc, argv, "cdhioO:pqrsv",
774 long_opts, NULL)) != -1) {
779 "error: %s: only one obduuid allowed",
783 param.obduuid = (struct obd_uuid *)optarg;
795 param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
798 if (!(param.verbose & VERBOSE_DETAIL)) {
799 param.verbose |= VERBOSE_COUNT;
804 if (!(param.verbose & VERBOSE_DETAIL)) {
805 param.verbose |= VERBOSE_SIZE;
811 if (!(param.verbose & VERBOSE_DETAIL)) {
812 param.verbose |= VERBOSE_OFFSET;
817 param.verbose |= VERBOSE_POOL;
822 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
823 argv[0], argv[optind - 1]);
835 param.verbose = VERBOSE_ALL;
837 param.verbose = VERBOSE_OBJID;
840 rc = llapi_getstripe(argv[optind], ¶m);
841 } while (++optind < argc && !rc);
844 fprintf(stderr, "error: %s failed for %s.\n",
845 argv[0], argv[optind - 1]);
849 static int lfs_osts(int argc, char **argv)
851 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
852 struct find_param param;
853 int index = 0, rc = 0;
858 if (argc == 2 && !realpath(argv[1], path)) {
860 fprintf(stderr, "error: invalid path '%s': %s\n",
861 argv[1], strerror(-rc));
865 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
866 /* Check if we have a mount point */
867 if (mntdir[0] == '\0')
870 memset(¶m, 0, sizeof(param));
871 rc = llapi_ostlist(mntdir, ¶m);
873 fprintf(stderr, "error: %s: failed on %s\n",
878 memset(mntdir, 0, PATH_MAX);
884 static int lfs_path2fid(int argc, char **argv)
894 rc = llapi_path2fid(path, &fid);
896 fprintf(stderr, "can't get fid for %s: %s\n", path,
897 strerror(errno = -rc));
901 printf(DFID"\n", PFID(&fid));
906 #define COOK(value) \
909 while (value > 1024) { \
918 #define HDF "%8.1f%c"
922 static int showdf(char *mntdir, struct obd_statfs *stat,
923 char *uuid, int ishow, int cooked,
924 char *type, int index, int rc)
926 long long avail, used, total;
928 char *suffix = "KMGTPEZY";
929 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
930 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
938 avail = stat->os_ffree;
939 used = stat->os_files - stat->os_ffree;
940 total = stat->os_files;
942 int shift = cooked ? 0 : 10;
944 avail = (stat->os_bavail * stat->os_bsize) >> shift;
945 used = ((stat->os_blocks - stat->os_bfree) *
946 stat->os_bsize) >> shift;
947 total = (stat->os_blocks * stat->os_bsize) >> shift;
951 ratio = (double)used / (double)total;
957 cook_val = (double)total;
960 sprintf(tbuf, HDF, cook_val, suffix[i - 1]);
962 sprintf(tbuf, CDF, total);
964 cook_val = (double)used;
967 sprintf(ubuf, HDF, cook_val, suffix[i - 1]);
969 sprintf(ubuf, CDF, used);
971 cook_val = (double)avail;
974 sprintf(abuf, HDF, cook_val, suffix[i - 1]);
976 sprintf(abuf, CDF, avail);
978 sprintf(tbuf, CDF, total);
979 sprintf(ubuf, CDF, used);
980 sprintf(abuf, CDF, avail);
983 sprintf(rbuf, RDF, (int)(ratio * 100));
984 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
985 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
987 printf("[%s:%d]\n", type, index);
993 printf(UUF": inactive device\n", uuid);
996 printf(UUF": %s\n", uuid, strerror(-rc));
1003 struct ll_stat_type {
1008 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow, int cooked)
1010 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1011 struct obd_uuid uuid_buf;
1012 char *poolname = NULL;
1013 struct ll_stat_type types[] = { { LL_STATFS_MDC, "MDT" },
1014 { LL_STATFS_LOV, "OST" },
1016 struct ll_stat_type *tp;
1021 poolname = strchr(pool, '.');
1022 if (poolname != NULL) {
1023 if (strncmp(fsname, pool, strlen(fsname))) {
1024 fprintf(stderr, "filesystem name incorrect\n");
1033 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1034 "UUID", "Inodes", "IUsed", "IFree",
1035 "IUse%", "Mounted on");
1037 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1038 "UUID", cooked ? "bytes" : "1K-blocks",
1039 "Used", "Available", "Use%", "Mounted on");
1041 for (tp = types; tp->st_name != NULL; tp++) {
1042 for (index = 0; ; index++) {
1043 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1044 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1045 rc = llapi_obd_statfs(mntdir, tp->st_op, index,
1046 &stat_buf, &uuid_buf);
1050 if (poolname && tp->st_op == LL_STATFS_LOV &&
1051 llapi_search_ost(fsname, poolname,
1052 obd_uuid2str(&uuid_buf)) != 1)
1055 /* the llapi_obd_statfs() call may have returned with
1056 * an error, but if it filled in uuid_buf we will at
1057 * lease use that to print out a message for that OBD.
1058 * If we didn't even fill in uuid_buf something is
1059 * definitely incorrect and no point in continuing. */
1060 if (uuid_buf.uuid[0] != '\0') {
1061 showdf(mntdir,&stat_buf,obd_uuid2str(&uuid_buf),
1062 ishow, cooked, tp->st_name, index, rc);
1066 sprintf(tmp_uuid, "%s%04x", tp->st_name, index);
1067 showdf(mntdir, &stat_buf, tmp_uuid,
1068 ishow, cooked, tp->st_name, index, rc);
1071 if (tp->st_op == LL_STATFS_MDC) {
1072 sum.os_ffree += stat_buf.os_ffree;
1073 sum.os_files += stat_buf.os_files;
1074 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
1075 sum.os_blocks += stat_buf.os_blocks *
1077 sum.os_bfree += stat_buf.os_bfree *
1079 sum.os_bavail += stat_buf.os_bavail *
1082 } else if (rc == -EINVAL || rc == -EFAULT) {
1089 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1094 static int lfs_df(int argc, char **argv)
1096 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1097 int ishow = 0, cooked = 0;
1098 int c, rc = 0, index = 0;
1099 char fsname[PATH_MAX] = "", *pool_name = NULL;
1100 struct option long_opts[] = {
1101 {"pool", required_argument, 0, 'p'},
1106 while ((c = getopt_long(argc, argv, "ihp:", long_opts, NULL)) != -1) {
1122 if (optind < argc && !realpath(argv[optind], path)) {
1124 fprintf(stderr, "error: invalid path '%s': %s\n",
1125 argv[optind], strerror(-rc));
1129 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1130 /* Check if we have a mount point */
1131 if (mntdir[0] == '\0')
1134 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked);
1135 if (rc || path[0] != '\0')
1137 fsname[0] = '\0'; /* avoid matching in next loop */
1138 mntdir[0] = '\0'; /* avoid matching in next loop */
1144 static int lfs_check(int argc, char **argv)
1148 char mntdir[PATH_MAX] = {'\0'};
1156 obd_types[0] = obd_type1;
1157 obd_types[1] = obd_type2;
1159 if (strcmp(argv[1], "osts") == 0) {
1160 strcpy(obd_types[0], "osc");
1161 } else if (strcmp(argv[1], "mds") == 0) {
1162 strcpy(obd_types[0], "mdc");
1163 } else if (strcmp(argv[1], "servers") == 0) {
1165 strcpy(obd_types[0], "osc");
1166 strcpy(obd_types[1], "mdc");
1168 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1173 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1174 if (rc < 0 || mntdir[0] == '\0') {
1175 fprintf(stderr, "No suitable Lustre mount found\n");
1179 rc = llapi_target_iterate(num_types, obd_types,
1180 mntdir, llapi_ping_target);
1183 fprintf(stderr, "error: %s: %s status failed\n",
1190 static int lfs_catinfo(int argc, char **argv)
1192 char mntdir[PATH_MAX] = {'\0'};
1195 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1198 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1201 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1202 if (rc == 0 && mntdir[0] != '\0') {
1204 rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1206 rc = llapi_catinfo(mntdir, argv[1], NULL);
1208 fprintf(stderr, "no lustre_lite mounted.\n");
1215 int lfs_join(int argc, char **argv)
1217 char *name_head, *name_tail;
1223 name_head = argv[1];
1224 fd = open(name_head, O_WRONLY);
1226 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1230 size = lseek(fd, 0, SEEK_END);
1231 if (size % JOIN_FILE_ALIGN) {
1232 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1233 name_head, (long long)size, JOIN_FILE_ALIGN);
1237 name_tail = argv[2];
1238 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1242 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1248 #ifdef HAVE_SYS_QUOTA_H
1249 static int quota_is_2_0_server(const char *mnt)
1254 rc = llapi_get_connect_flags(mnt, &flags);
1258 if (flags & OBD_CONNECT_FID)
1264 static int lfs_quotachown(int argc, char **argv)
1271 while ((c = getopt(argc, argv, "i")) != -1) {
1277 fprintf(stderr, "error: %s: option '-%c' "
1278 "unrecognized\n", argv[0], c);
1284 rc = llapi_quotachown(argv[optind], flag);
1286 fprintf(stderr,"error: change file owner/group failed.\n");
1290 static int lfs_quotacheck(int argc, char **argv)
1292 int c, check_type = 0;
1294 struct if_quotacheck qchk;
1295 struct if_quotactl qctl;
1296 char *obd_type = (char *)qchk.obd_type;
1301 while ((c = getopt(argc, argv, "ug")) != -1) {
1310 fprintf(stderr, "error: %s: option '-%c' "
1311 "unrecognized\n", argv[0], c);
1318 else /* do quotacheck for both user & group quota by default */
1325 v2 = quota_is_2_0_server(mnt);
1329 /* For b1_8 server */
1331 memset(&qctl, 0, sizeof(qctl));
1332 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1333 qctl.qc_type = check_type;
1334 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1335 * take this parameter into account */
1336 rc = llapi_quotactl(mnt, &qctl);
1338 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1343 rc = llapi_quotacheck(mnt, check_type);
1345 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1349 memset(&qchk, 0, sizeof(qchk));
1350 rc = llapi_poll_quotacheck(mnt, &qchk);
1353 fprintf(stderr, "%s %s ", obd_type,
1354 obd_uuid2str(&qchk.obd_uuid));
1355 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1359 memset(&qctl, 0, sizeof(qctl));
1360 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1361 qctl.qc_type = check_type;
1363 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1364 * take this parameter into account */
1365 rc = llapi_quotactl(mnt, &qctl);
1367 if (v2 > 0 && errno == EALREADY) {
1368 /* This is for 2.0 server. */
1372 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1373 obd_uuid2str(&qctl.obd_uuid));
1374 fprintf(stderr, "%s turn on quota failed: %s\n",
1375 argv[0], strerror(errno));
1382 static int lfs_quotaon(int argc, char **argv)
1386 struct if_quotactl qctl;
1387 char *obd_type = (char *)qctl.obd_type;
1391 memset(&qctl, 0, sizeof(qctl));
1392 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1395 while ((c = getopt(argc, argv, "ugf")) != -1) {
1398 qctl.qc_type |= 0x01;
1401 qctl.qc_type |= 0x02;
1404 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1407 fprintf(stderr, "error: %s: option '-%c' "
1408 "unrecognized\n", argv[0], c);
1415 else /* by default, enable quota for both user & group */
1416 qctl.qc_type = 0x02;
1422 v2 = quota_is_2_0_server(mnt);
1427 qctl.qc_id = QFMT_LDISKFS; /* compatibility: 1.6.5 and earliers
1428 * take this parameter into account */
1430 rc = llapi_quotactl(mnt, &qctl);
1432 if (v2 > 0 && errno == EALREADY) {
1433 /* This is for 2.0 server. */
1435 } else if (errno == ENOENT) {
1436 fprintf(stderr, "error: cannot find quota database, "
1437 "make sure you have run quotacheck\n");
1440 fprintf(stderr, "%s %s ", obd_type,
1441 obd_uuid2str(&qctl.obd_uuid));
1442 fprintf(stderr, "%s failed: %s\n", argv[0],
1450 static int lfs_quotaoff(int argc, char **argv)
1454 struct if_quotactl qctl;
1455 char *obd_type = (char *)qctl.obd_type;
1459 memset(&qctl, 0, sizeof(qctl));
1460 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1463 while ((c = getopt(argc, argv, "ug")) != -1) {
1466 qctl.qc_type |= 0x01;
1469 qctl.qc_type |= 0x02;
1472 fprintf(stderr, "error: %s: option '-%c' "
1473 "unrecognized\n", argv[0], c);
1480 else /* by default, disable quota for both user & group */
1481 qctl.qc_type = 0x02;
1487 v2 = quota_is_2_0_server(mnt);
1491 rc = llapi_quotactl(mnt, &qctl);
1493 if ((v2 > 0 && errno == EALREADY) ||
1494 (v2 == 0 && errno == ESRCH)) {
1498 fprintf(stderr, "%s %s ", obd_type,
1499 obd_uuid2str(&qctl.obd_uuid));
1500 fprintf(stderr, "quotaoff failed: %s\n",
1508 static int lfs_quotainv(int argc, char **argv)
1512 struct if_quotactl qctl;
1515 memset(&qctl, 0, sizeof(qctl));
1516 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1519 while ((c = getopt(argc, argv, "ugf")) != -1) {
1522 qctl.qc_type |= 0x01;
1525 qctl.qc_type |= 0x02;
1528 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1531 fprintf(stderr, "error: %s: option '-%c' "
1532 "unrecognized\n", argv[0], c);
1539 else /* by default, invalidate quota for both user & group */
1540 qctl.qc_type = 0x02;
1547 rc = llapi_quotactl(mnt, &qctl);
1549 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1556 #define ARG2INT(nr, str, msg) \
1559 nr = strtol(str, &endp, 0); \
1561 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1566 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1568 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1569 * returns the value or ULONG_MAX on integer overflow or incorrect format
1571 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1572 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1573 * 3. empty integer value is interpreted as 0
1575 static unsigned long str2sec(const char* timestr)
1577 const char spec[] = "smhdw";
1578 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1579 unsigned long val = 0;
1582 if (strpbrk(timestr, spec) == NULL) {
1583 /* no specifiers inside the time string,
1584 should treat it as an integer value */
1585 val = strtoul(timestr, &tail, 10);
1586 return *tail ? ULONG_MAX : val;
1589 /* format string is XXwXXdXXhXXmXXs */
1595 v = strtoul(timestr, &tail, 10);
1596 if (v == ULONG_MAX || *tail == '\0')
1597 /* value too large (ULONG_MAX or more)
1598 or missing specifier */
1601 ptr = strchr(spec, *tail);
1603 /* unknown specifier */
1608 /* check if product will overflow the type */
1609 if (!(v < ULONG_MAX / mult[ind]))
1612 ADD_OVERFLOW(val, mult[ind] * v);
1613 if (val == ULONG_MAX)
1625 #define ARG2ULL(nr, str, defscale) \
1627 unsigned long long limit, units = 0; \
1630 rc = parse_size(str, &limit, &units, 1); \
1632 fprintf(stderr, "error: bad limit value %s\n", str); \
1635 nr = ((units == 0) ? (defscale) : 1) * limit; \
1638 static inline int has_times_option(int argc, char **argv)
1642 for (i = 1; i < argc; i++)
1643 if (!strcmp(argv[i], "-t"))
1649 int lfs_setquota_times(int argc, char **argv)
1652 struct if_quotactl qctl;
1653 char *mnt, *obd_type = (char *)qctl.obd_type;
1654 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1655 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1656 struct option long_opts[] = {
1657 {"user", no_argument, 0, 'u'},
1658 {"group", no_argument, 0, 'g'},
1659 {"block-grace", required_argument, 0, 'b'},
1660 {"inode-grace", required_argument, 0, 'i'},
1661 {"times", no_argument, 0, 't'},
1665 memset(&qctl, 0, sizeof(qctl));
1666 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1667 qctl.qc_type = UGQUOTA;
1670 /* compatibility syntax: setquota -t -[u|g] t1 t2 mnt */
1671 if (argc == 6 && !strcmp(argv[1], "-t") &&
1672 (!strcmp(argv[2], "-u") || !strcmp(argv[2], "-g")) &&
1673 argv[3][0] != '-' && argv[4][0] != '-') {
1674 fprintf(stderr, "warning: using compatibility syntax, it may not"
1675 " be available in future releases!\n");
1677 qctl.qc_type = !strcmp(argv[2], "-u") ? USRQUOTA : GRPQUOTA;
1679 if ((dqi->dqi_bgrace = str2sec(argv[3])) == ULONG_MAX) {
1680 fprintf(stderr, "error: bad block-grace: %s\n", argv[3]);
1683 if ((dqi->dqi_igrace = str2sec(argv[4])) == ULONG_MAX) {
1684 fprintf(stderr, "error: bad inode-grace: %s\n", argv[4]);
1687 dqb->dqb_valid = QIF_TIMES;
1688 mnt = argv[argc - 1];
1694 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1698 if (qctl.qc_type != UGQUOTA) {
1699 fprintf(stderr, "error: -u and -g can't be used "
1700 "more than once\n");
1703 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1706 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1707 fprintf(stderr, "error: bad block-grace: %s\n",
1711 dqb->dqb_valid |= QIF_BTIME;
1714 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1715 fprintf(stderr, "error: bad inode-grace: %s\n",
1719 dqb->dqb_valid |= QIF_ITIME;
1721 case 't': /* Yes, of course! */
1723 default: /* getopt prints error message for us when opterr != 0 */
1728 if (qctl.qc_type == UGQUOTA) {
1729 fprintf(stderr, "error: neither -u nor -g specified\n");
1733 if (optind != argc - 1) {
1734 fprintf(stderr, "error: unexpected parameters encountered\n");
1743 rc = llapi_quotactl(mnt, &qctl);
1746 fprintf(stderr, "%s %s ", obd_type,
1747 obd_uuid2str(&qctl.obd_uuid));
1748 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1755 #define BSLIMIT (1 << 0)
1756 #define BHLIMIT (1 << 1)
1757 #define ISLIMIT (1 << 2)
1758 #define IHLIMIT (1 << 3)
1760 int lfs_setquota(int argc, char **argv)
1763 struct if_quotactl qctl;
1764 char *mnt, *obd_type = (char *)qctl.obd_type;
1765 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1766 struct option long_opts[] = {
1767 {"user", required_argument, 0, 'u'},
1768 {"group", required_argument, 0, 'g'},
1769 {"block-softlimit", required_argument, 0, 'b'},
1770 {"block-hardlimit", required_argument, 0, 'B'},
1771 {"inode-softlimit", required_argument, 0, 'i'},
1772 {"inode-hardlimit", required_argument, 0, 'I'},
1775 unsigned limit_mask = 0;
1778 if (has_times_option(argc, argv))
1779 return lfs_setquota_times(argc, argv);
1781 memset(&qctl, 0, sizeof(qctl));
1782 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1783 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1784 * so it can be used as a marker that qc_type
1785 * isn't reinitialized from command line */
1788 /* compatibility syntax: [-u|-g] <user|group> b B i I mount
1789 * will be removed in the future */
1790 if (argc == 8 && (!strcmp(argv[1], "-u") || !strcmp(argv[1], "-g")) &&
1791 argv[3][0] != '-' && argv[4][0] != '-' && argv[5][0] != '-' &&
1792 argv[6][0] != '-') {
1793 fprintf(stderr, "warning: using compatibility syntax, it may not"
1794 " be available in future releases!\n");
1796 qctl.qc_type = !strcmp(argv[1], "-u") ? USRQUOTA : GRPQUOTA;
1797 rc = name2id(&qctl.qc_id, argv[2],
1798 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1800 fprintf(stderr, "error: unknown id %s\n", argv[2]);
1804 ARG2ULL(dqb->dqb_bsoftlimit, argv[3], 1024);
1805 dqb->dqb_bsoftlimit >>= 10;
1806 ARG2ULL(dqb->dqb_bhardlimit, argv[4], 1024);
1807 dqb->dqb_bhardlimit >>= 10;
1808 ARG2ULL(dqb->dqb_isoftlimit, argv[5], 1);
1809 ARG2ULL(dqb->dqb_ihardlimit, argv[6], 1);
1811 dqb->dqb_valid = QIF_LIMITS;
1812 mnt = argv[argc - 1];
1818 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1822 if (qctl.qc_type != UGQUOTA) {
1823 fprintf(stderr, "error: -u and -g can't be used"
1824 " more than once\n");
1827 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1828 rc = name2id(&qctl.qc_id, optarg,
1829 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1831 qctl.qc_id = strtoul(optarg, &endptr, 10);
1832 if (*endptr != '\0') {
1833 fprintf(stderr, "error: can't find id "
1834 "for name %s\n", optarg);
1840 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1841 dqb->dqb_bsoftlimit >>= 10;
1842 limit_mask |= BSLIMIT;
1845 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1846 dqb->dqb_bhardlimit >>= 10;
1847 limit_mask |= BHLIMIT;
1850 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1851 limit_mask |= ISLIMIT;
1854 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1855 limit_mask |= IHLIMIT;
1857 default: /* getopt prints error message for us when opterr != 0 */
1862 if (qctl.qc_type == UGQUOTA) {
1863 fprintf(stderr, "error: neither -u nor -g was specified\n");
1867 if (limit_mask == 0) {
1868 fprintf(stderr, "error: at least one limit must be specified\n");
1872 if (optind != argc - 1) {
1873 fprintf(stderr, "error: unexpected parameters encountered\n");
1879 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1880 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1881 /* sigh, we can't just set blimits/ilimits */
1882 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1883 .qc_type = qctl.qc_type,
1884 .qc_id = qctl.qc_id};
1886 rc = llapi_quotactl(mnt, &tmp_qctl);
1888 fprintf(stderr, "error: setquota failed while retrieving"
1889 " current quota settings (%s)\n",
1894 if (!(limit_mask & BHLIMIT))
1895 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1896 if (!(limit_mask & BSLIMIT))
1897 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1898 if (!(limit_mask & IHLIMIT))
1899 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1900 if (!(limit_mask & ISLIMIT))
1901 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1903 /* Keep grace times if we have got no softlimit arguments */
1904 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
1905 dqb->dqb_valid |= QIF_BTIME;
1906 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
1909 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
1910 dqb->dqb_valid |= QIF_ITIME;
1911 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
1915 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1916 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1921 rc = llapi_quotactl(mnt, &qctl);
1924 fprintf(stderr, "%s %s ", obd_type,
1925 obd_uuid2str(&qctl.obd_uuid));
1926 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1933 static inline char *type2name(int check_type)
1935 if (check_type == USRQUOTA)
1937 else if (check_type == GRPQUOTA)
1944 /* Converts seconds value into format string
1945 * result is returned in buf
1947 * 1. result is in descenting order: 1w2d3h4m5s
1948 * 2. zero fields are not filled (except for p. 3): 5d1s
1949 * 3. zero seconds value is presented as "0s"
1951 static void sec2str(time_t seconds, char *buf)
1953 const char spec[] = "smhdw";
1954 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1959 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1960 c = seconds / mult[i];
1962 if (c > 0 || (i == 0 && buf == tail))
1963 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1970 static void diff2str(time_t seconds, char *buf, time_t now)
1976 if (seconds <= now) {
1977 strcpy(buf, "none");
1980 sec2str(seconds - now, buf);
1983 static void print_quota_title(char *name, struct if_quotactl *qctl)
1985 printf("Disk quotas for %s %s (%cid %u):\n",
1986 type2name(qctl->qc_type), name,
1987 *type2name(qctl->qc_type), qctl->qc_id);
1988 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1990 "kbytes", "quota", "limit", "grace",
1991 "files", "quota", "limit", "grace");
1994 #define GENERAL_QUOTA_INFO 1
1995 #define MDS_QUOTA_INFO 2
1996 #define OST_QUOTA_INFO 3
1998 static void print_quota(char *mnt, struct if_quotactl *qctl, int type)
2004 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
2005 int bover = 0, iover = 0;
2006 struct obd_dqblk *dqb = &qctl->qc_dqblk;
2008 if (dqb->dqb_bhardlimit &&
2009 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
2011 } else if (dqb->dqb_bsoftlimit &&
2012 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
2013 if (dqb->dqb_btime > now) {
2020 if (dqb->dqb_ihardlimit &&
2021 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2023 } else if (dqb->dqb_isoftlimit &&
2024 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
2025 if (dqb->dqb_btime > now) {
2032 #if 0 /* XXX: always print quotas even when no usages */
2033 if (dqb->dqb_curspace || dqb->dqb_curinodes)
2039 if (strlen(mnt) > 15)
2040 printf("%s\n%15s", mnt, "");
2042 printf("%15s", mnt);
2045 diff2str(dqb->dqb_btime, timebuf, now);
2046 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2047 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
2048 if (type == GENERAL_QUOTA_INFO)
2049 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2050 ? LPU64 : "["LPU64"]",
2051 dqb->dqb_bsoftlimit);
2053 sprintf(numbuf[1], "%s", "-");
2054 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2055 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2056 printf(" %7s%c %6s %7s %7s",
2057 numbuf[0], bover ? '*' : ' ', numbuf[1],
2058 numbuf[2], bover > 1 ? timebuf : "-");
2061 diff2str(dqb->dqb_itime, timebuf, now);
2063 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2064 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2065 if (type == GENERAL_QUOTA_INFO)
2066 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2067 ? LPU64 : "["LPU64"]",
2068 dqb->dqb_isoftlimit);
2070 sprintf(numbuf[1], "%s", "-");
2071 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2072 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2073 if (type != OST_QUOTA_INFO)
2074 printf(" %7s%c %6s %7s %7s",
2075 numbuf[0], iover ? '*' : ' ', numbuf[1],
2076 numbuf[2], iover > 1 ? timebuf : "-");
2078 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
2081 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2082 qctl->qc_cmd == Q_GETOINFO) {
2086 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2087 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2088 printf("Block grace time: %s; Inode grace time: %s\n",
2089 bgtimebuf, igtimebuf);
2093 static int print_mds_quota(char *mnt, struct if_quotactl *qctl)
2097 /* XXX: this is a flag to mark that only mds quota is wanted */
2098 qctl->qc_dqblk.dqb_valid = 1;
2099 rc = llapi_quotactl(mnt, qctl);
2101 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
2105 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, MDS_QUOTA_INFO);
2109 static int print_lov_quota(char *mnt, struct if_quotactl *qctl)
2112 struct obd_uuid *uuids = NULL, *uuidp;
2113 int obdcount = 1024;
2114 int i, rc = 0, rc1 = 0;
2118 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
2122 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
2123 sizeof(struct obd_uuid));
2128 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
2130 struct obd_uuid *uuids_temp;
2132 if (rc == -EOVERFLOW) {
2133 uuids_temp = realloc(uuids, obdcount *
2134 sizeof(struct obd_uuid));
2135 if (uuids_temp != NULL)
2136 goto retry_get_uuids;
2141 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
2145 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
2146 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
2148 /* XXX clear this flag to get quota from osts */
2149 qctl->qc_dqblk.dqb_valid = 0;
2150 rc = llapi_quotactl(mnt, qctl);
2154 fprintf(stderr, "%s quotactl failed: %s\n",
2155 uuidp->uuid, strerror(errno));
2159 print_quota((char *)uuidp->uuid, qctl, OST_QUOTA_INFO);
2169 static int lfs_quota(int argc, char **argv)
2172 char *mnt, *name = NULL;
2173 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2174 .qc_type = UGQUOTA };
2175 char *obd_type = (char *)qctl.obd_type;
2176 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2177 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, inacc, quiet = 0;
2182 while ((c = getopt(argc, argv, "ugto:qv")) != -1) {
2185 if (qctl.qc_type != UGQUOTA) {
2186 fprintf(stderr, "error: use either -u or -g\n");
2189 qctl.qc_type = USRQUOTA;
2192 if (qctl.qc_type != UGQUOTA) {
2193 fprintf(stderr, "error: use either -u or -g\n");
2196 qctl.qc_type = GRPQUOTA;
2199 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2202 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2211 fprintf(stderr, "error: %s: option '-%c' "
2212 "unrecognized\n", argv[0], c);
2217 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2218 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2219 optind == argc - 1) {
2221 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2222 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2224 qctl.qc_type = USRQUOTA;
2225 qctl.qc_id = geteuid();
2227 qctl.qc_type = GRPQUOTA;
2228 qctl.qc_id = getegid();
2230 rc = id2name(&name, qctl.qc_id,
2231 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2234 /* lfs quota -u username /path/to/lustre/mount */
2235 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2236 /* options should be followed by u/g-name and mntpoint */
2237 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2238 fprintf(stderr, "error: missing quota argument(s)\n");
2242 name = argv[optind++];
2243 rc = name2id(&qctl.qc_id, name,
2244 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2246 qctl.qc_id = strtoul(name, &endptr, 10);
2247 if (*endptr != '\0') {
2248 fprintf(stderr, "error: can't find id for name "
2253 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2254 fprintf(stderr, "error: missing quota info argument(s)\n");
2260 rc1 = llapi_quotactl(mnt, &qctl);
2264 fprintf(stderr, "Permission denied.\n");
2266 /* We already got a "No such file..." message. */
2269 fprintf(stderr, "%s quotas are not enabled.\n",
2270 qctl.qc_type == USRQUOTA?"user":"group");
2274 fprintf(stderr, "Unexpected quotactl error: %s\n",
2279 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
2280 print_quota_title(name, &qctl);
2282 if (rc1 && *obd_type)
2283 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2288 inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
2289 ((qctl.qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) != (QIF_LIMITS|QIF_USAGE));
2291 print_quota(mnt, &qctl, GENERAL_QUOTA_INFO);
2293 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2294 rc2 = print_mds_quota(mnt, &qctl);
2295 rc3 = print_lov_quota(mnt, &qctl);
2298 if (rc1 || rc2 || rc3 || inacc)
2299 printf("Some errors happened when getting quota info. "
2300 "Some devices may be not working or deactivated. "
2301 "The data in \"[]\" is inaccurate.\n");
2309 #endif /* HAVE_SYS_QUOTA_H! */
2311 int main(int argc, char **argv)
2317 ptl_initialize(argc, argv);
2318 if (obd_initialize(argc, argv) < 0)
2320 if (dbg_initialize(argc, argv) < 0)
2323 Parser_init("lfs > ", cmdlist);
2326 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2328 rc = Parser_commands();
2331 obd_finalize(argc, argv);
2332 return rc < 0 ? -rc : rc;