1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Robert Read <rread@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <sys/types.h>
41 #include <lnet/api-support.h>
42 #include <lnet/lnetctl.h>
44 #include <liblustre.h>
45 #include <lustre/lustre_idl.h>
46 #include <lustre/liblustreapi.h>
47 #include <lustre/lustre_user.h>
52 unsigned int libcfs_subsystem_debug = 0;
55 static int lfs_setstripe(int argc, char **argv);
56 static int lfs_find(int argc, char **argv);
57 static int lfs_getstripe(int argc, char **argv);
58 static int lfs_osts(int argc, char **argv);
59 static int lfs_df(int argc, char **argv);
60 static int lfs_check(int argc, char **argv);
61 static int lfs_catinfo(int argc, char **argv);
62 #ifdef HAVE_QUOTA_SUPPORT
63 static int lfs_quotachown(int argc, char **argv);
64 static int lfs_quotacheck(int argc, char **argv);
65 static int lfs_quotaon(int argc, char **argv);
66 static int lfs_quotaoff(int argc, char **argv);
67 static int lfs_setquota(int argc, char **argv);
68 static int lfs_quota(int argc, char **argv);
69 static int lfs_quotainv(int argc, char **argv);
71 static int lfs_join(int argc, char **argv);
73 /* all avaialable commands */
74 command_t cmdlist[] = {
75 {"setstripe", lfs_setstripe, 0,
76 "Create a new file with a specific striping pattern or\n"
77 "set the default striping pattern on an existing directory or\n"
78 "delete the default striping pattern from an existing directory\n"
79 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
81 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
82 " [--index|-i stripe_index]\n"
83 " [--count|-c stripe_count]\n"
85 " setstripe -d <dirname> (to delete default striping)\n"
86 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
87 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
88 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
89 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
90 {"getstripe", lfs_getstripe, 0,
91 "To list the striping info for a given filename or files in a\n"
92 "directory or recursively for all files in a directory tree.\n"
93 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
94 " [--recursive | -r] <dir|file> ..."},
96 "To find files that match given parameters recursively in a directory tree.\n"
97 "usage: find <dir/file> ... \n"
98 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
99 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
100 " [--print|-p] [--obd|-O <uuid>] [[!] --type|-t <filetype>]\n"
101 "\t !: used before an option indicates 'NOT' the requested attribute\n"
102 "\t -: used before an value indicates 'AT MOST' the requested value\n"
103 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
104 {"check", lfs_check, 0,
105 "Display the status of MDS or OSTs (as specified in the command)\n"
106 "or all the servers (MDS and OSTs).\n"
107 "usage: check <osts|mds|servers>"},
108 {"catinfo", lfs_catinfo, 0,
109 "Show information of specified type logs.\n"
110 "usage: catinfo {keyword} [node name]\n"
111 "\tkeywords are one of followings: config, deletions.\n"
112 "\tnode name must be provided when use keyword config."},
113 {"join", lfs_join, 0,
114 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
115 "usage: join <filename_A> <filename_B>\n"},
116 {"osts", lfs_osts, 0, "osts"},
118 "report filesystem disk space usage or inodes usage"
120 "Usage: df [-i] [-h] [path]"},
121 #ifdef HAVE_QUOTA_SUPPORT
122 {"quotachown",lfs_quotachown, 0,
123 "Change files' owner or group on the specified filesystem.\n"
124 "usage: quotachown [-i] <filesystem>\n"
125 "\t-i: ignore error if file is not exist\n"},
126 {"quotacheck", lfs_quotacheck, 0,
127 "Scan the specified filesystem for disk usage, and create,\n"
128 "or update quota files.\n"
129 "usage: quotacheck [ -ug ] <filesystem>"},
130 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
131 "usage: quotaon [ -ugf ] <filesystem>"},
132 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
133 "usage: quotaoff [ -ug ] <filesystem>"},
134 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
135 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
136 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
137 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
138 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
139 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
140 "usage: quotainv [-u|-g] <filesystem>"},
142 {"help", Parser_help, 0, "help"},
143 {"exit", Parser_quit, 0, "quit"},
144 {"quit", Parser_quit, 0, "quit"},
149 static int lfs_setstripe(int argc, char **argv)
153 unsigned long long st_size;
154 int st_offset, st_count;
158 char *stripe_size_arg = NULL;
159 char *stripe_off_arg = NULL;
160 char *stripe_count_arg = NULL;
161 unsigned long long size_units;
163 struct option long_opts[] = {
164 {"size", required_argument, 0, 's'},
165 {"count", required_argument, 0, 'c'},
166 {"index", required_argument, 0, 'i'},
167 {"delete", no_argument, 0, 'd'},
174 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
175 /* for compatibility with the existing positional parameter
179 } else if (argc == 5 &&
180 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
181 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
182 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
183 /* for compatibility with the existing positional parameter
186 stripe_size_arg = argv[2];
187 stripe_off_arg = argv[3];
188 stripe_count_arg = argv[4];
192 while ((c = getopt_long(argc, argv, "c:di:s:",
193 long_opts, NULL)) >= 0) {
199 stripe_count_arg = optarg;
202 /* delete the default striping pattern */
206 stripe_off_arg = optarg;
209 stripe_size_arg = optarg;
214 fprintf(stderr, "error: %s: option '%s' "
216 argv[0], argv[optind - 1]);
222 fname = argv[optind];
227 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
228 stripe_count_arg != NULL)) {
229 fprintf(stderr, "error: %s: cannot specify -d with "
230 "-s, -c or -i options\n",
236 if (optind != argc - 1) {
237 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
239 argv[0], argv[argc-1]);
243 /* get the stripe size */
244 if (stripe_size_arg != NULL) {
245 result = parse_size(stripe_size_arg, &st_size, &size_units);
247 fprintf(stderr,"error: bad size '%s'\n",
252 /* get the stripe offset */
253 if (stripe_off_arg != NULL) {
254 st_offset = strtoul(stripe_off_arg, &end, 0);
256 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
257 argv[0], stripe_off_arg);
261 /* get the stripe count */
262 if (stripe_count_arg != NULL) {
263 st_count = strtoul(stripe_count_arg, &end, 0);
265 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
266 argv[0], stripe_count_arg);
271 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
273 fprintf(stderr, "error: %s: create stripe file failed\n",
279 static int set_time(time_t *time, time_t *set, char *str)
286 else if (str[0] == '-')
292 t = strtol(str, NULL, 0);
293 if (*time < t * 24 * 60 * 60) {
296 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
300 *set = *time - t * 24 * 60 * 60;
304 static int lfs_find(int argc, char **argv)
309 struct find_param param = { .maxdepth = -1 };
311 struct option long_opts[] = {
312 /* New find options. */
313 {"atime", required_argument, 0, 'A'},
314 {"ctime", required_argument, 0, 'C'},
315 {"mtime", required_argument, 0, 'M'},
316 {"maxdepth", required_argument, 0, 'D'},
317 {"name", required_argument, 0, 'n'},
318 /* --obd is considered as a new option. */
319 {"obd", required_argument, 0, 'O'},
320 {"ost", required_argument, 0, 'O'},
321 {"print", no_argument, 0, 'P'},
322 {"print0", no_argument, 0, 'p'},
323 /* Old find options. */
324 {"quiet", no_argument, 0, 'q'},
325 {"recursive", no_argument, 0, 'r'},
326 {"size", required_argument, 0, 's'},
327 {"type", required_argument, 0, 't'},
328 {"verbose", no_argument, 0, 'v'},
341 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrs:t:v",
342 long_opts, NULL)) >= 0) {
347 /* '!' is part of option */
348 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
349 if (!isoption && pathend != -1) {
350 fprintf(stderr, "err: %s: filename|dirname must either "
351 "precede options or follow options\n",
355 if (!isoption && pathstart == -1)
356 pathstart = optind - 1;
357 if (isoption && pathstart != -1 && pathend == -1) {
358 pathend = optind - 2;
359 if ((c == 1 && strcmp(optarg, "!") == 0) ||
360 c == 'P' || c == 'p' || c == 'O' ||
361 c == 'q' || c == 'r' || c == 'v')
362 pathend = optind - 1;
369 if (strcmp(optarg, "!") == 0)
373 xtime = ¶m.atime;
374 xsign = ¶m.asign;
377 xtime = ¶m.ctime;
378 xsign = ¶m.csign;
382 xtime = ¶m.mtime;
383 xsign = ¶m.msign;
387 if (optarg[0] == '-')
389 else if (optarg[0] == '+')
398 ret = set_time(&t, xtime, optarg);
406 param.maxdepth = strtol(optarg, 0, 0);
410 param.pattern = (char *)optarg;
411 param.exclude_pattern = !!neg_opt;
414 char *buf, *token, *next, *p;
417 len = strlen((char *)optarg);
421 strcpy(buf, (char *)optarg);
423 if (param.num_alloc_obds == 0) {
424 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
425 sizeof(struct obd_uuid));
426 if (param.obduuid == NULL)
428 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
431 for (token = buf; token && *token; token = next) {
432 p = strchr(token, ',');
438 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
462 param.exclude_type = !!neg_opt;
464 case 'b': param.type = S_IFBLK; break;
465 case 'c': param.type = S_IFCHR; break;
466 case 'd': param.type = S_IFDIR; break;
467 case 'f': param.type = S_IFREG; break;
468 case 'l': param.type = S_IFLNK; break;
469 case 'p': param.type = S_IFIFO; break;
470 case 's': param.type = S_IFSOCK; break;
471 #ifdef S_IFDOOR /* Solaris only */
472 case 'D': param.type = S_IFDOOR; break;
474 default: fprintf(stderr, "error: %s: bad type '%s'\n",
481 if (optarg[0] == '-')
483 else if (optarg[0] == '+')
492 if (optarg[0] == '+')
493 param.size_sign = -1;
494 else if (optarg[0] == '-')
495 param.size_sign = +1;
499 ret = parse_size(optarg, ¶m.size,¶m.size_units);
501 fprintf(stderr,"error: bad size '%s'\n",
505 param.size_check = 1;
515 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
516 argv[0], argv[optind - 1]);
521 if (pathstart == -1) {
522 fprintf(stderr, "error: %s: no filename|pathname\n",
525 } else if (pathend == -1) {
533 static int deprecated_warning;
534 if (!deprecated_warning) {
535 fprintf(stderr, "lfs find: -q, -r, -v options "
536 "deprecated. Use 'lfs getstripe' instead.\n");
537 deprecated_warning = 1;
539 if (!param.recursive && param.maxdepth == -1)
545 ret = llapi_find(argv[pathstart], ¶m);
547 ret = llapi_getstripe(argv[pathstart], ¶m);
548 } while (++pathstart < pathend && !ret);
551 fprintf(stderr, "error: %s failed for %s.\n",
552 argv[0], argv[optind - 1]);
554 if (param.obduuid && param.num_alloc_obds)
560 static int lfs_getstripe(int argc, char **argv)
562 struct option long_opts[] = {
564 {"quiet", 0, 0, 'q'},
565 {"recursive", 0, 0, 'r'},
566 {"verbose", 0, 0, 'v'},
569 char short_opts[] = "hO:qrv";
571 struct find_param param = { 0 };
574 while ((c = getopt_long(argc, argv, short_opts,
575 long_opts, NULL)) != -1) {
580 "error: %s: only one obduuid allowed",
584 param.obduuid = (struct obd_uuid *)optarg;
600 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
601 argv[0], argv[optind - 1]);
609 param.maxdepth = param.recursive ? -1 : 1;
612 rc = llapi_getstripe(argv[optind], ¶m);
613 } while (++optind < argc && !rc);
616 fprintf(stderr, "error: %s failed for %s.\n",
617 argv[0], argv[optind - 1]);
621 static int lfs_osts(int argc, char **argv)
624 struct mntent *mnt = NULL;
625 struct obd_uuid *obduuid = NULL;
626 struct find_param param;
632 fp = setmntent(MOUNTED, "r");
635 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
639 memset(¶m, 0, sizeof(param));
640 param.obduuid = obduuid;
641 while (feof(fp) == 0 && ferror(fp) ==0) {
642 if (llapi_is_lustre_mnt(mnt)) {
643 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
646 "error: %s: failed on %s\n",
647 argv[0], mnt->mnt_dir);
657 #define COOK(value) \
660 while (value > 1024) { \
674 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
676 char rpath[PATH_MAX] = {'\0'};
678 int rc, len, out_len = 0;
680 if (!realpath(path, rpath)) {
682 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
683 path, strerror(-rc));
689 while (feof(fp) == 0 && ferror(fp) == 0) {
690 if (llapi_is_lustre_mnt(mnt)) {
691 len = strlen(mnt->mnt_dir);
693 !strncmp(rpath, mnt->mnt_dir, len)) {
695 memset(mntdir, 0, dir_len);
696 strncpy(mntdir, mnt->mnt_dir, dir_len);
705 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
709 static int showdf(char *mntdir, struct obd_statfs *stat,
710 char *uuid, int ishow, int cooked,
711 char *type, int index, int rc)
713 long long avail, used, total;
715 char *suffix = "KMGTPEZY";
716 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
717 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
725 avail = stat->os_ffree;
726 used = stat->os_files - stat->os_ffree;
727 total = stat->os_files;
729 int shift = cooked ? 0 : 10;
731 avail = (stat->os_bavail * stat->os_bsize) >> shift;
732 used = ((stat->os_blocks - stat->os_bfree) *
733 stat->os_bsize) >> shift;
734 total = (stat->os_blocks * stat->os_bsize) >> shift;
738 ratio = (double)used / (double)total;
744 cook_val = (double)total;
747 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
749 sprintf(tbuf, CDF, total);
751 cook_val = (double)used;
754 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
756 sprintf(ubuf, CDF, used);
758 cook_val = (double)avail;
761 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
763 sprintf(abuf, CDF, avail);
765 sprintf(tbuf, CDF, total);
766 sprintf(ubuf, CDF, used);
767 sprintf(abuf, CDF, avail);
770 sprintf(rbuf, RDF, (int)(ratio * 100));
771 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
772 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
774 printf("[%s:%d]\n", type, index);
780 printf(UUF": inactive device\n", uuid);
783 printf(UUF": %s\n", uuid, strerror(-rc));
790 static int mntdf(char *mntdir, int ishow, int cooked)
792 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
793 struct obd_uuid uuid_buf;
798 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
799 "UUID", "Inodes", "IUsed", "IFree",
800 "IUse%", "Mounted on");
802 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
803 "UUID", cooked ? "bytes" : "1K-blocks",
804 "Used", "Available", "Use%", "Mounted on");
806 for (index = 0; ; index++) {
807 memset(&stat_buf, 0, sizeof(struct obd_statfs));
808 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
809 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
810 &stat_buf, &uuid_buf);
817 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
818 rc == -ENODATA || rc == 0) {
819 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
820 ishow, cooked, "MDT", index, rc);
823 "error: llapi_obd_statfs(%s): %s (%d)\n",
824 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
828 sum.os_ffree += stat_buf.os_ffree;
829 sum.os_files += stat_buf.os_files;
833 for (index = 0; ; index++) {
834 memset(&stat_buf, 0, sizeof(struct obd_statfs));
835 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
836 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
837 &stat_buf, &uuid_buf);
844 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
845 rc == -ENODATA || rc == 0) {
846 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
847 ishow, cooked, "OST", index, rc);
850 "error: llapi_obd_statfs failed: %s (%d)\n",
855 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
856 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
857 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
862 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
867 static int lfs_df(int argc, char **argv)
871 struct mntent *mnt = NULL;
872 char mntdir[PATH_MAX] = {'\0'};
873 int ishow = 0, cooked = 0;
877 while ((c = getopt(argc, argv, "ih")) != -1) {
892 fp = setmntent(MOUNTED, "r");
895 fprintf(stderr, "error: %s: open %s failed( %s )\n",
896 argv[0], MOUNTED, strerror(errno));
900 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
906 rc = mntdf(mntdir, ishow, cooked);
911 while (feof(fp) == 0 && ferror(fp) == 0) {
912 if (llapi_is_lustre_mnt(mnt)) {
913 rc = mntdf(mnt->mnt_dir, ishow, cooked);
926 static int lfs_check(int argc, char **argv)
930 struct mntent *mnt = NULL;
939 obd_types[0] = obd_type1;
940 obd_types[1] = obd_type2;
942 if (strcmp(argv[1], "osts") == 0) {
943 strcpy(obd_types[0], "osc");
944 } else if (strcmp(argv[1], "mds") == 0) {
945 strcpy(obd_types[0], "mdc");
946 } else if (strcmp(argv[1], "servers") == 0) {
948 strcpy(obd_types[0], "osc");
949 strcpy(obd_types[1], "mdc");
951 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
956 fp = setmntent(MOUNTED, "r");
958 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
962 while (feof(fp) == 0 && ferror(fp) ==0) {
963 if (llapi_is_lustre_mnt(mnt))
971 fprintf(stderr, "No suitable Lustre mount found\n");
975 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
978 fprintf(stderr, "error: %s: %s status failed\n",
985 static int lfs_catinfo(int argc, char **argv)
988 struct mntent *mnt = NULL;
991 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
994 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
997 fp = setmntent(MOUNTED, "r");
999 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1002 mnt = getmntent(fp);
1003 while (feof(fp) == 0 && ferror(fp) == 0) {
1004 if (llapi_is_lustre_mnt(mnt))
1006 mnt = getmntent(fp);
1013 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1015 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1017 fprintf(stderr, "no lustre_lite mounted.\n");
1024 int lfs_join(int argc, char **argv)
1026 char *name_head, *name_tail;
1032 name_head = argv[1];
1033 fd = open(name_head, O_WRONLY);
1035 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1039 size = lseek(fd, 0, SEEK_END);
1040 if (size % JOIN_FILE_ALIGN) {
1041 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1042 name_head, (long long)size, JOIN_FILE_ALIGN);
1046 name_tail = argv[2];
1047 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1051 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1057 #ifdef HAVE_QUOTA_SUPPORT
1058 static int lfs_quotachown(int argc, char **argv)
1065 while ((c = getopt(argc, argv, "i")) != -1) {
1071 fprintf(stderr, "error: %s: option '-%c' "
1072 "unrecognized\n", argv[0], c);
1078 rc = llapi_quotachown(argv[optind], flag);
1080 fprintf(stderr,"error: change file owner/group failed.\n");
1085 static int lfs_quotacheck(int argc, char **argv)
1087 int c, check_type = 0;
1089 struct if_quotacheck qchk;
1090 struct if_quotactl qctl;
1091 char *obd_type = (char *)qchk.obd_type;
1094 memset(&qchk, 0, sizeof(qchk));
1097 while ((c = getopt(argc, argv, "ug")) != -1) {
1106 fprintf(stderr, "error: %s: option '-%c' "
1107 "unrecognized\n", argv[0], c);
1114 else /* do quotacheck for both user & group quota by default */
1122 memset(&qctl, 0, sizeof(qctl));
1123 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1124 qctl.qc_id = QFMT_LDISKFS;
1125 qctl.qc_type = check_type;
1126 rc = llapi_quotactl(mnt, &qctl);
1128 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1132 rc = llapi_quotacheck(mnt, check_type);
1134 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1138 rc = llapi_poll_quotacheck(mnt, &qchk);
1141 fprintf(stderr, "%s %s ", obd_type,
1142 obd_uuid2str(&qchk.obd_uuid));
1143 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1147 memset(&qctl, 0, sizeof(qctl));
1148 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1149 qctl.qc_id = QFMT_LDISKFS;
1150 qctl.qc_type = check_type;
1151 rc = llapi_quotactl(mnt, &qctl);
1154 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1155 obd_uuid2str(&qctl.obd_uuid));
1156 fprintf(stderr, "%s turn on quota failed: %s\n",
1157 argv[0], strerror(errno));
1164 static int lfs_quotaon(int argc, char **argv)
1168 struct if_quotactl qctl;
1169 char *obd_type = (char *)qctl.obd_type;
1172 memset(&qctl, 0, sizeof(qctl));
1173 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1174 qctl.qc_id = QFMT_LDISKFS;
1177 while ((c = getopt(argc, argv, "ugf")) != -1) {
1180 qctl.qc_type |= 0x01;
1183 qctl.qc_type |= 0x02;
1186 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1189 fprintf(stderr, "error: %s: option '-%c' "
1190 "unrecognized\n", argv[0], c);
1197 else /* by default, enable quota for both user & group */
1198 qctl.qc_type = 0x02;
1205 rc = llapi_quotactl(mnt, &qctl);
1208 fprintf(stderr, "%s %s ", obd_type,
1209 obd_uuid2str(&qctl.obd_uuid));
1210 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1217 static int lfs_quotaoff(int argc, char **argv)
1221 struct if_quotactl qctl;
1222 char *obd_type = (char *)qctl.obd_type;
1225 memset(&qctl, 0, sizeof(qctl));
1226 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1229 while ((c = getopt(argc, argv, "ug")) != -1) {
1232 qctl.qc_type |= 0x01;
1235 qctl.qc_type |= 0x02;
1238 fprintf(stderr, "error: %s: option '-%c' "
1239 "unrecognized\n", argv[0], c);
1246 else /* by default, disable quota for both user & group */
1247 qctl.qc_type = 0x02;
1254 rc = llapi_quotactl(mnt, &qctl);
1255 if (rc == -1 && errno == ESRCH) {
1256 fprintf(stderr, "\n%s quotas are not enabled.\n",
1257 qctl.qc_type == 0x00 ? "user" : "group");
1262 fprintf(stderr, "%s %s ", obd_type,
1263 obd_uuid2str(&qctl.obd_uuid));
1264 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1271 static int lfs_quotainv(int argc, char **argv)
1275 struct if_quotactl qctl;
1278 memset(&qctl, 0, sizeof(qctl));
1279 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1282 while ((c = getopt(argc, argv, "ug")) != -1) {
1285 qctl.qc_type |= 0x01;
1288 qctl.qc_type |= 0x02;
1291 fprintf(stderr, "error: %s: option '-%c' "
1292 "unrecognized\n", argv[0], c);
1299 else /* by default, invalidate quota for both user & group */
1300 qctl.qc_type = 0x02;
1307 rc = llapi_quotactl(mnt, &qctl);
1309 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1316 static int name2id(unsigned int *id, char *name, int type)
1318 if (type == USRQUOTA) {
1319 struct passwd *entry;
1321 if (!(entry = getpwnam(name))) {
1327 *id = entry->pw_uid;
1329 struct group *entry;
1331 if (!(entry = getgrnam(name))) {
1337 *id = entry->gr_gid;
1343 static int id2name(char **name, unsigned int id, int type)
1345 if (type == USRQUOTA) {
1346 struct passwd *entry;
1348 if (!(entry = getpwuid(id))) {
1354 *name = entry->pw_name;
1356 struct group *entry;
1358 if (!(entry = getgrgid(id))) {
1364 *name = entry->gr_name;
1370 #define ARG2INT(nr, str, msg) \
1373 nr = strtol(str, &endp, 0); \
1375 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1380 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1382 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1383 * returns the value or ULONG_MAX on integer overflow or incorrect format
1385 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1386 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1387 * 3. empty integer value is interpreted as 0
1390 static unsigned long str2sec(const char* timestr) {
1391 const char spec[] = "smhdw";
1392 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1393 unsigned long val = 0;
1396 if (strpbrk(timestr, spec) == NULL) {
1397 /* no specifiers inside the time string,
1398 should treat it as an integer value */
1399 val = strtoul(timestr, &tail, 10);
1400 return *tail ? ULONG_MAX : val;
1403 /* format string is XXwXXdXXhXXmXXs */
1409 v = strtoul(timestr, &tail, 10);
1410 if (v == ULONG_MAX || *tail == '\0')
1411 /* value too large (ULONG_MAX or more)
1412 or missing specifier */
1415 ptr = strchr(spec, *tail);
1417 /* unknown specifier */
1422 /* check if product will overflow the type */
1423 if (!(v < ULONG_MAX / mult[ind]))
1426 ADD_OVERFLOW(val, mult[ind] * v);
1427 if (val == ULONG_MAX)
1439 #define ARG2ULL(nr, str, msg) \
1442 nr = strtoull(str, &endp, 0); \
1444 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1450 int lfs_setquota(int argc, char **argv)
1454 struct if_quotactl qctl;
1455 char *obd_type = (char *)qctl.obd_type;
1458 memset(&qctl, 0, sizeof(qctl));
1459 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1462 while ((c = getopt(argc, argv, "ugt")) != -1) {
1465 qctl.qc_type |= 0x01;
1468 qctl.qc_type |= 0x02;
1471 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1474 fprintf(stderr, "error: %s: option '-%c' "
1475 "unrecognized\n", argv[0], c);
1483 if (qctl.qc_type == UGQUOTA) {
1484 fprintf(stderr, "error: user and group quotas can't be set "
1489 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1490 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1492 if (optind + 6 != argc)
1495 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1497 fprintf(stderr, "error: find id for name %s failed: %s\n",
1498 argv[optind - 1], strerror(errno));
1502 ARG2ULL(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1503 ARG2ULL(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1504 ARG2ULL(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1505 ARG2ULL(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1507 dqb->dqb_valid = QIF_LIMITS;
1509 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1511 if (optind + 3 != argc)
1514 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1515 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1518 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1519 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1526 rc = llapi_quotactl(mnt, &qctl);
1529 fprintf(stderr, "%s %s ", obd_type,
1530 obd_uuid2str(&qctl.obd_uuid));
1531 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1538 static inline char *type2name(int check_type)
1540 if (check_type == USRQUOTA)
1542 else if (check_type == GRPQUOTA)
1549 /* Converts seconds value into format string
1550 * result is returned in buf
1552 * 1. result is in descenting order: 1w2d3h4m5s
1553 * 2. zero fields are not filled (except for p. 3): 5d1s
1554 * 3. zero seconds value is presented as "0s"
1556 static void sec2str(time_t seconds, char *buf)
1558 const char spec[] = "smhdw";
1559 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1564 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1565 c = seconds / mult[i];
1567 if (c > 0 || (i == 0 && buf == tail))
1568 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1575 static void diff2str(time_t seconds, char *buf, time_t now)
1581 if (seconds <= now) {
1582 strcpy(buf, "none");
1585 sec2str(seconds - now, buf);
1588 static void print_quota_title(char *name, struct if_quotactl *qctl)
1590 printf("Disk quotas for %s %s (%cid %u):\n",
1591 type2name(qctl->qc_type), name,
1592 *type2name(qctl->qc_type), qctl->qc_id);
1593 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1595 "kbytes", "quota", "limit", "grace",
1596 "files", "quota", "limit", "grace");
1599 #define GENERAL_QUOTA_INFO 1
1600 #define MDS_QUOTA_INFO 2
1601 #define OST_QUOTA_INFO 3
1603 static void print_quota(char *mnt, struct if_quotactl *qctl, int type)
1609 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1610 int bover = 0, iover = 0;
1611 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1613 if (dqb->dqb_bhardlimit &&
1614 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1616 } else if (dqb->dqb_bsoftlimit &&
1617 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1618 if (dqb->dqb_btime > now) {
1625 if (dqb->dqb_ihardlimit &&
1626 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1628 } else if (dqb->dqb_isoftlimit &&
1629 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1630 if (dqb->dqb_btime > now) {
1637 #if 0 /* XXX: always print quotas even when no usages */
1638 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1644 if (strlen(mnt) > 15)
1645 printf("%s\n%15s", mnt, "");
1647 printf("%15s", mnt);
1650 diff2str(dqb->dqb_btime, timebuf, now);
1651 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
1652 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
1653 if (type == GENERAL_QUOTA_INFO)
1654 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
1655 ? LPU64 : "["LPU64"]",
1656 dqb->dqb_bsoftlimit);
1658 sprintf(numbuf[1], "%s", "");
1659 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
1660 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
1661 printf(" %7s%c %6s %7s %7s",
1662 numbuf[0], bover ? '*' : ' ', numbuf[1],
1663 numbuf[2], bover > 1 ? timebuf : "");
1666 diff2str(dqb->dqb_itime, timebuf, now);
1668 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
1669 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
1670 if (type == GENERAL_QUOTA_INFO)
1671 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
1672 ? LPU64 : "["LPU64"]",
1673 dqb->dqb_isoftlimit);
1675 sprintf(numbuf[1], "%s", "");
1676 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
1677 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
1678 if (type != OST_QUOTA_INFO)
1679 printf(" %7s%c %6s %7s %7s",
1680 numbuf[0], iover ? '*' : ' ', numbuf[1],
1681 numbuf[2], iover > 1 ? timebuf : "");
1684 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1685 qctl->qc_cmd == Q_GETOINFO) {
1689 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1690 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1691 printf("Block grace time: %s; Inode grace time: %s\n",
1692 bgtimebuf, igtimebuf);
1696 static int print_mds_quota(char *mnt, struct if_quotactl *qctl)
1700 /* XXX: this is a flag to mark that only mds quota is wanted */
1701 qctl->qc_dqblk.dqb_valid = 1;
1702 rc = llapi_quotactl(mnt, qctl);
1704 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1708 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, MDS_QUOTA_INFO);
1712 static int print_lov_quota(char *mnt, struct if_quotactl *qctl)
1715 struct obd_uuid *uuids = NULL, *uuidp;
1716 int obdcount = 1024;
1717 int i, rc = 0, rc1 = 0;
1721 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1725 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1726 sizeof(struct obd_uuid));
1731 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1733 struct obd_uuid *uuids_temp;
1735 if (rc == -EOVERFLOW) {
1736 uuids_temp = realloc(uuids, obdcount *
1737 sizeof(struct obd_uuid));
1738 if (uuids_temp != NULL)
1739 goto retry_get_uuids;
1744 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1748 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1749 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1751 /* XXX clear this flag to get quota from osts */
1752 qctl->qc_dqblk.dqb_valid = 0;
1753 rc = llapi_quotactl(mnt, qctl);
1757 fprintf(stderr, "%s quotactl failed: %s\n",
1758 uuidp->uuid, strerror(errno));
1762 print_quota((char *)uuidp->uuid, qctl, OST_QUOTA_INFO);
1772 static int lfs_quota(int argc, char **argv)
1775 char *name = NULL, *mnt;
1776 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1778 char *obd_type = (char *)qctl.obd_type;
1779 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
1780 int rc, rc1 = 0, rc2 = 0, rc3 = 0;
1783 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1786 qctl.qc_type = 0x01;
1789 qctl.qc_type = 0x02;
1792 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1795 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1798 fprintf(stderr, "error: %s: option '-%c' "
1799 "unrecognized\n", argv[0], c);
1808 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1809 if (optind + 2 != argc) {
1810 fprintf(stderr, "error: missing quota argument(s)\n");
1814 name = argv[optind++];
1815 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1817 fprintf(stderr,"error: can't find id for name %s: %s\n",
1818 name, strerror(errno));
1821 print_quota_title(name, &qctl);
1822 } else if (optind + 1 != argc) {
1823 fprintf(stderr, "error: missing quota info argument(s)\n");
1829 rc1 = llapi_quotactl(mnt, &qctl);
1830 if (rc1 == -1 && errno == ESRCH) {
1831 fprintf(stderr, "\n%s quotas are not enabled.\n",
1832 qctl.qc_type == 0x00 ? "user" : "group");
1835 if (rc1 && *obd_type)
1836 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1839 rc = id2name(&name, getuid(), qctl.qc_type);
1846 print_quota(mnt, &qctl, GENERAL_QUOTA_INFO);
1848 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1849 rc2 = print_mds_quota(mnt, &qctl);
1850 rc3 = print_lov_quota(mnt, &qctl);
1853 if (rc1 || rc2 || rc3)
1854 printf("Some errors happened when getting quota info. "
1855 "Some devices may be not working or deactivated. "
1856 "The data in \"[]\" is inaccurate.\n");
1859 #endif /* HAVE_QUOTA_SUPPORT */
1861 int main(int argc, char **argv)
1867 ptl_initialize(argc, argv);
1868 if (obd_initialize(argc, argv) < 0)
1870 if (dbg_initialize(argc, argv) < 0)
1873 Parser_init("lfs > ", cmdlist);
1876 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1878 rc = Parser_commands();
1881 obd_finalize(argc, argv);