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>
40 #include <lnet/api-support.h>
41 #include <lnet/lnetctl.h>
43 #include <liblustre.h>
44 #include <lustre/lustre_idl.h>
45 #include <lustre/liblustreapi.h>
46 #include <lustre/lustre_user.h>
51 unsigned int libcfs_subsystem_debug = 0;
54 static int lfs_setstripe(int argc, char **argv);
55 static int lfs_find(int argc, char **argv);
56 static int lfs_getstripe(int argc, char **argv);
57 static int lfs_osts(int argc, char **argv);
58 static int lfs_df(int argc, char **argv);
59 static int lfs_check(int argc, char **argv);
60 static int lfs_catinfo(int argc, char **argv);
61 #ifdef HAVE_QUOTA_SUPPORT
62 static int lfs_quotachown(int argc, char **argv);
63 static int lfs_quotacheck(int argc, char **argv);
64 static int lfs_quotaon(int argc, char **argv);
65 static int lfs_quotaoff(int argc, char **argv);
66 static int lfs_setquota(int argc, char **argv);
67 static int lfs_quota(int argc, char **argv);
69 static int lfs_join(int argc, char **argv);
71 /* all avaialable commands */
72 command_t cmdlist[] = {
73 {"setstripe", lfs_setstripe, 0,
74 "Create a new file with a specific striping pattern or\n"
75 "set the default striping pattern on an existing directory or\n"
76 "delete the default striping pattern from an existing directory\n"
77 "usage: setstripe <filename|dirname> <stripe size> <stripe start> <stripe count>\n"
79 " setstripe -d <dirname> (to delete default striping)\n"
80 "\tstripe size: Number of bytes on each OST (0 filesystem default)\n"
81 "\tstripe start: OST index of first stripe (-1 filesystem default)\n"
82 "\tstripe count: Number of OSTs to stripe over (0 default, -1 all)"},
84 "To list the extended attributes for a given filename or files in a\n"
85 "directory or recursively for all files in a directory tree.\n"
86 "usage: find [--obd <uuid>] [--quiet | --verbose] [--recursive] <dir|file> ..."},
87 {"getstripe", lfs_getstripe, 0,
88 "To list the striping pattern for given filename.\n"
89 "usage:getstripe <filename>"},
90 {"check", lfs_check, 0,
91 "Display the status of MDS or OSTs (as specified in the command)\n"
92 "or all the servers (MDS and OSTs).\n"
93 "usage: check <osts|mds|servers>"},
94 {"catinfo", lfs_catinfo, 0,
95 "Show information of specified type logs.\n"
96 "usage: catinfo {keyword} [node name]\n"
97 "\tkeywords are one of followings: config, deletions.\n"
98 "\tnode name must be provided when use keyword config."},
100 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
101 "usage: join <filename_A> <filename_B>\n"},
102 {"osts", lfs_osts, 0, "osts"},
104 "report filesystem disk space usage or inodes usage"
106 "Usage: df [-i] [-h] [path]"},
107 #ifdef HAVE_QUOTA_SUPPORT
108 {"quotachown",lfs_quotachown, 0,
109 "Change files' owner or group on the specified filesystem.\n"
110 "usage: quotachown [-i] <filesystem>\n"
111 "\t-i: ignore error if file is not exist\n"},
112 {"quotacheck", lfs_quotacheck, 0,
113 "Scan the specified filesystem for disk usage, and create,\n"
114 "or update quota files.\n"
115 "usage: quotacheck [ -ug ] <filesystem>"},
116 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
117 "usage: quotaon [ -ugf ] <filesystem>"},
118 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
119 "usage: quotaoff [ -ug ] <filesystem>"},
120 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
121 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
122 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
123 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
124 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
126 {"help", Parser_help, 0, "help"},
127 {"exit", Parser_quit, 0, "quit"},
128 {"quit", Parser_quit, 0, "quit"},
133 static int lfs_setstripe(int argc, char **argv)
138 int st_offset, st_count;
141 if (argc != 5 && argc != 3)
146 if (strcmp(argv[1], "-d") != 0)
156 /* get the stripe size */
157 st_size = strtoul(argv[2], &end, 0);
159 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
164 /* get the stripe offset */
165 st_offset = strtoul(argv[3], &end, 0);
167 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
171 /* get the stripe count */
172 st_count = strtoul(argv[4], &end, 0);
174 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
180 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
182 fprintf(stderr, "error: %s: create stripe file failed\n",
188 static int lfs_find(int argc, char **argv)
190 struct option long_opts[] = {
192 {"quiet", 0, 0, 'q'},
193 {"recursive", 0, 0, 'r'},
194 {"verbose", 0, 0, 'v'},
197 char short_opts[] = "ho:qrv";
198 int quiet, verbose, recursive, c, rc;
199 struct obd_uuid *obduuid = NULL;
202 quiet = verbose = recursive = 0;
203 while ((c = getopt_long(argc, argv, short_opts,
204 long_opts, NULL)) != -1) {
209 "error: %s: only one obduuid allowed",
213 obduuid = (struct obd_uuid *)optarg;
230 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
231 argv[0], argv[optind - 1]);
241 rc = llapi_find(argv[optind], obduuid, recursive,verbose,quiet);
242 } while (++optind < argc && !rc);
245 fprintf(stderr, "error: %s: find failed\n", argv[0]);
249 static int lfs_getstripe(int argc, char **argv)
251 struct option long_opts[] = {
252 {"quiet", 0, 0, 'q'},
253 {"verbose", 0, 0, 'v'},
256 char short_opts[] = "qv";
257 int quiet, verbose, recursive, c, rc;
258 struct obd_uuid *obduuid = NULL;
261 quiet = verbose = recursive = 0;
262 while ((c = getopt_long(argc, argv, short_opts,
263 long_opts, NULL)) != -1) {
268 "error: %s: only one obduuid allowed",
272 obduuid = (struct obd_uuid *)optarg;
286 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
287 argv[0], argv[optind - 1]);
297 rc = llapi_find(argv[optind], obduuid, recursive,verbose,quiet);
298 } while (++optind < argc && !rc);
301 fprintf(stderr, "error: %s: getstripe failed for %s\n",
307 static int lfs_osts(int argc, char **argv)
310 struct mntent *mnt = NULL;
311 struct obd_uuid *obduuid = NULL;
317 fp = setmntent(MOUNTED, "r");
320 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
324 while (feof(fp) == 0 && ferror(fp) ==0) {
325 if (llapi_is_lustre_mnttype(mnt)) {
326 rc = llapi_find(mnt->mnt_dir, obduuid, 0, 0, 0);
329 "error: %s: failed on %s\n",
330 argv[0], mnt->mnt_dir);
340 #define COOK(value) \
343 while (value > 1024) { \
357 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
359 char rpath[PATH_MAX] = {'\0'};
361 int rc, len, out_len = 0;
363 if (!realpath(path, rpath)) {
365 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
366 path, strerror(-rc));
372 while (feof(fp) == 0 && ferror(fp) == 0) {
373 if (llapi_is_lustre_mnttype(mnt)) {
374 len = strlen(mnt->mnt_dir);
376 !strncmp(rpath, mnt->mnt_dir, len)) {
378 memset(mntdir, 0, dir_len);
379 strncpy(mntdir, mnt->mnt_dir, dir_len);
388 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
392 static int showdf(char *mntdir, struct obd_statfs *stat,
393 char *uuid, int ishow, int cooked,
394 char *type, int index, int rc)
396 __u64 avail, used, total;
398 char *suffix = "KMGTPEZY";
399 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
407 avail = stat->os_ffree;
408 used = stat->os_files - stat->os_ffree;
409 total = stat->os_files;
411 int shift = cooked ? 0 : 10;
413 avail = (stat->os_bavail * stat->os_bsize) >> shift;
414 used = stat->os_blocks - stat->os_bavail;
415 used = (used * stat->os_bsize) >> shift;
416 total = (stat->os_blocks * stat->os_bsize) >> shift;
420 ratio = (double)used / (double)total;
426 cook_val = (double)total;
429 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
431 sprintf(tbuf, CDF, total);
433 cook_val = (double)used;
436 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
438 sprintf(ubuf, CDF, used);
440 cook_val = (double)avail;
443 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
445 sprintf(abuf, CDF, avail);
447 sprintf(tbuf, CDF, total);
448 sprintf(ubuf, CDF, used);
449 sprintf(abuf, CDF, avail);
452 sprintf(rbuf, RDF, (int)(ratio * 100));
453 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
454 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
456 printf("[%s:%d]\n", type, index);
462 printf(UUF": inactive device\n", uuid);
465 printf(UUF": %s\n", uuid, strerror(-rc));
472 static int mntdf(char *mntdir, int ishow, int cooked)
474 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
475 struct obd_uuid uuid_buf;
480 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
481 "UUID", "Inodes", "IUsed", "IFree",
482 "IUse%", "Mounted on");
484 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
485 "UUID", cooked ? "bytes" : "1K-blocks",
486 "Used", "Available", "Use%", "Mounted on");
488 for (index = 0; ; index++) {
489 memset(&stat_buf, 0, sizeof(struct obd_statfs));
490 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
491 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
492 &stat_buf, &uuid_buf);
496 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
497 rc == -ENODATA || rc == 0) {
498 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
502 "error: llapi_obd_statfs(%s): %s (%d)\n",
503 uuid_buf.uuid, strerror(-rc), rc);
507 sum.os_ffree += stat_buf.os_ffree;
508 sum.os_files += stat_buf.os_files;
512 for (index = 0; ; index++) {
513 memset(&stat_buf, 0, sizeof(struct obd_statfs));
514 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
515 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
516 &stat_buf, &uuid_buf);
520 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
521 rc == -ENODATA || rc == 0) {
522 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
526 "error: llapi_obd_statfs failed: %s (%d)\n",
531 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
532 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
533 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
538 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
543 static int lfs_df(int argc, char **argv)
547 struct mntent *mnt = NULL;
548 char mntdir[PATH_MAX] = {'\0'};
549 int ishow = 0, cooked = 0;
553 while ((c = getopt(argc, argv, "ih")) != -1) {
568 fp = setmntent(MOUNTED, "r");
571 fprintf(stderr, "error: %s: open %s failed( %s )\n",
572 argv[0], MOUNTED, strerror(errno));
576 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
582 rc = mntdf(mntdir, ishow, cooked);
587 while (feof(fp) == 0 && ferror(fp) == 0) {
588 if (llapi_is_lustre_mnttype(mnt)) {
589 rc = mntdf(mnt->mnt_dir, ishow, cooked);
602 static int lfs_check(int argc, char **argv)
606 struct mntent *mnt = NULL;
615 obd_types[0] = obd_type1;
616 obd_types[1] = obd_type2;
618 if (strcmp(argv[1], "osts") == 0) {
619 strcpy(obd_types[0], "osc");
620 } else if (strcmp(argv[1], "mds") == 0) {
621 strcpy(obd_types[0], "mdc");
622 } else if (strcmp(argv[1], "servers") == 0) {
624 strcpy(obd_types[0], "osc");
625 strcpy(obd_types[1], "mdc");
627 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
632 fp = setmntent(MOUNTED, "r");
634 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
638 while (feof(fp) == 0 && ferror(fp) ==0) {
639 if (llapi_is_lustre_mnttype(mnt))
647 fprintf(stderr, "No suitable Lustre mount found\n");
651 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
654 fprintf(stderr, "error: %s: %s status failed\n",
661 static int lfs_catinfo(int argc, char **argv)
664 struct mntent *mnt = NULL;
667 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
670 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
673 fp = setmntent(MOUNTED, "r");
675 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
679 while (feof(fp) == 0 && ferror(fp) == 0) {
680 if (llapi_is_lustre_mnttype(mnt))
689 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
691 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
693 fprintf(stderr, "no lustre_lite mounted.\n");
700 int lfs_join(int argc, char **argv)
702 char *name_head, *name_tail;
709 fd = open(name_head, O_WRONLY);
711 fprintf(stderr, "Can not open name_head %s rc=%d\n",
715 size = lseek(fd, 0, SEEK_END);
716 if (size % JOIN_FILE_ALIGN) {
717 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
718 name_head, size, JOIN_FILE_ALIGN);
723 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
727 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
733 #ifdef HAVE_QUOTA_SUPPORT
734 static int lfs_quotachown(int argc, char **argv)
740 while ((c = getopt(argc, argv, "i")) != -1) {
746 fprintf(stderr, "error: %s: option '-%c' "
747 "unrecognized\n", argv[0], c);
753 rc = llapi_quotachown(argv[optind], flag);
755 fprintf(stderr,"error: change file owner/group failed.\n");
760 static int lfs_quotacheck(int argc, char **argv)
762 int c, check_type = 0;
764 struct if_quotacheck qchk;
765 struct if_quotactl qctl;
766 char *obd_type = qchk.obd_type;
767 char *obd_uuid = qchk.obd_uuid.uuid;
770 memset(&qchk, 0, sizeof(qchk));
773 while ((c = getopt(argc, argv, "ug")) != -1) {
782 fprintf(stderr, "error: %s: option '-%c' "
783 "unrecognized\n", argv[0], c);
790 else /* do quotacheck for both user & group quota by default */
798 memset(&qctl, 0, sizeof(qctl));
799 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
800 qctl.qc_id = QFMT_LDISKFS;
801 qctl.qc_type = check_type;
802 rc = llapi_quotactl(mnt, &qctl);
804 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
808 rc = llapi_quotacheck(mnt, check_type);
810 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
814 rc = llapi_poll_quotacheck(mnt, &qchk);
817 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
818 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
822 memset(&qctl, 0, sizeof(qctl));
823 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
824 qctl.qc_id = QFMT_LDISKFS;
825 qctl.qc_type = check_type;
826 rc = llapi_quotactl(mnt, &qctl);
829 fprintf(stderr, "%s %s ",
830 qctl.obd_type, qctl.obd_uuid.uuid);
831 fprintf(stderr, "%s turn on quota failed: %s\n",
832 argv[0], strerror(errno));
839 static int lfs_quotaon(int argc, char **argv)
843 struct if_quotactl qctl;
844 char *obd_type = qctl.obd_type;
845 char *obd_uuid = qctl.obd_uuid.uuid;
848 memset(&qctl, 0, sizeof(qctl));
849 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
850 qctl.qc_id = QFMT_LDISKFS;
853 while ((c = getopt(argc, argv, "ugf")) != -1) {
856 qctl.qc_type |= 0x01;
859 qctl.qc_type |= 0x02;
862 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
865 fprintf(stderr, "error: %s: option '-%c' "
866 "unrecognized\n", argv[0], c);
879 rc = llapi_quotactl(mnt, &qctl);
882 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
883 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
890 static int lfs_quotaoff(int argc, char **argv)
894 struct if_quotactl qctl;
895 char *obd_type = qctl.obd_type;
896 char *obd_uuid = qctl.obd_uuid.uuid;
899 memset(&qctl, 0, sizeof(qctl));
900 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
903 while ((c = getopt(argc, argv, "ug")) != -1) {
906 qctl.qc_type |= 0x01;
909 qctl.qc_type |= 0x02;
912 fprintf(stderr, "error: %s: option '-%c' "
913 "unrecognized\n", argv[0], c);
926 rc = llapi_quotactl(mnt, &qctl);
929 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
930 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
937 static int name2id(unsigned int *id, char *name, int type)
939 if (type == USRQUOTA) {
940 struct passwd *entry;
942 if (!(entry = getpwnam(name))) {
952 if (!(entry = getgrnam(name))) {
964 static int id2name(char **name, unsigned int id, int type)
966 if (type == USRQUOTA) {
967 struct passwd *entry;
969 if (!(entry = getpwuid(id))) {
975 *name = entry->pw_name;
979 if (!(entry = getgrgid(id))) {
985 *name = entry->gr_name;
991 #define ARG2INT(nr, str, msg) \
994 nr = strtol(str, &endp, 0); \
996 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1001 int lfs_setquota(int argc, char **argv)
1005 struct if_quotactl qctl;
1006 char *obd_type = qctl.obd_type;
1007 char *obd_uuid = qctl.obd_uuid.uuid;
1010 memset(&qctl, 0, sizeof(qctl));
1011 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1014 while ((c = getopt(argc, argv, "ugt")) != -1) {
1017 qctl.qc_type |= 0x01;
1020 qctl.qc_type |= 0x02;
1023 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1026 fprintf(stderr, "error: %s: option '-%c' "
1027 "unrecognized\n", argv[0], c);
1035 if (qctl.qc_type == UGQUOTA) {
1036 fprintf(stderr, "error: user and group quotas can't be set "
1041 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1042 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1044 if (optind + 6 != argc)
1047 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1049 fprintf(stderr, "error: find id for name %s failed: %s\n",
1050 argv[optind - 1], strerror(errno));
1054 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1055 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1056 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1057 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1059 dqb->dqb_valid = QIF_LIMITS;
1061 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1063 if (optind + 3 != argc)
1066 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1067 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1072 rc = llapi_quotactl(mnt, &qctl);
1075 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1076 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1083 static inline char *type2name(int check_type)
1085 if (check_type == USRQUOTA)
1087 else if (check_type == GRPQUOTA)
1094 static void grace2str(time_t seconds,char *buf)
1096 uint minutes, hours, days;
1098 minutes = (seconds + 30) / 60;
1099 hours = minutes / 60;
1104 snprintf(buf, 40, "%ddays", days);
1106 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1110 static void diff2str(time_t seconds, char *buf, time_t now)
1116 if (seconds <= now) {
1117 strcpy(buf, "none");
1120 grace2str(seconds - now, buf);
1123 static void print_quota_title(char *name, struct if_quotactl *qctl)
1125 printf("Disk quotas for %s %s (%cid %u):\n",
1126 type2name(qctl->qc_type), name,
1127 *type2name(qctl->qc_type), qctl->qc_id);
1128 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1130 "blocks", "quota", "limit", "grace",
1131 "files", "quota", "limit", "grace");
1134 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1140 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1141 int bover = 0, iover = 0;
1142 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1144 if (dqb->dqb_bhardlimit &&
1145 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1147 } else if (dqb->dqb_bsoftlimit &&
1148 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1149 if (dqb->dqb_btime > now) {
1156 if (dqb->dqb_ihardlimit &&
1157 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1159 } else if (dqb->dqb_isoftlimit &&
1160 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1161 if (dqb->dqb_btime > now) {
1168 #if 0 /* XXX: always print quotas even when no usages */
1169 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1175 if (strlen(mnt) > 15)
1176 printf("%s\n%15s", mnt, "");
1178 printf("%15s", mnt);
1181 diff2str(dqb->dqb_btime, timebuf, now);
1183 sprintf(numbuf[0], "%llu", toqb(dqb->dqb_curspace));
1184 sprintf(numbuf[1], "%llu", dqb->dqb_bsoftlimit);
1185 sprintf(numbuf[2], "%llu", dqb->dqb_bhardlimit);
1186 printf(" %7s%c %6s %7s %7s",
1187 numbuf[0], bover ? '*' : ' ', numbuf[1],
1188 numbuf[2], bover > 1 ? timebuf : "");
1191 diff2str(dqb->dqb_itime, timebuf, now);
1193 sprintf(numbuf[0], "%llu", dqb->dqb_curinodes);
1194 sprintf(numbuf[1], "%llu", dqb->dqb_isoftlimit);
1195 sprintf(numbuf[2], "%llu", dqb->dqb_ihardlimit);
1197 printf(" %7s%c %6s %7s %7s",
1198 numbuf[0], iover ? '*' : ' ', numbuf[1],
1199 numbuf[2], iover > 1 ? timebuf : "");
1202 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1203 qctl->qc_cmd == Q_GETOINFO) {
1207 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1208 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1209 printf("Block grace time: %s; Inode grace time: %s\n",
1210 bgtimebuf, igtimebuf);
1214 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1218 /* XXX: this is a flag to mark that only mds quota is wanted */
1219 qctl->qc_dqblk.dqb_valid = 1;
1220 rc = llapi_quotactl(mnt, qctl);
1222 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1225 qctl->qc_dqblk.dqb_valid = 0;
1227 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1230 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1233 struct obd_uuid uuids[1024], *uuidp;
1234 int obdcount = 1024;
1239 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1243 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1245 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1249 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1250 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1252 /* XXX clear this flag to get quota from osts */
1253 qctl->qc_dqblk.dqb_valid = 0;
1254 rc = llapi_quotactl(mnt, qctl);
1256 fprintf(stderr, "%s quotactl failed: %s\n",
1257 uuidp->uuid, strerror(errno));
1261 print_quota(uuidp->uuid, qctl, 1);
1269 static int lfs_quota(int argc, char **argv)
1272 char *name = NULL, *mnt;
1273 struct if_quotactl qctl;
1274 char *obd_type = qctl.obd_type;
1275 char *obd_uuid = qctl.obd_uuid.uuid;
1278 memset(&qctl, 0, sizeof(qctl));
1279 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1282 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1285 qctl.qc_type |= 0x01;
1288 qctl.qc_type |= 0x02;
1291 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1294 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1297 fprintf(stderr, "error: %s: option '-%c' "
1298 "unrecognized\n", argv[0], c);
1306 if (qctl.qc_type == UGQUOTA) {
1307 fprintf(stderr, "error: user or group can't be specified"
1312 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1313 if (optind + 2 != argc)
1316 name = argv[optind++];
1317 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1319 fprintf(stderr, "error: find id for name %s failed: %s\n",
1320 name, strerror(errno));
1323 print_quota_title(name, &qctl);
1324 } else if (optind + 1 != argc) {
1330 rc = llapi_quotactl(mnt, &qctl);
1333 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1334 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1339 rc = id2name(&name, getuid(), qctl.qc_type);
1346 print_quota(mnt, &qctl, 0);
1348 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1349 print_mds_quota(mnt, &qctl);
1350 print_lov_quota(mnt, &qctl);
1355 #endif /* HAVE_QUOTA_SUPPORT */
1357 int main(int argc, char **argv)
1363 ptl_initialize(argc, argv);
1364 if (obd_initialize(argc, argv) < 0)
1366 if (dbg_initialize(argc, argv) < 0)
1369 Parser_init("lfs > ", cmdlist);
1372 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1374 rc = Parser_commands();
1377 obd_finalize(argc, argv);