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>
42 #include <lnet/api-support.h>
43 #include <lnet/lnetctl.h>
45 #include <liblustre.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre/liblustreapi.h>
48 #include <lustre/lustre_user.h>
53 unsigned int libcfs_subsystem_debug = 0;
56 static int lfs_setstripe(int argc, char **argv);
57 static int lfs_find(int argc, char **argv);
58 static int lfs_getstripe(int argc, char **argv);
59 static int lfs_osts(int argc, char **argv);
60 static int lfs_df(int argc, char **argv);
61 static int lfs_check(int argc, char **argv);
62 static int lfs_catinfo(int argc, char **argv);
63 #ifdef HAVE_QUOTA_SUPPORT
64 static int lfs_quotachown(int argc, char **argv);
65 static int lfs_quotacheck(int argc, char **argv);
66 static int lfs_quotaon(int argc, char **argv);
67 static int lfs_quotaoff(int argc, char **argv);
68 static int lfs_setquota(int argc, char **argv);
69 static int lfs_quota(int argc, char **argv);
71 static int lfs_join(int argc, char **argv);
73 /* all avaialable commands */
74 command_t cmdlist[] = {
75 {"setstripe", lfs_setstripe, 0,
76 "Create a new file with a specific striping pattern or\n"
77 "set the default striping pattern on an existing directory or\n"
78 "delete the default striping pattern from an existing directory\n"
79 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
81 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
82 " [--index|-i stripe_index]\n"
83 " [--count|-c stripe_count]\n"
85 " setstripe -d <dirname> (to delete default striping)\n"
86 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
87 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
88 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
89 {"getstripe", lfs_getstripe, 0,
90 "To list the striping info for a given filename or files in a\n"
91 "directory or recursively for all files in a directory tree.\n"
92 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
93 " [--recursive | -r] <dir|file> ..."},
95 "To find files that match given parameters recursively in a directory tree.\n"
96 "usage: find <dir/file> ... \n"
97 " [[!] --atime|-A N] [[!] --mtime|-M N] [[!] --ctime|-C N] [--maxdepth|-D N]\n"
98 " [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
99 "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
100 "\t !: used before --name means find exclude the regular expression pattern\n"
101 "If one of the options below is provided, find works the same as 'getstripe':\n"
102 "To list the striping info for a given filename or files in a directory or\n"
104 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
105 " [--recursive | -r] <dir|file> ..."},
106 {"check", lfs_check, 0,
107 "Display the status of MDS or OSTs (as specified in the command)\n"
108 "or all the servers (MDS and OSTs).\n"
109 "usage: check <osts|mds|servers>"},
110 {"catinfo", lfs_catinfo, 0,
111 "Show information of specified type logs.\n"
112 "usage: catinfo {keyword} [node name]\n"
113 "\tkeywords are one of followings: config, deletions.\n"
114 "\tnode name must be provided when use keyword config."},
115 {"join", lfs_join, 0,
116 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
117 "usage: join <filename_A> <filename_B>\n"},
118 {"osts", lfs_osts, 0, "osts"},
120 "report filesystem disk space usage or inodes usage"
122 "Usage: df [-i] [-h] [path]"},
123 #ifdef HAVE_QUOTA_SUPPORT
124 {"quotachown",lfs_quotachown, 0,
125 "Change files' owner or group on the specified filesystem.\n"
126 "usage: quotachown [-i] <filesystem>\n"
127 "\t-i: ignore error if file is not exist\n"},
128 {"quotacheck", lfs_quotacheck, 0,
129 "Scan the specified filesystem for disk usage, and create,\n"
130 "or update quota files.\n"
131 "usage: quotacheck [ -ug ] <filesystem>"},
132 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
133 "usage: quotaon [ -ugf ] <filesystem>"},
134 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
135 "usage: quotaoff [ -ug ] <filesystem>"},
136 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
137 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
138 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
139 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
140 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
142 {"help", Parser_help, 0, "help"},
143 {"exit", Parser_quit, 0, "quit"},
144 {"quit", Parser_quit, 0, "quit"},
149 static int lfs_setstripe(int argc, char **argv)
154 int st_offset, st_count;
158 char *stripe_size_arg = NULL;
159 char *stripe_off_arg = NULL;
160 char *stripe_count_arg = NULL;
162 struct option long_opts[] = {
163 {"size", required_argument, 0, 's'},
164 {"count", required_argument, 0, 'c'},
165 {"index", required_argument, 0, 'i'},
166 {"delete", no_argument, 0, 'd'},
173 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
174 /* for compatibility with the existing positional parameter
178 } else if (argc == 5 &&
179 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
180 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
181 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
182 /* for compatibility with the existing positional parameter
185 stripe_size_arg = argv[2];
186 stripe_off_arg = argv[3];
187 stripe_count_arg = argv[4];
190 while ((c = getopt_long(argc, argv, "c:di:s:",
191 long_opts, NULL)) >= 0)
198 stripe_count_arg = optarg;
201 /* delete the default striping pattern */
205 stripe_off_arg = optarg;
208 stripe_size_arg = optarg;
213 fprintf(stderr, "error: %s: option '%s' "
215 argv[0], argv[optind - 1]);
221 fname = argv[optind];
226 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
227 stripe_count_arg != NULL)) {
228 fprintf(stderr, "error: %s: cannot specify -d with "
229 "-s, -c or -i options\n",
235 if (optind != argc - 1) {
236 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
238 argv[0], argv[argc-1]);
242 /* get the stripe size */
243 if (stripe_size_arg != NULL) {
244 st_size = strtoul(stripe_size_arg, &end, 0);
246 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
247 argv[0], stripe_size_arg);
251 /* get the stripe offset */
252 if (stripe_off_arg != NULL) {
253 st_offset = strtoul(stripe_off_arg, &end, 0);
255 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
256 argv[0], stripe_off_arg);
260 /* get the stripe count */
261 if (stripe_count_arg != NULL) {
262 st_count = strtoul(stripe_count_arg, &end, 0);
264 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
265 argv[0], stripe_count_arg);
270 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
272 fprintf(stderr, "error: %s: create stripe file failed\n",
278 static int set_time(time_t *time, time_t *set, char *str)
285 else if (str[0] == '-')
291 t = strtol(str, NULL, 0);
292 if (*time < t * 24 * 60 * 60) {
295 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
299 *set = *time - t * 24 * 60 * 60;
303 static int lfs_find(int argc, char **argv)
310 int quiet, verbose, recursive;
311 struct find_param param;
312 struct obd_uuid *obduuid = NULL;
314 struct option long_opts[] = {
315 /* New find options. */
316 {"atime", required_argument, 0, 'A'},
317 {"ctime", required_argument, 0, 'C'},
318 {"mtime", required_argument, 0, 'M'},
319 {"maxdepth", required_argument, 0, 'D'},
320 {"name", required_argument, 0, 'n'},
321 /* --obd is considered as a new option. */
322 {"obd", required_argument, 0, 'O'},
323 {"ost", required_argument, 0, 'O'},
324 {"print", no_argument, 0, 'P'},
325 {"print0", no_argument, 0, 'p'},
326 /* Old find options. */
327 {"quiet", no_argument, 0, 'q'},
328 {"recursive", no_argument, 0, 'r'},
329 {"verbose", no_argument, 0, 'v'},
342 quiet = verbose = recursive = 0;
344 memset(¶m, 0, sizeof(param));
346 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpOqrv",
347 long_opts, NULL)) >= 0)
353 /* '!' is part of option */
354 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
355 if (!isoption && pathend != -1) {
356 fprintf(stderr, "err: %s: filename|dirname must either "
357 "precede options or follow options\n",
361 if (!isoption && pathstart == -1)
362 pathstart = optind - 1;
363 if (isoption && pathstart != -1 && pathend == -1) {
364 pathend = optind - 2;
365 if ((c == 1 && strcmp(optarg, "!") == 0) ||
366 c == 'P' || c == 'p' || c == 'O' ||
367 c == 'q' || c == 'r' || c == 'v')
368 pathend = optind - 1;
375 if (strcmp(optarg, "!") == 0)
379 xtime = ¶m.atime;
380 xsign = ¶m.asign;
383 xtime = ¶m.ctime;
384 xsign = ¶m.csign;
388 xtime = ¶m.mtime;
389 xsign = ¶m.msign;
393 if (optarg[0] == '-')
395 else if (optarg[0] == '+')
400 strcat(timestr, optarg);
404 ret = set_time(&t, xtime, optarg);
411 depth = strtol(optarg, 0, 0);
415 param.pattern = (char *)optarg;
417 param.exclude_pattern = 1;
419 param.exclude_pattern = 0;
424 "error: %s: only one obduuid allowed",
428 obduuid = (struct obd_uuid *)optarg;
452 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
453 argv[0], argv[optind - 1]);
458 if (pathstart == -1) {
459 fprintf(stderr, "error: %s: no filename|pathname\n",
462 } else if (pathend == -1) {
467 param.obduuid = obduuid;
469 param.maxdepth = depth;
470 param.zeroend = zeroend;
473 param.recursive = recursive;
474 param.verbose = verbose;
476 param.maxdepth = recursive ? -1 : 1;
481 ret = llapi_find(argv[pathstart], ¶m);
483 ret = llapi_getstripe(argv[pathstart], ¶m);
484 } while (++pathstart < pathend && !ret);
487 fprintf(stderr, "error: %s failed for %s.\n",
488 argv[0], argv[optind - 1]);
493 static int lfs_getstripe(int argc, char **argv)
495 struct option long_opts[] = {
497 {"quiet", 0, 0, 'q'},
498 {"recursive", 0, 0, 'r'},
499 {"verbose", 0, 0, 'v'},
502 char short_opts[] = "hO:qrv";
503 int quiet, verbose, recursive, c, rc;
504 struct obd_uuid *obduuid = NULL;
505 struct find_param param;
508 quiet = verbose = recursive = 0;
509 while ((c = getopt_long(argc, argv, short_opts,
510 long_opts, NULL)) != -1) {
515 "error: %s: only one obduuid allowed",
519 obduuid = (struct obd_uuid *)optarg;
535 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
536 argv[0], argv[optind - 1]);
544 memset(¶m, 0, sizeof(param));
545 param.recursive = recursive;
546 param.verbose = verbose;
548 param.obduuid = obduuid;
549 param.maxdepth = recursive ? -1 : 1;
552 rc = llapi_getstripe(argv[optind], ¶m);
553 } while (++optind < argc && !rc);
556 fprintf(stderr, "error: %s failed for %s.\n",
557 argv[0], argv[optind - 1]);
561 static int lfs_osts(int argc, char **argv)
564 struct mntent *mnt = NULL;
565 struct obd_uuid *obduuid = NULL;
566 struct find_param param;
572 fp = setmntent(MOUNTED, "r");
575 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
579 memset(¶m, 0, sizeof(param));
580 param.obduuid = obduuid;
581 while (feof(fp) == 0 && ferror(fp) ==0) {
582 if (llapi_is_lustre_mnt(mnt)) {
583 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
586 "error: %s: failed on %s\n",
587 argv[0], mnt->mnt_dir);
597 #define COOK(value) \
600 while (value > 1024) { \
614 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
616 char rpath[PATH_MAX] = {'\0'};
618 int rc, len, out_len = 0;
620 if (!realpath(path, rpath)) {
622 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
623 path, strerror(-rc));
629 while (feof(fp) == 0 && ferror(fp) == 0) {
630 if (llapi_is_lustre_mnt(mnt)) {
631 len = strlen(mnt->mnt_dir);
633 !strncmp(rpath, mnt->mnt_dir, len)) {
635 memset(mntdir, 0, dir_len);
636 strncpy(mntdir, mnt->mnt_dir, dir_len);
645 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
649 static int showdf(char *mntdir, struct obd_statfs *stat,
650 char *uuid, int ishow, int cooked,
651 char *type, int index, int rc)
653 long long avail, used, total;
655 char *suffix = "KMGTPEZY";
656 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
664 avail = stat->os_ffree;
665 used = stat->os_files - stat->os_ffree;
666 total = stat->os_files;
668 int shift = cooked ? 0 : 10;
670 avail = (stat->os_bavail * stat->os_bsize) >> shift;
671 used = stat->os_blocks - stat->os_bavail;
672 used = (used * stat->os_bsize) >> shift;
673 total = (stat->os_blocks * stat->os_bsize) >> shift;
677 ratio = (double)used / (double)total;
683 cook_val = (double)total;
686 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
688 sprintf(tbuf, CDF, total);
690 cook_val = (double)used;
693 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
695 sprintf(ubuf, CDF, used);
697 cook_val = (double)avail;
700 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
702 sprintf(abuf, CDF, avail);
704 sprintf(tbuf, CDF, total);
705 sprintf(ubuf, CDF, used);
706 sprintf(abuf, CDF, avail);
709 sprintf(rbuf, RDF, (int)(ratio * 100));
710 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
711 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
713 printf("[%s:%d]\n", type, index);
719 printf(UUF": inactive device\n", uuid);
722 printf(UUF": %s\n", uuid, strerror(-rc));
729 static int mntdf(char *mntdir, int ishow, int cooked)
731 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
732 struct obd_uuid uuid_buf;
737 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
738 "UUID", "Inodes", "IUsed", "IFree",
739 "IUse%", "Mounted on");
741 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
742 "UUID", cooked ? "bytes" : "1K-blocks",
743 "Used", "Available", "Use%", "Mounted on");
745 for (index = 0; ; index++) {
746 memset(&stat_buf, 0, sizeof(struct obd_statfs));
747 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
748 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
749 &stat_buf, &uuid_buf);
756 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
757 rc == -ENODATA || rc == 0) {
758 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
762 "error: llapi_obd_statfs(%s): %s (%d)\n",
763 uuid_buf.uuid, strerror(-rc), rc);
767 sum.os_ffree += stat_buf.os_ffree;
768 sum.os_files += stat_buf.os_files;
772 for (index = 0; ; index++) {
773 memset(&stat_buf, 0, sizeof(struct obd_statfs));
774 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
775 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
776 &stat_buf, &uuid_buf);
783 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
784 rc == -ENODATA || rc == 0) {
785 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
789 "error: llapi_obd_statfs failed: %s (%d)\n",
794 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
795 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
796 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
801 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
806 static int lfs_df(int argc, char **argv)
810 struct mntent *mnt = NULL;
811 char mntdir[PATH_MAX] = {'\0'};
812 int ishow = 0, cooked = 0;
816 while ((c = getopt(argc, argv, "ih")) != -1) {
831 fp = setmntent(MOUNTED, "r");
834 fprintf(stderr, "error: %s: open %s failed( %s )\n",
835 argv[0], MOUNTED, strerror(errno));
839 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
845 rc = mntdf(mntdir, ishow, cooked);
850 while (feof(fp) == 0 && ferror(fp) == 0) {
851 if (llapi_is_lustre_mnt(mnt)) {
852 rc = mntdf(mnt->mnt_dir, ishow, cooked);
865 static int lfs_check(int argc, char **argv)
869 struct mntent *mnt = NULL;
878 obd_types[0] = obd_type1;
879 obd_types[1] = obd_type2;
881 if (strcmp(argv[1], "osts") == 0) {
882 strcpy(obd_types[0], "osc");
883 } else if (strcmp(argv[1], "mds") == 0) {
884 strcpy(obd_types[0], "mdc");
885 } else if (strcmp(argv[1], "servers") == 0) {
887 strcpy(obd_types[0], "osc");
888 strcpy(obd_types[1], "mdc");
890 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
895 fp = setmntent(MOUNTED, "r");
897 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
901 while (feof(fp) == 0 && ferror(fp) ==0) {
902 if (llapi_is_lustre_mnt(mnt))
910 fprintf(stderr, "No suitable Lustre mount found\n");
914 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
917 fprintf(stderr, "error: %s: %s status failed\n",
924 static int lfs_catinfo(int argc, char **argv)
927 struct mntent *mnt = NULL;
930 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
933 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
936 fp = setmntent(MOUNTED, "r");
938 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
942 while (feof(fp) == 0 && ferror(fp) == 0) {
943 if (llapi_is_lustre_mnt(mnt))
952 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
954 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
956 fprintf(stderr, "no lustre_lite mounted.\n");
963 int lfs_join(int argc, char **argv)
965 char *name_head, *name_tail;
972 fd = open(name_head, O_WRONLY);
974 fprintf(stderr, "Can not open name_head %s rc=%d\n",
978 size = lseek(fd, 0, SEEK_END);
979 if (size % JOIN_FILE_ALIGN) {
980 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
981 name_head, (long long)size, JOIN_FILE_ALIGN);
986 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
990 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
996 #ifdef HAVE_QUOTA_SUPPORT
997 static int lfs_quotachown(int argc, char **argv)
1003 while ((c = getopt(argc, argv, "i")) != -1) {
1009 fprintf(stderr, "error: %s: option '-%c' "
1010 "unrecognized\n", argv[0], c);
1016 rc = llapi_quotachown(argv[optind], flag);
1018 fprintf(stderr,"error: change file owner/group failed.\n");
1023 static int lfs_quotacheck(int argc, char **argv)
1025 int c, check_type = 0;
1027 struct if_quotacheck qchk;
1028 struct if_quotactl qctl;
1029 char *obd_type = qchk.obd_type;
1030 char *obd_uuid = qchk.obd_uuid.uuid;
1033 memset(&qchk, 0, sizeof(qchk));
1036 while ((c = getopt(argc, argv, "ug")) != -1) {
1045 fprintf(stderr, "error: %s: option '-%c' "
1046 "unrecognized\n", argv[0], c);
1053 else /* do quotacheck for both user & group quota by default */
1061 memset(&qctl, 0, sizeof(qctl));
1062 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1063 qctl.qc_id = QFMT_LDISKFS;
1064 qctl.qc_type = check_type;
1065 rc = llapi_quotactl(mnt, &qctl);
1067 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1071 rc = llapi_quotacheck(mnt, check_type);
1073 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1077 rc = llapi_poll_quotacheck(mnt, &qchk);
1080 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1081 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1085 memset(&qctl, 0, sizeof(qctl));
1086 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1087 qctl.qc_id = QFMT_LDISKFS;
1088 qctl.qc_type = check_type;
1089 rc = llapi_quotactl(mnt, &qctl);
1092 fprintf(stderr, "%s %s ",
1093 qctl.obd_type, qctl.obd_uuid.uuid);
1094 fprintf(stderr, "%s turn on quota failed: %s\n",
1095 argv[0], strerror(errno));
1102 static int lfs_quotaon(int argc, char **argv)
1106 struct if_quotactl qctl;
1107 char *obd_type = qctl.obd_type;
1108 char *obd_uuid = qctl.obd_uuid.uuid;
1111 memset(&qctl, 0, sizeof(qctl));
1112 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1113 qctl.qc_id = QFMT_LDISKFS;
1116 while ((c = getopt(argc, argv, "ugf")) != -1) {
1119 qctl.qc_type |= 0x01;
1122 qctl.qc_type |= 0x02;
1125 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1128 fprintf(stderr, "error: %s: option '-%c' "
1129 "unrecognized\n", argv[0], c);
1142 rc = llapi_quotactl(mnt, &qctl);
1145 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1146 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1153 static int lfs_quotaoff(int argc, char **argv)
1157 struct if_quotactl qctl;
1158 char *obd_type = qctl.obd_type;
1159 char *obd_uuid = qctl.obd_uuid.uuid;
1162 memset(&qctl, 0, sizeof(qctl));
1163 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1166 while ((c = getopt(argc, argv, "ug")) != -1) {
1169 qctl.qc_type |= 0x01;
1172 qctl.qc_type |= 0x02;
1175 fprintf(stderr, "error: %s: option '-%c' "
1176 "unrecognized\n", argv[0], c);
1189 rc = llapi_quotactl(mnt, &qctl);
1192 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1193 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1200 static int name2id(unsigned int *id, char *name, int type)
1202 if (type == USRQUOTA) {
1203 struct passwd *entry;
1205 if (!(entry = getpwnam(name))) {
1211 *id = entry->pw_uid;
1213 struct group *entry;
1215 if (!(entry = getgrnam(name))) {
1221 *id = entry->gr_gid;
1227 static int id2name(char **name, unsigned int id, int type)
1229 if (type == USRQUOTA) {
1230 struct passwd *entry;
1232 if (!(entry = getpwuid(id))) {
1238 *name = entry->pw_name;
1240 struct group *entry;
1242 if (!(entry = getgrgid(id))) {
1248 *name = entry->gr_name;
1254 #define ARG2INT(nr, str, msg) \
1257 nr = strtol(str, &endp, 0); \
1259 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1264 int lfs_setquota(int argc, char **argv)
1268 struct if_quotactl qctl;
1269 char *obd_type = qctl.obd_type;
1270 char *obd_uuid = qctl.obd_uuid.uuid;
1273 memset(&qctl, 0, sizeof(qctl));
1274 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1277 while ((c = getopt(argc, argv, "ugt")) != -1) {
1280 qctl.qc_type |= 0x01;
1283 qctl.qc_type |= 0x02;
1286 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1289 fprintf(stderr, "error: %s: option '-%c' "
1290 "unrecognized\n", argv[0], c);
1298 if (qctl.qc_type == UGQUOTA) {
1299 fprintf(stderr, "error: user and group quotas can't be set "
1304 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1305 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1307 if (optind + 6 != argc)
1310 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1312 fprintf(stderr, "error: find id for name %s failed: %s\n",
1313 argv[optind - 1], strerror(errno));
1317 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1318 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1319 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1320 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1322 dqb->dqb_valid = QIF_LIMITS;
1324 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1326 if (optind + 3 != argc)
1329 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1330 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1335 rc = llapi_quotactl(mnt, &qctl);
1338 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1339 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1346 static inline char *type2name(int check_type)
1348 if (check_type == USRQUOTA)
1350 else if (check_type == GRPQUOTA)
1357 static void grace2str(time_t seconds,char *buf)
1359 uint minutes, hours, days;
1361 minutes = (seconds + 30) / 60;
1362 hours = minutes / 60;
1367 snprintf(buf, 40, "%ddays", days);
1369 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1373 static void diff2str(time_t seconds, char *buf, time_t now)
1379 if (seconds <= now) {
1380 strcpy(buf, "none");
1383 grace2str(seconds - now, buf);
1386 static void print_quota_title(char *name, struct if_quotactl *qctl)
1388 printf("Disk quotas for %s %s (%cid %u):\n",
1389 type2name(qctl->qc_type), name,
1390 *type2name(qctl->qc_type), qctl->qc_id);
1391 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1393 "blocks", "quota", "limit", "grace",
1394 "files", "quota", "limit", "grace");
1397 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1403 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1404 int bover = 0, iover = 0;
1405 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1407 if (dqb->dqb_bhardlimit &&
1408 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1410 } else if (dqb->dqb_bsoftlimit &&
1411 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1412 if (dqb->dqb_btime > now) {
1419 if (dqb->dqb_ihardlimit &&
1420 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1422 } else if (dqb->dqb_isoftlimit &&
1423 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1424 if (dqb->dqb_btime > now) {
1431 #if 0 /* XXX: always print quotas even when no usages */
1432 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1438 if (strlen(mnt) > 15)
1439 printf("%s\n%15s", mnt, "");
1441 printf("%15s", mnt);
1444 diff2str(dqb->dqb_btime, timebuf, now);
1446 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1447 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1448 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1449 printf(" %7s%c %6s %7s %7s",
1450 numbuf[0], bover ? '*' : ' ', numbuf[1],
1451 numbuf[2], bover > 1 ? timebuf : "");
1454 diff2str(dqb->dqb_itime, timebuf, now);
1456 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1457 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1458 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1460 printf(" %7s%c %6s %7s %7s",
1461 numbuf[0], iover ? '*' : ' ', numbuf[1],
1462 numbuf[2], iover > 1 ? timebuf : "");
1465 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1466 qctl->qc_cmd == Q_GETOINFO) {
1470 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1471 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1472 printf("Block grace time: %s; Inode grace time: %s\n",
1473 bgtimebuf, igtimebuf);
1477 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1481 /* XXX: this is a flag to mark that only mds quota is wanted */
1482 qctl->qc_dqblk.dqb_valid = 1;
1483 rc = llapi_quotactl(mnt, qctl);
1485 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1488 qctl->qc_dqblk.dqb_valid = 0;
1490 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1493 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1496 struct obd_uuid uuids[1024], *uuidp;
1497 int obdcount = 1024;
1502 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1506 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1508 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1512 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1513 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1515 /* XXX clear this flag to get quota from osts */
1516 qctl->qc_dqblk.dqb_valid = 0;
1517 rc = llapi_quotactl(mnt, qctl);
1519 fprintf(stderr, "%s quotactl failed: %s\n",
1520 uuidp->uuid, strerror(errno));
1524 print_quota(uuidp->uuid, qctl, 1);
1532 static int lfs_quota(int argc, char **argv)
1535 char *name = NULL, *mnt;
1536 struct if_quotactl qctl;
1537 char *obd_type = qctl.obd_type;
1538 char *obd_uuid = qctl.obd_uuid.uuid;
1541 memset(&qctl, 0, sizeof(qctl));
1542 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1545 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1548 qctl.qc_type |= 0x01;
1551 qctl.qc_type |= 0x02;
1554 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1557 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1560 fprintf(stderr, "error: %s: option '-%c' "
1561 "unrecognized\n", argv[0], c);
1569 if (qctl.qc_type == UGQUOTA) {
1570 fprintf(stderr, "error: user or group can't be specified"
1575 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1576 if (optind + 2 != argc)
1579 name = argv[optind++];
1580 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1582 fprintf(stderr, "error: find id for name %s failed: %s\n",
1583 name, strerror(errno));
1586 print_quota_title(name, &qctl);
1587 } else if (optind + 1 != argc) {
1593 rc = llapi_quotactl(mnt, &qctl);
1596 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1597 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1602 rc = id2name(&name, getuid(), qctl.qc_type);
1609 print_quota(mnt, &qctl, 0);
1611 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1612 print_mds_quota(mnt, &qctl);
1613 print_lov_quota(mnt, &qctl);
1618 #endif /* HAVE_QUOTA_SUPPORT */
1620 int main(int argc, char **argv)
1626 ptl_initialize(argc, argv);
1627 if (obd_initialize(argc, argv) < 0)
1629 if (dbg_initialize(argc, argv) < 0)
1632 Parser_init("lfs > ", cmdlist);
1635 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1637 rc = Parser_commands();
1640 obd_finalize(argc, argv);