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.
35 #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);
70 static int lfs_join(int argc, char **argv);
72 /* all avaialable commands */
73 command_t cmdlist[] = {
74 {"setstripe", lfs_setstripe, 0,
75 "Create a new file with a specific striping pattern or\n"
76 "set the default striping pattern on an existing directory or\n"
77 "delete the default striping pattern from an existing directory\n"
78 "usage: setstripe <filename|dirname> <stripe size> <stripe start> <stripe count>\n"
80 " setstripe -d <dirname> (to delete default striping)\n"
81 "\tstripe size: Number of bytes on each OST (0 filesystem default)\n"
82 "\tstripe start: OST index of first stripe (-1 filesystem default)\n"
83 "\tstripe count: Number of OSTs to stripe over (0 default, -1 all)"},
84 {"getstripe", lfs_getstripe, 0,
85 "To list the striping info for a given filename or files in a\n"
86 "directory or recursively for all files in a directory tree.\n"
87 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
88 " [--recursive | -r] <dir|file> ..."},
90 "To find files that match given parameters recursively in a directory tree.\n"
91 "usage: find [--atime|-A N] [--mtime|-M N] [--ctime|-C N] [--maxdepth|-D N]\n"
92 " [--print0|-P] [--print|-p] [--obd|-O <uuid>] <dir/file> ...\n"
93 "If one of the options below is provided, find works the same as 'getstripe':\n"
94 "To list the striping info for a given filename or files in a directory or\n"
96 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
97 " [--recursive | -r] <dir|file> ..."},
98 {"check", lfs_check, 0,
99 "Display the status of MDS or OSTs (as specified in the command)\n"
100 "or all the servers (MDS and OSTs).\n"
101 "usage: check <osts|mds|servers>"},
102 {"catinfo", lfs_catinfo, 0,
103 "Show information of specified type logs.\n"
104 "usage: catinfo {keyword} [node name]\n"
105 "\tkeywords are one of followings: config, deletions.\n"
106 "\tnode name must be provided when use keyword config."},
107 {"join", lfs_join, 0,
108 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
109 "usage: join <filename_A> <filename_B>\n"},
110 {"osts", lfs_osts, 0, "osts"},
112 "report filesystem disk space usage or inodes usage"
114 "Usage: df [-i] [-h] [path]"},
115 #ifdef HAVE_QUOTA_SUPPORT
116 {"quotachown",lfs_quotachown, 0,
117 "Change files' owner or group on the specified filesystem.\n"
118 "usage: quotachown [-i] <filesystem>\n"
119 "\t-i: ignore error if file is not exist\n"},
120 {"quotacheck", lfs_quotacheck, 0,
121 "Scan the specified filesystem for disk usage, and create,\n"
122 "or update quota files.\n"
123 "usage: quotacheck [ -ug ] <filesystem>"},
124 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
125 "usage: quotaon [ -ugf ] <filesystem>"},
126 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
127 "usage: quotaoff [ -ug ] <filesystem>"},
128 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
129 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
130 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
131 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
132 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
134 {"help", Parser_help, 0, "help"},
135 {"exit", Parser_quit, 0, "quit"},
136 {"quit", Parser_quit, 0, "quit"},
141 static int lfs_setstripe(int argc, char **argv)
146 int st_offset, st_count;
149 if (argc != 5 && argc != 3)
154 if (strcmp(argv[1], "-d") != 0)
164 /* get the stripe size */
165 st_size = strtoul(argv[2], &end, 0);
167 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
172 /* get the stripe offset */
173 st_offset = strtoul(argv[3], &end, 0);
175 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
179 /* get the stripe count */
180 st_count = strtoul(argv[4], &end, 0);
182 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
188 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
190 fprintf(stderr, "error: %s: create stripe file failed\n",
196 static int set_time(time_t *time, time_t *set, char *str)
203 else if (str[0] == '-')
209 t = strtol(str, NULL, 0);
210 if (*time < t * 24 * 60 * 60) {
213 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
217 *set = *time - t * 24 * 60 * 60;
221 static int lfs_find(int argc, char **argv)
228 time_t atime, ctime, mtime;
229 int asign, msign, csign;
230 int quiet, verbose, recursive;
231 struct find_param param;
232 struct obd_uuid *obduuid = NULL;
233 struct option long_opts[] = {
234 /* New find options. */
235 {"atime", required_argument, 0, 'A'},
236 {"ctime", required_argument, 0, 'C'},
237 {"mtime", required_argument, 0, 'M'},
238 {"maxdepth", required_argument, 0, 'D'},
239 /* --obd is considered as a new option. */
240 {"obd", required_argument, 0, 'O'},
241 {"print", no_argument, 0, 'P'},
242 {"print0", no_argument, 0, 'p'},
243 /* Old find options. */
244 {"quiet", no_argument, 0, 'q'},
245 {"recursive", no_argument, 0, 'r'},
246 {"verbose", no_argument, 0, 'v'},
253 atime = ctime = mtime = 0;
254 asign = csign = msign = 0;
255 quiet = verbose = recursive = 0;
257 while ((c = getopt_long_only(argc, argv, "A:C:D:M:PpOqrv",
258 long_opts, NULL)) >= 0)
265 ret = set_time(&t, &atime, optarg);
272 ret = set_time(&t, &ctime, optarg);
279 depth = strtol(optarg, 0, 0);
282 ret = set_time(&t, &mtime, optarg);
291 "error: %s: only one obduuid allowed",
295 obduuid = (struct obd_uuid *)optarg;
319 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
320 argv[0], argv[optind - 1]);
328 memset(¶m, 0, sizeof(param));
329 param.obduuid = obduuid;
331 param.maxdepth = depth;
338 param.zeroend = zeroend;
341 param.recursive = recursive;
342 param.verbose = verbose;
344 param.maxdepth = recursive ? -1 : 1;
349 ret = llapi_find(argv[optind], ¶m);
351 ret = llapi_getstripe(argv[optind], ¶m);
352 } while (++optind < argc && !ret);
355 fprintf(stderr, "error: %s failed for %s.\n",
356 argv[0], argv[optind - 1]);
360 static int lfs_getstripe(int argc, char **argv)
362 struct option long_opts[] = {
364 {"quiet", 0, 0, 'q'},
365 {"recursive", 0, 0, 'r'},
366 {"verbose", 0, 0, 'v'},
369 char short_opts[] = "hO:qrv";
370 int quiet, verbose, recursive, c, rc;
371 struct obd_uuid *obduuid = NULL;
372 struct find_param param;
375 quiet = verbose = recursive = 0;
376 while ((c = getopt_long(argc, argv, short_opts,
377 long_opts, NULL)) != -1) {
382 "error: %s: only one obduuid allowed",
386 obduuid = (struct obd_uuid *)optarg;
402 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
403 argv[0], argv[optind - 1]);
411 memset(¶m, 0, sizeof(param));
412 param.recursive = recursive;
413 param.verbose = verbose;
415 param.obduuid = obduuid;
416 param.maxdepth = recursive ? -1 : 1;
419 rc = llapi_getstripe(argv[optind], ¶m);
420 } while (++optind < argc && !rc);
423 fprintf(stderr, "error: %s failed for %s.\n",
424 argv[0], argv[optind - 1]);
428 static int lfs_osts(int argc, char **argv)
431 struct mntent *mnt = NULL;
432 struct obd_uuid *obduuid = NULL;
433 struct find_param param;
439 fp = setmntent(MOUNTED, "r");
442 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
446 memset(¶m, 0, sizeof(param));
447 param.obduuid = obduuid;
448 while (feof(fp) == 0 && ferror(fp) ==0) {
449 if (llapi_is_lustre_mnttype(mnt)) {
450 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
453 "error: %s: failed on %s\n",
454 argv[0], mnt->mnt_dir);
464 #define COOK(value) \
467 while (value > 1024) { \
481 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
483 char rpath[PATH_MAX] = {'\0'};
485 int rc, len, out_len = 0;
487 if (!realpath(path, rpath)) {
489 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
490 path, strerror(-rc));
496 while (feof(fp) == 0 && ferror(fp) == 0) {
497 if (llapi_is_lustre_mnttype(mnt)) {
498 len = strlen(mnt->mnt_dir);
500 !strncmp(rpath, mnt->mnt_dir, len)) {
502 memset(mntdir, 0, dir_len);
503 strncpy(mntdir, mnt->mnt_dir, dir_len);
512 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
516 static int showdf(char *mntdir, struct obd_statfs *stat,
517 char *uuid, int ishow, int cooked,
518 char *type, int index, int rc)
520 long long avail, used, total;
522 char *suffix = "KMGTPEZY";
523 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
531 avail = stat->os_ffree;
532 used = stat->os_files - stat->os_ffree;
533 total = stat->os_files;
535 int shift = cooked ? 0 : 10;
537 avail = (stat->os_bavail * stat->os_bsize) >> shift;
538 used = stat->os_blocks - stat->os_bavail;
539 used = (used * stat->os_bsize) >> shift;
540 total = (stat->os_blocks * stat->os_bsize) >> shift;
544 ratio = (double)used / (double)total;
550 cook_val = (double)total;
553 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
555 sprintf(tbuf, CDF, total);
557 cook_val = (double)used;
560 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
562 sprintf(ubuf, CDF, used);
564 cook_val = (double)avail;
567 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
569 sprintf(abuf, CDF, avail);
571 sprintf(tbuf, CDF, total);
572 sprintf(ubuf, CDF, used);
573 sprintf(abuf, CDF, avail);
576 sprintf(rbuf, RDF, (int)(ratio * 100));
577 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
578 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
580 printf("[%s:%d]\n", type, index);
586 printf(UUF": inactive device\n", uuid);
589 printf(UUF": %s\n", uuid, strerror(-rc));
596 static int mntdf(char *mntdir, int ishow, int cooked)
598 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
599 struct obd_uuid uuid_buf;
604 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
605 "UUID", "Inodes", "IUsed", "IFree",
606 "IUse%", "Mounted on");
608 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
609 "UUID", cooked ? "bytes" : "1K-blocks",
610 "Used", "Available", "Use%", "Mounted on");
612 for (index = 0; ; index++) {
613 memset(&stat_buf, 0, sizeof(struct obd_statfs));
614 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
615 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
616 &stat_buf, &uuid_buf);
620 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
621 rc == -ENODATA || rc == 0) {
622 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
626 "error: llapi_obd_statfs(%s): %s (%d)\n",
627 uuid_buf.uuid, strerror(-rc), rc);
631 sum.os_ffree += stat_buf.os_ffree;
632 sum.os_files += stat_buf.os_files;
636 for (index = 0; ; index++) {
637 memset(&stat_buf, 0, sizeof(struct obd_statfs));
638 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
639 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
640 &stat_buf, &uuid_buf);
644 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
645 rc == -ENODATA || rc == 0) {
646 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
650 "error: llapi_obd_statfs failed: %s (%d)\n",
655 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
656 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
657 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
662 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
667 static int lfs_df(int argc, char **argv)
671 struct mntent *mnt = NULL;
672 char mntdir[PATH_MAX] = {'\0'};
673 int ishow = 0, cooked = 0;
677 while ((c = getopt(argc, argv, "ih")) != -1) {
692 fp = setmntent(MOUNTED, "r");
695 fprintf(stderr, "error: %s: open %s failed( %s )\n",
696 argv[0], MOUNTED, strerror(errno));
700 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
706 rc = mntdf(mntdir, ishow, cooked);
711 while (feof(fp) == 0 && ferror(fp) == 0) {
712 if (llapi_is_lustre_mnttype(mnt)) {
713 rc = mntdf(mnt->mnt_dir, ishow, cooked);
726 static int lfs_check(int argc, char **argv)
730 struct mntent *mnt = NULL;
739 obd_types[0] = obd_type1;
740 obd_types[1] = obd_type2;
742 if (strcmp(argv[1], "osts") == 0) {
743 strcpy(obd_types[0], "osc");
744 } else if (strcmp(argv[1], "mds") == 0) {
745 strcpy(obd_types[0], "mdc");
746 } else if (strcmp(argv[1], "servers") == 0) {
748 strcpy(obd_types[0], "osc");
749 strcpy(obd_types[1], "mdc");
751 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
756 fp = setmntent(MOUNTED, "r");
758 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
762 while (feof(fp) == 0 && ferror(fp) ==0) {
763 if (llapi_is_lustre_mnttype(mnt))
771 fprintf(stderr, "No suitable Lustre mount found\n");
775 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
778 fprintf(stderr, "error: %s: %s status failed\n",
785 static int lfs_catinfo(int argc, char **argv)
788 struct mntent *mnt = NULL;
791 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
794 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
797 fp = setmntent(MOUNTED, "r");
799 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
803 while (feof(fp) == 0 && ferror(fp) == 0) {
804 if (llapi_is_lustre_mnttype(mnt))
813 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
815 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
817 fprintf(stderr, "no lustre_lite mounted.\n");
824 int lfs_join(int argc, char **argv)
826 char *name_head, *name_tail;
833 fd = open(name_head, O_WRONLY);
835 fprintf(stderr, "Can not open name_head %s rc=%d\n",
839 size = lseek(fd, 0, SEEK_END);
840 if (size % JOIN_FILE_ALIGN) {
841 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
842 name_head, (long long)size, JOIN_FILE_ALIGN);
847 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
851 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
857 #ifdef HAVE_QUOTA_SUPPORT
858 static int lfs_quotachown(int argc, char **argv)
864 while ((c = getopt(argc, argv, "i")) != -1) {
870 fprintf(stderr, "error: %s: option '-%c' "
871 "unrecognized\n", argv[0], c);
877 rc = llapi_quotachown(argv[optind], flag);
879 fprintf(stderr,"error: change file owner/group failed.\n");
884 static int lfs_quotacheck(int argc, char **argv)
886 int c, check_type = 0;
888 struct if_quotacheck qchk;
889 struct if_quotactl qctl;
890 char *obd_type = qchk.obd_type;
891 char *obd_uuid = qchk.obd_uuid.uuid;
894 memset(&qchk, 0, sizeof(qchk));
897 while ((c = getopt(argc, argv, "ug")) != -1) {
906 fprintf(stderr, "error: %s: option '-%c' "
907 "unrecognized\n", argv[0], c);
914 else /* do quotacheck for both user & group quota by default */
922 memset(&qctl, 0, sizeof(qctl));
923 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
924 qctl.qc_id = QFMT_LDISKFS;
925 qctl.qc_type = check_type;
926 rc = llapi_quotactl(mnt, &qctl);
928 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
932 rc = llapi_quotacheck(mnt, check_type);
934 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
938 rc = llapi_poll_quotacheck(mnt, &qchk);
941 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
942 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
946 memset(&qctl, 0, sizeof(qctl));
947 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
948 qctl.qc_id = QFMT_LDISKFS;
949 qctl.qc_type = check_type;
950 rc = llapi_quotactl(mnt, &qctl);
953 fprintf(stderr, "%s %s ",
954 qctl.obd_type, qctl.obd_uuid.uuid);
955 fprintf(stderr, "%s turn on quota failed: %s\n",
956 argv[0], strerror(errno));
963 static int lfs_quotaon(int argc, char **argv)
967 struct if_quotactl qctl;
968 char *obd_type = qctl.obd_type;
969 char *obd_uuid = qctl.obd_uuid.uuid;
972 memset(&qctl, 0, sizeof(qctl));
973 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
974 qctl.qc_id = QFMT_LDISKFS;
977 while ((c = getopt(argc, argv, "ugf")) != -1) {
980 qctl.qc_type |= 0x01;
983 qctl.qc_type |= 0x02;
986 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
989 fprintf(stderr, "error: %s: option '-%c' "
990 "unrecognized\n", argv[0], c);
1003 rc = llapi_quotactl(mnt, &qctl);
1006 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1007 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1014 static int lfs_quotaoff(int argc, char **argv)
1018 struct if_quotactl qctl;
1019 char *obd_type = qctl.obd_type;
1020 char *obd_uuid = qctl.obd_uuid.uuid;
1023 memset(&qctl, 0, sizeof(qctl));
1024 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1027 while ((c = getopt(argc, argv, "ug")) != -1) {
1030 qctl.qc_type |= 0x01;
1033 qctl.qc_type |= 0x02;
1036 fprintf(stderr, "error: %s: option '-%c' "
1037 "unrecognized\n", argv[0], c);
1050 rc = llapi_quotactl(mnt, &qctl);
1053 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1054 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1061 static int name2id(unsigned int *id, char *name, int type)
1063 if (type == USRQUOTA) {
1064 struct passwd *entry;
1066 if (!(entry = getpwnam(name))) {
1072 *id = entry->pw_uid;
1074 struct group *entry;
1076 if (!(entry = getgrnam(name))) {
1082 *id = entry->gr_gid;
1088 static int id2name(char **name, unsigned int id, int type)
1090 if (type == USRQUOTA) {
1091 struct passwd *entry;
1093 if (!(entry = getpwuid(id))) {
1099 *name = entry->pw_name;
1101 struct group *entry;
1103 if (!(entry = getgrgid(id))) {
1109 *name = entry->gr_name;
1115 #define ARG2INT(nr, str, msg) \
1118 nr = strtol(str, &endp, 0); \
1120 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1125 int lfs_setquota(int argc, char **argv)
1129 struct if_quotactl qctl;
1130 char *obd_type = qctl.obd_type;
1131 char *obd_uuid = qctl.obd_uuid.uuid;
1134 memset(&qctl, 0, sizeof(qctl));
1135 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1138 while ((c = getopt(argc, argv, "ugt")) != -1) {
1141 qctl.qc_type |= 0x01;
1144 qctl.qc_type |= 0x02;
1147 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1150 fprintf(stderr, "error: %s: option '-%c' "
1151 "unrecognized\n", argv[0], c);
1159 if (qctl.qc_type == UGQUOTA) {
1160 fprintf(stderr, "error: user and group quotas can't be set "
1165 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1166 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1168 if (optind + 6 != argc)
1171 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1173 fprintf(stderr, "error: find id for name %s failed: %s\n",
1174 argv[optind - 1], strerror(errno));
1178 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1179 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1180 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1181 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1183 dqb->dqb_valid = QIF_LIMITS;
1185 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1187 if (optind + 3 != argc)
1190 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1191 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1196 rc = llapi_quotactl(mnt, &qctl);
1199 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1200 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1207 static inline char *type2name(int check_type)
1209 if (check_type == USRQUOTA)
1211 else if (check_type == GRPQUOTA)
1218 static void grace2str(time_t seconds,char *buf)
1220 uint minutes, hours, days;
1222 minutes = (seconds + 30) / 60;
1223 hours = minutes / 60;
1228 snprintf(buf, 40, "%ddays", days);
1230 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1234 static void diff2str(time_t seconds, char *buf, time_t now)
1240 if (seconds <= now) {
1241 strcpy(buf, "none");
1244 grace2str(seconds - now, buf);
1247 static void print_quota_title(char *name, struct if_quotactl *qctl)
1249 printf("Disk quotas for %s %s (%cid %u):\n",
1250 type2name(qctl->qc_type), name,
1251 *type2name(qctl->qc_type), qctl->qc_id);
1252 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1254 "blocks", "quota", "limit", "grace",
1255 "files", "quota", "limit", "grace");
1258 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1264 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1265 int bover = 0, iover = 0;
1266 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1268 if (dqb->dqb_bhardlimit &&
1269 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1271 } else if (dqb->dqb_bsoftlimit &&
1272 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1273 if (dqb->dqb_btime > now) {
1280 if (dqb->dqb_ihardlimit &&
1281 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1283 } else if (dqb->dqb_isoftlimit &&
1284 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1285 if (dqb->dqb_btime > now) {
1292 #if 0 /* XXX: always print quotas even when no usages */
1293 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1299 if (strlen(mnt) > 15)
1300 printf("%s\n%15s", mnt, "");
1302 printf("%15s", mnt);
1305 diff2str(dqb->dqb_btime, timebuf, now);
1307 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1308 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1309 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1310 printf(" %7s%c %6s %7s %7s",
1311 numbuf[0], bover ? '*' : ' ', numbuf[1],
1312 numbuf[2], bover > 1 ? timebuf : "");
1315 diff2str(dqb->dqb_itime, timebuf, now);
1317 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1318 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1319 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1321 printf(" %7s%c %6s %7s %7s",
1322 numbuf[0], iover ? '*' : ' ', numbuf[1],
1323 numbuf[2], iover > 1 ? timebuf : "");
1326 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1327 qctl->qc_cmd == Q_GETOINFO) {
1331 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1332 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1333 printf("Block grace time: %s; Inode grace time: %s\n",
1334 bgtimebuf, igtimebuf);
1338 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1342 /* XXX: this is a flag to mark that only mds quota is wanted */
1343 qctl->qc_dqblk.dqb_valid = 1;
1344 rc = llapi_quotactl(mnt, qctl);
1346 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1349 qctl->qc_dqblk.dqb_valid = 0;
1351 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1354 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1357 struct obd_uuid uuids[1024], *uuidp;
1358 int obdcount = 1024;
1363 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1367 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1369 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1373 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1374 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1376 /* XXX clear this flag to get quota from osts */
1377 qctl->qc_dqblk.dqb_valid = 0;
1378 rc = llapi_quotactl(mnt, qctl);
1380 fprintf(stderr, "%s quotactl failed: %s\n",
1381 uuidp->uuid, strerror(errno));
1385 print_quota(uuidp->uuid, qctl, 1);
1393 static int lfs_quota(int argc, char **argv)
1396 char *name = NULL, *mnt;
1397 struct if_quotactl qctl;
1398 char *obd_type = qctl.obd_type;
1399 char *obd_uuid = qctl.obd_uuid.uuid;
1402 memset(&qctl, 0, sizeof(qctl));
1403 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1406 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1409 qctl.qc_type |= 0x01;
1412 qctl.qc_type |= 0x02;
1415 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1418 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1421 fprintf(stderr, "error: %s: option '-%c' "
1422 "unrecognized\n", argv[0], c);
1430 if (qctl.qc_type == UGQUOTA) {
1431 fprintf(stderr, "error: user or group can't be specified"
1436 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1437 if (optind + 2 != argc)
1440 name = argv[optind++];
1441 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1443 fprintf(stderr, "error: find id for name %s failed: %s\n",
1444 name, strerror(errno));
1447 print_quota_title(name, &qctl);
1448 } else if (optind + 1 != argc) {
1454 rc = llapi_quotactl(mnt, &qctl);
1457 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1458 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1463 rc = id2name(&name, getuid(), qctl.qc_type);
1470 print_quota(mnt, &qctl, 0);
1472 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1473 print_mds_quota(mnt, &qctl);
1474 print_lov_quota(mnt, &qctl);
1479 #endif /* HAVE_QUOTA_SUPPORT */
1481 int main(int argc, char **argv)
1487 ptl_initialize(argc, argv);
1488 if (obd_initialize(argc, argv) < 0)
1490 if (dbg_initialize(argc, argv) < 0)
1493 Parser_init("lfs > ", cmdlist);
1496 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1498 rc = Parser_commands();
1501 obd_finalize(argc, argv);