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 <dir/file> ... \n"
92 " [[!] --atime|-A N] [[!] --mtime|-M N] [[!] --ctime|-C N] [--maxdepth|-D N]\n"
93 " [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
94 "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
95 "\t !: used before --name means find exclude the regular expression pattern\n"
96 "If one of the options below is provided, find works the same as 'getstripe':\n"
97 "To list the striping info for a given filename or files in a directory or\n"
99 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
100 " [--recursive | -r] <dir|file> ..."},
101 {"check", lfs_check, 0,
102 "Display the status of MDS or OSTs (as specified in the command)\n"
103 "or all the servers (MDS and OSTs).\n"
104 "usage: check <osts|mds|servers>"},
105 {"catinfo", lfs_catinfo, 0,
106 "Show information of specified type logs.\n"
107 "usage: catinfo {keyword} [node name]\n"
108 "\tkeywords are one of followings: config, deletions.\n"
109 "\tnode name must be provided when use keyword config."},
110 {"join", lfs_join, 0,
111 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
112 "usage: join <filename_A> <filename_B>\n"},
113 {"osts", lfs_osts, 0, "osts"},
115 "report filesystem disk space usage or inodes usage"
117 "Usage: df [-i] [-h] [path]"},
118 #ifdef HAVE_QUOTA_SUPPORT
119 {"quotachown",lfs_quotachown, 0,
120 "Change files' owner or group on the specified filesystem.\n"
121 "usage: quotachown [-i] <filesystem>\n"
122 "\t-i: ignore error if file is not exist\n"},
123 {"quotacheck", lfs_quotacheck, 0,
124 "Scan the specified filesystem for disk usage, and create,\n"
125 "or update quota files.\n"
126 "usage: quotacheck [ -ug ] <filesystem>"},
127 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
128 "usage: quotaon [ -ugf ] <filesystem>"},
129 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
130 "usage: quotaoff [ -ug ] <filesystem>"},
131 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
132 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
133 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
134 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
135 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
137 {"help", Parser_help, 0, "help"},
138 {"exit", Parser_quit, 0, "quit"},
139 {"quit", Parser_quit, 0, "quit"},
144 static int lfs_setstripe(int argc, char **argv)
149 int st_offset, st_count;
152 if (argc != 5 && argc != 3)
157 if (strcmp(argv[1], "-d") != 0)
167 /* get the stripe size */
168 st_size = strtoul(argv[2], &end, 0);
170 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
175 /* get the stripe offset */
176 st_offset = strtoul(argv[3], &end, 0);
178 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
182 /* get the stripe count */
183 st_count = strtoul(argv[4], &end, 0);
185 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
191 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
193 fprintf(stderr, "error: %s: create stripe file failed\n",
199 static int set_time(time_t *time, time_t *set, char *str)
206 else if (str[0] == '-')
212 t = strtol(str, NULL, 0);
213 if (*time < t * 24 * 60 * 60) {
216 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
220 *set = *time - t * 24 * 60 * 60;
224 static int lfs_find(int argc, char **argv)
231 int quiet, verbose, recursive;
232 struct find_param param;
233 struct obd_uuid *obduuid = NULL;
235 struct option long_opts[] = {
236 /* New find options. */
237 {"atime", required_argument, 0, 'A'},
238 {"ctime", required_argument, 0, 'C'},
239 {"mtime", required_argument, 0, 'M'},
240 {"maxdepth", required_argument, 0, 'D'},
241 {"name", required_argument, 0, 'n'},
242 /* --obd is considered as a new option. */
243 {"obd", required_argument, 0, 'O'},
244 {"ost", required_argument, 0, 'O'},
245 {"print", no_argument, 0, 'P'},
246 {"print0", no_argument, 0, 'p'},
247 /* Old find options. */
248 {"quiet", no_argument, 0, 'q'},
249 {"recursive", no_argument, 0, 'r'},
250 {"verbose", no_argument, 0, 'v'},
263 quiet = verbose = recursive = 0;
265 memset(¶m, 0, sizeof(param));
267 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpOqrv",
268 long_opts, NULL)) >= 0)
274 /* '!' is part of option */
275 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
276 if (!isoption && pathend != -1) {
277 fprintf(stderr, "err: %s: filename|dirname must either "
278 "precede options or follow options\n",
282 if (!isoption && pathstart == -1)
283 pathstart = optind - 1;
284 if (isoption && pathstart != -1 && pathend == -1) {
285 pathend = optind - 2;
286 if ((c == 1 && strcmp(optarg, "!") == 0) ||
287 c == 'P' || c == 'p' || c == 'O' ||
288 c == 'q' || c == 'r' || c == 'v')
289 pathend = optind - 1;
296 if (strcmp(optarg, "!") == 0)
300 xtime = ¶m.atime;
301 xsign = ¶m.asign;
304 xtime = ¶m.ctime;
305 xsign = ¶m.csign;
309 xtime = ¶m.mtime;
310 xsign = ¶m.msign;
314 if (optarg[0] == '-')
316 else if (optarg[0] == '+')
321 strcat(timestr, optarg);
325 ret = set_time(&t, xtime, optarg);
332 depth = strtol(optarg, 0, 0);
336 param.pattern = (char *)optarg;
338 param.exclude_pattern = 1;
340 param.exclude_pattern = 0;
345 "error: %s: only one obduuid allowed",
349 obduuid = (struct obd_uuid *)optarg;
373 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
374 argv[0], argv[optind - 1]);
379 if (pathstart == -1) {
380 fprintf(stderr, "error: %s: no filename|pathname\n",
383 } else if (pathend == -1) {
388 param.obduuid = obduuid;
390 param.maxdepth = depth;
391 param.zeroend = zeroend;
394 param.recursive = recursive;
395 param.verbose = verbose;
397 param.maxdepth = recursive ? -1 : 1;
402 ret = llapi_find(argv[pathstart], ¶m);
404 ret = llapi_getstripe(argv[pathstart], ¶m);
405 } while (++pathstart < pathend && !ret);
408 fprintf(stderr, "error: %s failed for %s.\n",
409 argv[0], argv[optind - 1]);
414 static int lfs_getstripe(int argc, char **argv)
416 struct option long_opts[] = {
418 {"quiet", 0, 0, 'q'},
419 {"recursive", 0, 0, 'r'},
420 {"verbose", 0, 0, 'v'},
423 char short_opts[] = "hO:qrv";
424 int quiet, verbose, recursive, c, rc;
425 struct obd_uuid *obduuid = NULL;
426 struct find_param param;
429 quiet = verbose = recursive = 0;
430 while ((c = getopt_long(argc, argv, short_opts,
431 long_opts, NULL)) != -1) {
436 "error: %s: only one obduuid allowed",
440 obduuid = (struct obd_uuid *)optarg;
456 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
457 argv[0], argv[optind - 1]);
465 memset(¶m, 0, sizeof(param));
466 param.recursive = recursive;
467 param.verbose = verbose;
469 param.obduuid = obduuid;
470 param.maxdepth = recursive ? -1 : 1;
473 rc = llapi_getstripe(argv[optind], ¶m);
474 } while (++optind < argc && !rc);
477 fprintf(stderr, "error: %s failed for %s.\n",
478 argv[0], argv[optind - 1]);
482 static int lfs_osts(int argc, char **argv)
485 struct mntent *mnt = NULL;
486 struct obd_uuid *obduuid = NULL;
487 struct find_param param;
493 fp = setmntent(MOUNTED, "r");
496 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
500 memset(¶m, 0, sizeof(param));
501 param.obduuid = obduuid;
502 while (feof(fp) == 0 && ferror(fp) ==0) {
503 if (llapi_is_lustre_mnttype(mnt)) {
504 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
507 "error: %s: failed on %s\n",
508 argv[0], mnt->mnt_dir);
518 #define COOK(value) \
521 while (value > 1024) { \
535 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
537 char rpath[PATH_MAX] = {'\0'};
539 int rc, len, out_len = 0;
541 if (!realpath(path, rpath)) {
543 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
544 path, strerror(-rc));
550 while (feof(fp) == 0 && ferror(fp) == 0) {
551 if (llapi_is_lustre_mnttype(mnt)) {
552 len = strlen(mnt->mnt_dir);
554 !strncmp(rpath, mnt->mnt_dir, len)) {
556 memset(mntdir, 0, dir_len);
557 strncpy(mntdir, mnt->mnt_dir, dir_len);
566 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
570 static int showdf(char *mntdir, struct obd_statfs *stat,
571 char *uuid, int ishow, int cooked,
572 char *type, int index, int rc)
574 long long avail, used, total;
576 char *suffix = "KMGTPEZY";
577 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
585 avail = stat->os_ffree;
586 used = stat->os_files - stat->os_ffree;
587 total = stat->os_files;
589 int shift = cooked ? 0 : 10;
591 avail = (stat->os_bavail * stat->os_bsize) >> shift;
592 used = stat->os_blocks - stat->os_bavail;
593 used = (used * stat->os_bsize) >> shift;
594 total = (stat->os_blocks * stat->os_bsize) >> shift;
598 ratio = (double)used / (double)total;
604 cook_val = (double)total;
607 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
609 sprintf(tbuf, CDF, total);
611 cook_val = (double)used;
614 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
616 sprintf(ubuf, CDF, used);
618 cook_val = (double)avail;
621 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
623 sprintf(abuf, CDF, avail);
625 sprintf(tbuf, CDF, total);
626 sprintf(ubuf, CDF, used);
627 sprintf(abuf, CDF, avail);
630 sprintf(rbuf, RDF, (int)(ratio * 100));
631 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
632 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
634 printf("[%s:%d]\n", type, index);
640 printf(UUF": inactive device\n", uuid);
643 printf(UUF": %s\n", uuid, strerror(-rc));
650 static int mntdf(char *mntdir, int ishow, int cooked)
652 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
653 struct obd_uuid uuid_buf;
658 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
659 "UUID", "Inodes", "IUsed", "IFree",
660 "IUse%", "Mounted on");
662 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
663 "UUID", cooked ? "bytes" : "1K-blocks",
664 "Used", "Available", "Use%", "Mounted on");
666 for (index = 0; ; index++) {
667 memset(&stat_buf, 0, sizeof(struct obd_statfs));
668 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
669 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
670 &stat_buf, &uuid_buf);
677 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
678 rc == -ENODATA || rc == 0) {
679 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
683 "error: llapi_obd_statfs(%s): %s (%d)\n",
684 uuid_buf.uuid, strerror(-rc), rc);
688 sum.os_ffree += stat_buf.os_ffree;
689 sum.os_files += stat_buf.os_files;
693 for (index = 0; ; index++) {
694 memset(&stat_buf, 0, sizeof(struct obd_statfs));
695 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
696 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
697 &stat_buf, &uuid_buf);
704 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
705 rc == -ENODATA || rc == 0) {
706 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
710 "error: llapi_obd_statfs failed: %s (%d)\n",
715 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
716 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
717 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
722 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
727 static int lfs_df(int argc, char **argv)
731 struct mntent *mnt = NULL;
732 char mntdir[PATH_MAX] = {'\0'};
733 int ishow = 0, cooked = 0;
737 while ((c = getopt(argc, argv, "ih")) != -1) {
752 fp = setmntent(MOUNTED, "r");
755 fprintf(stderr, "error: %s: open %s failed( %s )\n",
756 argv[0], MOUNTED, strerror(errno));
760 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
766 rc = mntdf(mntdir, ishow, cooked);
771 while (feof(fp) == 0 && ferror(fp) == 0) {
772 if (llapi_is_lustre_mnttype(mnt)) {
773 rc = mntdf(mnt->mnt_dir, ishow, cooked);
786 static int lfs_check(int argc, char **argv)
790 struct mntent *mnt = NULL;
799 obd_types[0] = obd_type1;
800 obd_types[1] = obd_type2;
802 if (strcmp(argv[1], "osts") == 0) {
803 strcpy(obd_types[0], "osc");
804 } else if (strcmp(argv[1], "mds") == 0) {
805 strcpy(obd_types[0], "mdc");
806 } else if (strcmp(argv[1], "servers") == 0) {
808 strcpy(obd_types[0], "osc");
809 strcpy(obd_types[1], "mdc");
811 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
816 fp = setmntent(MOUNTED, "r");
818 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
822 while (feof(fp) == 0 && ferror(fp) ==0) {
823 if (llapi_is_lustre_mnttype(mnt))
831 fprintf(stderr, "No suitable Lustre mount found\n");
835 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
838 fprintf(stderr, "error: %s: %s status failed\n",
845 static int lfs_catinfo(int argc, char **argv)
848 struct mntent *mnt = NULL;
851 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
854 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
857 fp = setmntent(MOUNTED, "r");
859 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
863 while (feof(fp) == 0 && ferror(fp) == 0) {
864 if (llapi_is_lustre_mnttype(mnt))
873 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
875 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
877 fprintf(stderr, "no lustre_lite mounted.\n");
884 int lfs_join(int argc, char **argv)
886 char *name_head, *name_tail;
893 fd = open(name_head, O_WRONLY);
895 fprintf(stderr, "Can not open name_head %s rc=%d\n",
899 size = lseek(fd, 0, SEEK_END);
900 if (size % JOIN_FILE_ALIGN) {
901 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
902 name_head, (long long)size, JOIN_FILE_ALIGN);
907 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
911 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
917 #ifdef HAVE_QUOTA_SUPPORT
918 static int lfs_quotachown(int argc, char **argv)
924 while ((c = getopt(argc, argv, "i")) != -1) {
930 fprintf(stderr, "error: %s: option '-%c' "
931 "unrecognized\n", argv[0], c);
937 rc = llapi_quotachown(argv[optind], flag);
939 fprintf(stderr,"error: change file owner/group failed.\n");
944 static int lfs_quotacheck(int argc, char **argv)
946 int c, check_type = 0;
948 struct if_quotacheck qchk;
949 struct if_quotactl qctl;
950 char *obd_type = qchk.obd_type;
951 char *obd_uuid = qchk.obd_uuid.uuid;
954 memset(&qchk, 0, sizeof(qchk));
957 while ((c = getopt(argc, argv, "ug")) != -1) {
966 fprintf(stderr, "error: %s: option '-%c' "
967 "unrecognized\n", argv[0], c);
974 else /* do quotacheck for both user & group quota by default */
982 memset(&qctl, 0, sizeof(qctl));
983 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
984 qctl.qc_id = QFMT_LDISKFS;
985 qctl.qc_type = check_type;
986 rc = llapi_quotactl(mnt, &qctl);
988 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
992 rc = llapi_quotacheck(mnt, check_type);
994 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
998 rc = llapi_poll_quotacheck(mnt, &qchk);
1001 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1002 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1006 memset(&qctl, 0, sizeof(qctl));
1007 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1008 qctl.qc_id = QFMT_LDISKFS;
1009 qctl.qc_type = check_type;
1010 rc = llapi_quotactl(mnt, &qctl);
1013 fprintf(stderr, "%s %s ",
1014 qctl.obd_type, qctl.obd_uuid.uuid);
1015 fprintf(stderr, "%s turn on quota failed: %s\n",
1016 argv[0], strerror(errno));
1023 static int lfs_quotaon(int argc, char **argv)
1027 struct if_quotactl qctl;
1028 char *obd_type = qctl.obd_type;
1029 char *obd_uuid = qctl.obd_uuid.uuid;
1032 memset(&qctl, 0, sizeof(qctl));
1033 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1034 qctl.qc_id = QFMT_LDISKFS;
1037 while ((c = getopt(argc, argv, "ugf")) != -1) {
1040 qctl.qc_type |= 0x01;
1043 qctl.qc_type |= 0x02;
1046 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1049 fprintf(stderr, "error: %s: option '-%c' "
1050 "unrecognized\n", argv[0], c);
1063 rc = llapi_quotactl(mnt, &qctl);
1066 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1067 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1074 static int lfs_quotaoff(int argc, char **argv)
1078 struct if_quotactl qctl;
1079 char *obd_type = qctl.obd_type;
1080 char *obd_uuid = qctl.obd_uuid.uuid;
1083 memset(&qctl, 0, sizeof(qctl));
1084 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1087 while ((c = getopt(argc, argv, "ug")) != -1) {
1090 qctl.qc_type |= 0x01;
1093 qctl.qc_type |= 0x02;
1096 fprintf(stderr, "error: %s: option '-%c' "
1097 "unrecognized\n", argv[0], c);
1110 rc = llapi_quotactl(mnt, &qctl);
1113 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1114 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1121 static int name2id(unsigned int *id, char *name, int type)
1123 if (type == USRQUOTA) {
1124 struct passwd *entry;
1126 if (!(entry = getpwnam(name))) {
1132 *id = entry->pw_uid;
1134 struct group *entry;
1136 if (!(entry = getgrnam(name))) {
1142 *id = entry->gr_gid;
1148 static int id2name(char **name, unsigned int id, int type)
1150 if (type == USRQUOTA) {
1151 struct passwd *entry;
1153 if (!(entry = getpwuid(id))) {
1159 *name = entry->pw_name;
1161 struct group *entry;
1163 if (!(entry = getgrgid(id))) {
1169 *name = entry->gr_name;
1175 #define ARG2INT(nr, str, msg) \
1178 nr = strtol(str, &endp, 0); \
1180 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1185 int lfs_setquota(int argc, char **argv)
1189 struct if_quotactl qctl;
1190 char *obd_type = qctl.obd_type;
1191 char *obd_uuid = qctl.obd_uuid.uuid;
1194 memset(&qctl, 0, sizeof(qctl));
1195 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1198 while ((c = getopt(argc, argv, "ugt")) != -1) {
1201 qctl.qc_type |= 0x01;
1204 qctl.qc_type |= 0x02;
1207 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1210 fprintf(stderr, "error: %s: option '-%c' "
1211 "unrecognized\n", argv[0], c);
1219 if (qctl.qc_type == UGQUOTA) {
1220 fprintf(stderr, "error: user and group quotas can't be set "
1225 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1226 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1228 if (optind + 6 != argc)
1231 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1233 fprintf(stderr, "error: find id for name %s failed: %s\n",
1234 argv[optind - 1], strerror(errno));
1238 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1239 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1240 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1241 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1243 dqb->dqb_valid = QIF_LIMITS;
1245 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1247 if (optind + 3 != argc)
1250 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1251 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1256 rc = llapi_quotactl(mnt, &qctl);
1259 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1260 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1267 static inline char *type2name(int check_type)
1269 if (check_type == USRQUOTA)
1271 else if (check_type == GRPQUOTA)
1278 static void grace2str(time_t seconds,char *buf)
1280 uint minutes, hours, days;
1282 minutes = (seconds + 30) / 60;
1283 hours = minutes / 60;
1288 snprintf(buf, 40, "%ddays", days);
1290 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1294 static void diff2str(time_t seconds, char *buf, time_t now)
1300 if (seconds <= now) {
1301 strcpy(buf, "none");
1304 grace2str(seconds - now, buf);
1307 static void print_quota_title(char *name, struct if_quotactl *qctl)
1309 printf("Disk quotas for %s %s (%cid %u):\n",
1310 type2name(qctl->qc_type), name,
1311 *type2name(qctl->qc_type), qctl->qc_id);
1312 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1314 "blocks", "quota", "limit", "grace",
1315 "files", "quota", "limit", "grace");
1318 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1324 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1325 int bover = 0, iover = 0;
1326 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1328 if (dqb->dqb_bhardlimit &&
1329 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1331 } else if (dqb->dqb_bsoftlimit &&
1332 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1333 if (dqb->dqb_btime > now) {
1340 if (dqb->dqb_ihardlimit &&
1341 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1343 } else if (dqb->dqb_isoftlimit &&
1344 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1345 if (dqb->dqb_btime > now) {
1352 #if 0 /* XXX: always print quotas even when no usages */
1353 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1359 if (strlen(mnt) > 15)
1360 printf("%s\n%15s", mnt, "");
1362 printf("%15s", mnt);
1365 diff2str(dqb->dqb_btime, timebuf, now);
1367 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1368 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1369 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1370 printf(" %7s%c %6s %7s %7s",
1371 numbuf[0], bover ? '*' : ' ', numbuf[1],
1372 numbuf[2], bover > 1 ? timebuf : "");
1375 diff2str(dqb->dqb_itime, timebuf, now);
1377 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1378 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1379 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1381 printf(" %7s%c %6s %7s %7s",
1382 numbuf[0], iover ? '*' : ' ', numbuf[1],
1383 numbuf[2], iover > 1 ? timebuf : "");
1386 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1387 qctl->qc_cmd == Q_GETOINFO) {
1391 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1392 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1393 printf("Block grace time: %s; Inode grace time: %s\n",
1394 bgtimebuf, igtimebuf);
1398 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1402 /* XXX: this is a flag to mark that only mds quota is wanted */
1403 qctl->qc_dqblk.dqb_valid = 1;
1404 rc = llapi_quotactl(mnt, qctl);
1406 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1409 qctl->qc_dqblk.dqb_valid = 0;
1411 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1414 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1417 struct obd_uuid uuids[1024], *uuidp;
1418 int obdcount = 1024;
1423 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1427 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1429 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1433 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1434 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1436 /* XXX clear this flag to get quota from osts */
1437 qctl->qc_dqblk.dqb_valid = 0;
1438 rc = llapi_quotactl(mnt, qctl);
1440 fprintf(stderr, "%s quotactl failed: %s\n",
1441 uuidp->uuid, strerror(errno));
1445 print_quota(uuidp->uuid, qctl, 1);
1453 static int lfs_quota(int argc, char **argv)
1456 char *name = NULL, *mnt;
1457 struct if_quotactl qctl;
1458 char *obd_type = qctl.obd_type;
1459 char *obd_uuid = qctl.obd_uuid.uuid;
1462 memset(&qctl, 0, sizeof(qctl));
1463 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1466 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1469 qctl.qc_type |= 0x01;
1472 qctl.qc_type |= 0x02;
1475 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1478 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1481 fprintf(stderr, "error: %s: option '-%c' "
1482 "unrecognized\n", argv[0], c);
1490 if (qctl.qc_type == UGQUOTA) {
1491 fprintf(stderr, "error: user or group can't be specified"
1496 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1497 if (optind + 2 != argc)
1500 name = argv[optind++];
1501 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1503 fprintf(stderr, "error: find id for name %s failed: %s\n",
1504 name, strerror(errno));
1507 print_quota_title(name, &qctl);
1508 } else if (optind + 1 != argc) {
1514 rc = llapi_quotactl(mnt, &qctl);
1517 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1518 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1523 rc = id2name(&name, getuid(), qctl.qc_type);
1530 print_quota(mnt, &qctl, 0);
1532 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1533 print_mds_quota(mnt, &qctl);
1534 print_lov_quota(mnt, &qctl);
1539 #endif /* HAVE_QUOTA_SUPPORT */
1541 int main(int argc, char **argv)
1547 ptl_initialize(argc, argv);
1548 if (obd_initialize(argc, argv) < 0)
1550 if (dbg_initialize(argc, argv) < 0)
1553 Parser_init("lfs > ", cmdlist);
1556 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1558 rc = Parser_commands();
1561 obd_finalize(argc, argv);