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);
674 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
675 rc == -ENODATA || rc == 0) {
676 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
680 "error: llapi_obd_statfs(%s): %s (%d)\n",
681 uuid_buf.uuid, strerror(-rc), rc);
685 sum.os_ffree += stat_buf.os_ffree;
686 sum.os_files += stat_buf.os_files;
690 for (index = 0; ; index++) {
691 memset(&stat_buf, 0, sizeof(struct obd_statfs));
692 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
693 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
694 &stat_buf, &uuid_buf);
698 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
699 rc == -ENODATA || rc == 0) {
700 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
704 "error: llapi_obd_statfs failed: %s (%d)\n",
709 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
710 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
711 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
716 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
721 static int lfs_df(int argc, char **argv)
725 struct mntent *mnt = NULL;
726 char mntdir[PATH_MAX] = {'\0'};
727 int ishow = 0, cooked = 0;
731 while ((c = getopt(argc, argv, "ih")) != -1) {
746 fp = setmntent(MOUNTED, "r");
749 fprintf(stderr, "error: %s: open %s failed( %s )\n",
750 argv[0], MOUNTED, strerror(errno));
754 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
760 rc = mntdf(mntdir, ishow, cooked);
765 while (feof(fp) == 0 && ferror(fp) == 0) {
766 if (llapi_is_lustre_mnttype(mnt)) {
767 rc = mntdf(mnt->mnt_dir, ishow, cooked);
780 static int lfs_check(int argc, char **argv)
784 struct mntent *mnt = NULL;
793 obd_types[0] = obd_type1;
794 obd_types[1] = obd_type2;
796 if (strcmp(argv[1], "osts") == 0) {
797 strcpy(obd_types[0], "osc");
798 } else if (strcmp(argv[1], "mds") == 0) {
799 strcpy(obd_types[0], "mdc");
800 } else if (strcmp(argv[1], "servers") == 0) {
802 strcpy(obd_types[0], "osc");
803 strcpy(obd_types[1], "mdc");
805 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
810 fp = setmntent(MOUNTED, "r");
812 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
816 while (feof(fp) == 0 && ferror(fp) ==0) {
817 if (llapi_is_lustre_mnttype(mnt))
825 fprintf(stderr, "No suitable Lustre mount found\n");
829 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
832 fprintf(stderr, "error: %s: %s status failed\n",
839 static int lfs_catinfo(int argc, char **argv)
842 struct mntent *mnt = NULL;
845 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
848 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
851 fp = setmntent(MOUNTED, "r");
853 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
857 while (feof(fp) == 0 && ferror(fp) == 0) {
858 if (llapi_is_lustre_mnttype(mnt))
867 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
869 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
871 fprintf(stderr, "no lustre_lite mounted.\n");
878 int lfs_join(int argc, char **argv)
880 char *name_head, *name_tail;
887 fd = open(name_head, O_WRONLY);
889 fprintf(stderr, "Can not open name_head %s rc=%d\n",
893 size = lseek(fd, 0, SEEK_END);
894 if (size % JOIN_FILE_ALIGN) {
895 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
896 name_head, (long long)size, JOIN_FILE_ALIGN);
901 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
905 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
911 #ifdef HAVE_QUOTA_SUPPORT
912 static int lfs_quotachown(int argc, char **argv)
918 while ((c = getopt(argc, argv, "i")) != -1) {
924 fprintf(stderr, "error: %s: option '-%c' "
925 "unrecognized\n", argv[0], c);
931 rc = llapi_quotachown(argv[optind], flag);
933 fprintf(stderr,"error: change file owner/group failed.\n");
938 static int lfs_quotacheck(int argc, char **argv)
940 int c, check_type = 0;
942 struct if_quotacheck qchk;
943 struct if_quotactl qctl;
944 char *obd_type = qchk.obd_type;
945 char *obd_uuid = qchk.obd_uuid.uuid;
948 memset(&qchk, 0, sizeof(qchk));
951 while ((c = getopt(argc, argv, "ug")) != -1) {
960 fprintf(stderr, "error: %s: option '-%c' "
961 "unrecognized\n", argv[0], c);
968 else /* do quotacheck for both user & group quota by default */
976 memset(&qctl, 0, sizeof(qctl));
977 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
978 qctl.qc_id = QFMT_LDISKFS;
979 qctl.qc_type = check_type;
980 rc = llapi_quotactl(mnt, &qctl);
982 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
986 rc = llapi_quotacheck(mnt, check_type);
988 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
992 rc = llapi_poll_quotacheck(mnt, &qchk);
995 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
996 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1000 memset(&qctl, 0, sizeof(qctl));
1001 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1002 qctl.qc_id = QFMT_LDISKFS;
1003 qctl.qc_type = check_type;
1004 rc = llapi_quotactl(mnt, &qctl);
1007 fprintf(stderr, "%s %s ",
1008 qctl.obd_type, qctl.obd_uuid.uuid);
1009 fprintf(stderr, "%s turn on quota failed: %s\n",
1010 argv[0], strerror(errno));
1017 static int lfs_quotaon(int argc, char **argv)
1021 struct if_quotactl qctl;
1022 char *obd_type = qctl.obd_type;
1023 char *obd_uuid = qctl.obd_uuid.uuid;
1026 memset(&qctl, 0, sizeof(qctl));
1027 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1028 qctl.qc_id = QFMT_LDISKFS;
1031 while ((c = getopt(argc, argv, "ugf")) != -1) {
1034 qctl.qc_type |= 0x01;
1037 qctl.qc_type |= 0x02;
1040 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1043 fprintf(stderr, "error: %s: option '-%c' "
1044 "unrecognized\n", argv[0], c);
1057 rc = llapi_quotactl(mnt, &qctl);
1060 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1061 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1068 static int lfs_quotaoff(int argc, char **argv)
1072 struct if_quotactl qctl;
1073 char *obd_type = qctl.obd_type;
1074 char *obd_uuid = qctl.obd_uuid.uuid;
1077 memset(&qctl, 0, sizeof(qctl));
1078 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1081 while ((c = getopt(argc, argv, "ug")) != -1) {
1084 qctl.qc_type |= 0x01;
1087 qctl.qc_type |= 0x02;
1090 fprintf(stderr, "error: %s: option '-%c' "
1091 "unrecognized\n", argv[0], c);
1104 rc = llapi_quotactl(mnt, &qctl);
1107 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1108 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1115 static int name2id(unsigned int *id, char *name, int type)
1117 if (type == USRQUOTA) {
1118 struct passwd *entry;
1120 if (!(entry = getpwnam(name))) {
1126 *id = entry->pw_uid;
1128 struct group *entry;
1130 if (!(entry = getgrnam(name))) {
1136 *id = entry->gr_gid;
1142 static int id2name(char **name, unsigned int id, int type)
1144 if (type == USRQUOTA) {
1145 struct passwd *entry;
1147 if (!(entry = getpwuid(id))) {
1153 *name = entry->pw_name;
1155 struct group *entry;
1157 if (!(entry = getgrgid(id))) {
1163 *name = entry->gr_name;
1169 #define ARG2INT(nr, str, msg) \
1172 nr = strtol(str, &endp, 0); \
1174 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1179 int lfs_setquota(int argc, char **argv)
1183 struct if_quotactl qctl;
1184 char *obd_type = qctl.obd_type;
1185 char *obd_uuid = qctl.obd_uuid.uuid;
1188 memset(&qctl, 0, sizeof(qctl));
1189 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1192 while ((c = getopt(argc, argv, "ugt")) != -1) {
1195 qctl.qc_type |= 0x01;
1198 qctl.qc_type |= 0x02;
1201 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1204 fprintf(stderr, "error: %s: option '-%c' "
1205 "unrecognized\n", argv[0], c);
1213 if (qctl.qc_type == UGQUOTA) {
1214 fprintf(stderr, "error: user and group quotas can't be set "
1219 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1220 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1222 if (optind + 6 != argc)
1225 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1227 fprintf(stderr, "error: find id for name %s failed: %s\n",
1228 argv[optind - 1], strerror(errno));
1232 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1233 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1234 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1235 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1237 dqb->dqb_valid = QIF_LIMITS;
1239 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1241 if (optind + 3 != argc)
1244 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1245 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1250 rc = llapi_quotactl(mnt, &qctl);
1253 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1254 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1261 static inline char *type2name(int check_type)
1263 if (check_type == USRQUOTA)
1265 else if (check_type == GRPQUOTA)
1272 static void grace2str(time_t seconds,char *buf)
1274 uint minutes, hours, days;
1276 minutes = (seconds + 30) / 60;
1277 hours = minutes / 60;
1282 snprintf(buf, 40, "%ddays", days);
1284 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1288 static void diff2str(time_t seconds, char *buf, time_t now)
1294 if (seconds <= now) {
1295 strcpy(buf, "none");
1298 grace2str(seconds - now, buf);
1301 static void print_quota_title(char *name, struct if_quotactl *qctl)
1303 printf("Disk quotas for %s %s (%cid %u):\n",
1304 type2name(qctl->qc_type), name,
1305 *type2name(qctl->qc_type), qctl->qc_id);
1306 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1308 "blocks", "quota", "limit", "grace",
1309 "files", "quota", "limit", "grace");
1312 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1318 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1319 int bover = 0, iover = 0;
1320 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1322 if (dqb->dqb_bhardlimit &&
1323 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1325 } else if (dqb->dqb_bsoftlimit &&
1326 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1327 if (dqb->dqb_btime > now) {
1334 if (dqb->dqb_ihardlimit &&
1335 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1337 } else if (dqb->dqb_isoftlimit &&
1338 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1339 if (dqb->dqb_btime > now) {
1346 #if 0 /* XXX: always print quotas even when no usages */
1347 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1353 if (strlen(mnt) > 15)
1354 printf("%s\n%15s", mnt, "");
1356 printf("%15s", mnt);
1359 diff2str(dqb->dqb_btime, timebuf, now);
1361 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1362 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1363 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1364 printf(" %7s%c %6s %7s %7s",
1365 numbuf[0], bover ? '*' : ' ', numbuf[1],
1366 numbuf[2], bover > 1 ? timebuf : "");
1369 diff2str(dqb->dqb_itime, timebuf, now);
1371 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1372 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1373 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1375 printf(" %7s%c %6s %7s %7s",
1376 numbuf[0], iover ? '*' : ' ', numbuf[1],
1377 numbuf[2], iover > 1 ? timebuf : "");
1380 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1381 qctl->qc_cmd == Q_GETOINFO) {
1385 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1386 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1387 printf("Block grace time: %s; Inode grace time: %s\n",
1388 bgtimebuf, igtimebuf);
1392 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1396 /* XXX: this is a flag to mark that only mds quota is wanted */
1397 qctl->qc_dqblk.dqb_valid = 1;
1398 rc = llapi_quotactl(mnt, qctl);
1400 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1403 qctl->qc_dqblk.dqb_valid = 0;
1405 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1408 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1411 struct obd_uuid uuids[1024], *uuidp;
1412 int obdcount = 1024;
1417 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1421 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1423 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1427 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1428 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1430 /* XXX clear this flag to get quota from osts */
1431 qctl->qc_dqblk.dqb_valid = 0;
1432 rc = llapi_quotactl(mnt, qctl);
1434 fprintf(stderr, "%s quotactl failed: %s\n",
1435 uuidp->uuid, strerror(errno));
1439 print_quota(uuidp->uuid, qctl, 1);
1447 static int lfs_quota(int argc, char **argv)
1450 char *name = NULL, *mnt;
1451 struct if_quotactl qctl;
1452 char *obd_type = qctl.obd_type;
1453 char *obd_uuid = qctl.obd_uuid.uuid;
1456 memset(&qctl, 0, sizeof(qctl));
1457 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1460 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1463 qctl.qc_type |= 0x01;
1466 qctl.qc_type |= 0x02;
1469 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1472 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1475 fprintf(stderr, "error: %s: option '-%c' "
1476 "unrecognized\n", argv[0], c);
1484 if (qctl.qc_type == UGQUOTA) {
1485 fprintf(stderr, "error: user or group can't be specified"
1490 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1491 if (optind + 2 != argc)
1494 name = argv[optind++];
1495 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1497 fprintf(stderr, "error: find id for name %s failed: %s\n",
1498 name, strerror(errno));
1501 print_quota_title(name, &qctl);
1502 } else if (optind + 1 != argc) {
1508 rc = llapi_quotactl(mnt, &qctl);
1511 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1512 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1517 rc = id2name(&name, getuid(), qctl.qc_type);
1524 print_quota(mnt, &qctl, 0);
1526 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1527 print_mds_quota(mnt, &qctl);
1528 print_lov_quota(mnt, &qctl);
1533 #endif /* HAVE_QUOTA_SUPPORT */
1535 int main(int argc, char **argv)
1541 ptl_initialize(argc, argv);
1542 if (obd_initialize(argc, argv) < 0)
1544 if (dbg_initialize(argc, argv) < 0)
1547 Parser_init("lfs > ", cmdlist);
1550 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1552 rc = Parser_commands();
1555 obd_finalize(argc, argv);