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_index> <stripe_count>\n"
80 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
81 " [--index|-i stripe_index]\n"
82 " [--count|-c stripe_count]\n"
84 " setstripe -d <dirname> (to delete default striping)\n"
85 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
86 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
87 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
88 {"getstripe", lfs_getstripe, 0,
89 "To list the striping info for a given filename or files in a\n"
90 "directory or recursively for all files in a directory tree.\n"
91 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
92 " [--recursive | -r] <dir|file> ..."},
94 "To find files that match given parameters recursively in a directory tree.\n"
95 "usage: find <dir/file> ... \n"
96 " [[!] --atime|-A N] [[!] --mtime|-M N] [[!] --ctime|-C N] [--maxdepth|-D N]\n"
97 " [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
98 "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
99 "\t !: used before --name means find exclude the regular expression pattern\n"
100 "If one of the options below is provided, find works the same as 'getstripe':\n"
101 "To list the striping info for a given filename or files in a directory or\n"
103 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
104 " [--recursive | -r] <dir|file> ..."},
105 {"check", lfs_check, 0,
106 "Display the status of MDS or OSTs (as specified in the command)\n"
107 "or all the servers (MDS and OSTs).\n"
108 "usage: check <osts|mds|servers>"},
109 {"catinfo", lfs_catinfo, 0,
110 "Show information of specified type logs.\n"
111 "usage: catinfo {keyword} [node name]\n"
112 "\tkeywords are one of followings: config, deletions.\n"
113 "\tnode name must be provided when use keyword config."},
114 {"join", lfs_join, 0,
115 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
116 "usage: join <filename_A> <filename_B>\n"},
117 {"osts", lfs_osts, 0, "osts"},
119 "report filesystem disk space usage or inodes usage"
121 "Usage: df [-i] [-h] [path]"},
122 #ifdef HAVE_QUOTA_SUPPORT
123 {"quotachown",lfs_quotachown, 0,
124 "Change files' owner or group on the specified filesystem.\n"
125 "usage: quotachown [-i] <filesystem>\n"
126 "\t-i: ignore error if file is not exist\n"},
127 {"quotacheck", lfs_quotacheck, 0,
128 "Scan the specified filesystem for disk usage, and create,\n"
129 "or update quota files.\n"
130 "usage: quotacheck [ -ug ] <filesystem>"},
131 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
132 "usage: quotaon [ -ugf ] <filesystem>"},
133 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
134 "usage: quotaoff [ -ug ] <filesystem>"},
135 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
136 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
137 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
138 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
139 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
141 {"help", Parser_help, 0, "help"},
142 {"exit", Parser_quit, 0, "quit"},
143 {"quit", Parser_quit, 0, "quit"},
148 static int lfs_setstripe(int argc, char **argv)
153 int st_offset, st_count;
157 char *stripe_size_arg = NULL;
158 char *stripe_off_arg = NULL;
159 char *stripe_count_arg = NULL;
161 struct option long_opts[] = {
162 {"size", required_argument, 0, 's'},
163 {"count", required_argument, 0, 'c'},
164 {"index", required_argument, 0, 'i'},
165 {"delete", no_argument, 0, 'd'},
172 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
173 /* for compatibility with the existing positional parameter
177 } else if (argc == 5 &&
178 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
179 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
180 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
181 /* for compatibility with the existing positional parameter
184 stripe_size_arg = argv[2];
185 stripe_off_arg = argv[3];
186 stripe_count_arg = argv[4];
189 while ((c = getopt_long(argc, argv, "c:di:s:",
190 long_opts, NULL)) >= 0)
197 stripe_count_arg = optarg;
200 /* delete the default striping pattern */
204 stripe_off_arg = optarg;
207 stripe_size_arg = optarg;
212 fprintf(stderr, "error: %s: option '%s' "
214 argv[0], argv[optind - 1]);
220 fname = argv[optind];
225 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
226 stripe_count_arg != NULL)) {
227 fprintf(stderr, "error: %s: cannot specify -d with "
228 "-s, -c or -i options\n",
234 if (optind != argc - 1) {
235 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
237 argv[0], argv[argc-1]);
241 /* get the stripe size */
242 if (stripe_size_arg != NULL) {
243 st_size = strtoul(stripe_size_arg, &end, 0);
245 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
246 argv[0], stripe_size_arg);
250 /* get the stripe offset */
251 if (stripe_off_arg != NULL) {
252 st_offset = strtoul(stripe_off_arg, &end, 0);
254 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
255 argv[0], stripe_off_arg);
259 /* get the stripe count */
260 if (stripe_count_arg != NULL) {
261 st_count = strtoul(stripe_count_arg, &end, 0);
263 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
264 argv[0], stripe_count_arg);
269 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
271 fprintf(stderr, "error: %s: create stripe file failed\n",
277 static int set_time(time_t *time, time_t *set, char *str)
284 else if (str[0] == '-')
290 t = strtol(str, NULL, 0);
291 if (*time < t * 24 * 60 * 60) {
294 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
298 *set = *time - t * 24 * 60 * 60;
302 static int lfs_find(int argc, char **argv)
309 int quiet, verbose, recursive;
310 struct find_param param;
311 struct obd_uuid *obduuid = NULL;
313 struct option long_opts[] = {
314 /* New find options. */
315 {"atime", required_argument, 0, 'A'},
316 {"ctime", required_argument, 0, 'C'},
317 {"mtime", required_argument, 0, 'M'},
318 {"maxdepth", required_argument, 0, 'D'},
319 {"name", required_argument, 0, 'n'},
320 /* --obd is considered as a new option. */
321 {"obd", required_argument, 0, 'O'},
322 {"ost", required_argument, 0, 'O'},
323 {"print", no_argument, 0, 'P'},
324 {"print0", no_argument, 0, 'p'},
325 /* Old find options. */
326 {"quiet", no_argument, 0, 'q'},
327 {"recursive", no_argument, 0, 'r'},
328 {"verbose", no_argument, 0, 'v'},
341 quiet = verbose = recursive = 0;
343 memset(¶m, 0, sizeof(param));
345 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpOqrv",
346 long_opts, NULL)) >= 0)
352 /* '!' is part of option */
353 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
354 if (!isoption && pathend != -1) {
355 fprintf(stderr, "err: %s: filename|dirname must either "
356 "precede options or follow options\n",
360 if (!isoption && pathstart == -1)
361 pathstart = optind - 1;
362 if (isoption && pathstart != -1 && pathend == -1) {
363 pathend = optind - 2;
364 if ((c == 1 && strcmp(optarg, "!") == 0) ||
365 c == 'P' || c == 'p' || c == 'O' ||
366 c == 'q' || c == 'r' || c == 'v')
367 pathend = optind - 1;
374 if (strcmp(optarg, "!") == 0)
378 xtime = ¶m.atime;
379 xsign = ¶m.asign;
382 xtime = ¶m.ctime;
383 xsign = ¶m.csign;
387 xtime = ¶m.mtime;
388 xsign = ¶m.msign;
392 if (optarg[0] == '-')
394 else if (optarg[0] == '+')
399 strcat(timestr, optarg);
403 ret = set_time(&t, xtime, optarg);
410 depth = strtol(optarg, 0, 0);
414 param.pattern = (char *)optarg;
416 param.exclude_pattern = 1;
418 param.exclude_pattern = 0;
423 "error: %s: only one obduuid allowed",
427 obduuid = (struct obd_uuid *)optarg;
451 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
452 argv[0], argv[optind - 1]);
457 if (pathstart == -1) {
458 fprintf(stderr, "error: %s: no filename|pathname\n",
461 } else if (pathend == -1) {
466 param.obduuid = obduuid;
468 param.maxdepth = depth;
469 param.zeroend = zeroend;
472 param.recursive = recursive;
473 param.verbose = verbose;
475 param.maxdepth = recursive ? -1 : 1;
480 ret = llapi_find(argv[pathstart], ¶m);
482 ret = llapi_getstripe(argv[pathstart], ¶m);
483 } while (++pathstart < pathend && !ret);
486 fprintf(stderr, "error: %s failed for %s.\n",
487 argv[0], argv[optind - 1]);
492 static int lfs_getstripe(int argc, char **argv)
494 struct option long_opts[] = {
496 {"quiet", 0, 0, 'q'},
497 {"recursive", 0, 0, 'r'},
498 {"verbose", 0, 0, 'v'},
501 char short_opts[] = "hO:qrv";
502 int quiet, verbose, recursive, c, rc;
503 struct obd_uuid *obduuid = NULL;
504 struct find_param param;
507 quiet = verbose = recursive = 0;
508 while ((c = getopt_long(argc, argv, short_opts,
509 long_opts, NULL)) != -1) {
514 "error: %s: only one obduuid allowed",
518 obduuid = (struct obd_uuid *)optarg;
534 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
535 argv[0], argv[optind - 1]);
543 memset(¶m, 0, sizeof(param));
544 param.recursive = recursive;
545 param.verbose = verbose;
547 param.obduuid = obduuid;
548 param.maxdepth = recursive ? -1 : 1;
551 rc = llapi_getstripe(argv[optind], ¶m);
552 } while (++optind < argc && !rc);
555 fprintf(stderr, "error: %s failed for %s.\n",
556 argv[0], argv[optind - 1]);
560 static int lfs_osts(int argc, char **argv)
563 struct mntent *mnt = NULL;
564 struct obd_uuid *obduuid = NULL;
565 struct find_param param;
571 fp = setmntent(MOUNTED, "r");
574 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
578 memset(¶m, 0, sizeof(param));
579 param.obduuid = obduuid;
580 while (feof(fp) == 0 && ferror(fp) ==0) {
581 if (llapi_is_lustre_mnt(mnt)) {
582 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
585 "error: %s: failed on %s\n",
586 argv[0], mnt->mnt_dir);
596 #define COOK(value) \
599 while (value > 1024) { \
613 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
615 char rpath[PATH_MAX] = {'\0'};
617 int rc, len, out_len = 0;
619 if (!realpath(path, rpath)) {
621 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
622 path, strerror(-rc));
628 while (feof(fp) == 0 && ferror(fp) == 0) {
629 if (llapi_is_lustre_mnt(mnt)) {
630 len = strlen(mnt->mnt_dir);
632 !strncmp(rpath, mnt->mnt_dir, len)) {
634 memset(mntdir, 0, dir_len);
635 strncpy(mntdir, mnt->mnt_dir, dir_len);
644 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
648 static int showdf(char *mntdir, struct obd_statfs *stat,
649 char *uuid, int ishow, int cooked,
650 char *type, int index, int rc)
652 long long avail, used, total;
654 char *suffix = "KMGTPEZY";
655 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
663 avail = stat->os_ffree;
664 used = stat->os_files - stat->os_ffree;
665 total = stat->os_files;
667 int shift = cooked ? 0 : 10;
669 avail = (stat->os_bavail * stat->os_bsize) >> shift;
670 used = stat->os_blocks - stat->os_bavail;
671 used = (used * stat->os_bsize) >> shift;
672 total = (stat->os_blocks * stat->os_bsize) >> shift;
676 ratio = (double)used / (double)total;
682 cook_val = (double)total;
685 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
687 sprintf(tbuf, CDF, total);
689 cook_val = (double)used;
692 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
694 sprintf(ubuf, CDF, used);
696 cook_val = (double)avail;
699 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
701 sprintf(abuf, CDF, avail);
703 sprintf(tbuf, CDF, total);
704 sprintf(ubuf, CDF, used);
705 sprintf(abuf, CDF, avail);
708 sprintf(rbuf, RDF, (int)(ratio * 100));
709 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
710 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
712 printf("[%s:%d]\n", type, index);
718 printf(UUF": inactive device\n", uuid);
721 printf(UUF": %s\n", uuid, strerror(-rc));
728 static int mntdf(char *mntdir, int ishow, int cooked)
730 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
731 struct obd_uuid uuid_buf;
736 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
737 "UUID", "Inodes", "IUsed", "IFree",
738 "IUse%", "Mounted on");
740 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
741 "UUID", cooked ? "bytes" : "1K-blocks",
742 "Used", "Available", "Use%", "Mounted on");
744 for (index = 0; ; index++) {
745 memset(&stat_buf, 0, sizeof(struct obd_statfs));
746 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
747 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
748 &stat_buf, &uuid_buf);
755 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
756 rc == -ENODATA || rc == 0) {
757 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
761 "error: llapi_obd_statfs(%s): %s (%d)\n",
762 uuid_buf.uuid, strerror(-rc), rc);
766 sum.os_ffree += stat_buf.os_ffree;
767 sum.os_files += stat_buf.os_files;
771 for (index = 0; ; index++) {
772 memset(&stat_buf, 0, sizeof(struct obd_statfs));
773 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
774 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
775 &stat_buf, &uuid_buf);
782 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
783 rc == -ENODATA || rc == 0) {
784 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
788 "error: llapi_obd_statfs failed: %s (%d)\n",
793 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
794 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
795 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
800 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
805 static int lfs_df(int argc, char **argv)
809 struct mntent *mnt = NULL;
810 char mntdir[PATH_MAX] = {'\0'};
811 int ishow = 0, cooked = 0;
815 while ((c = getopt(argc, argv, "ih")) != -1) {
830 fp = setmntent(MOUNTED, "r");
833 fprintf(stderr, "error: %s: open %s failed( %s )\n",
834 argv[0], MOUNTED, strerror(errno));
838 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
844 rc = mntdf(mntdir, ishow, cooked);
849 while (feof(fp) == 0 && ferror(fp) == 0) {
850 if (llapi_is_lustre_mnt(mnt)) {
851 rc = mntdf(mnt->mnt_dir, ishow, cooked);
864 static int lfs_check(int argc, char **argv)
868 struct mntent *mnt = NULL;
877 obd_types[0] = obd_type1;
878 obd_types[1] = obd_type2;
880 if (strcmp(argv[1], "osts") == 0) {
881 strcpy(obd_types[0], "osc");
882 } else if (strcmp(argv[1], "mds") == 0) {
883 strcpy(obd_types[0], "mdc");
884 } else if (strcmp(argv[1], "servers") == 0) {
886 strcpy(obd_types[0], "osc");
887 strcpy(obd_types[1], "mdc");
889 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
894 fp = setmntent(MOUNTED, "r");
896 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
900 while (feof(fp) == 0 && ferror(fp) ==0) {
901 if (llapi_is_lustre_mnt(mnt))
909 fprintf(stderr, "No suitable Lustre mount found\n");
913 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
916 fprintf(stderr, "error: %s: %s status failed\n",
923 static int lfs_catinfo(int argc, char **argv)
926 struct mntent *mnt = NULL;
929 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
932 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
935 fp = setmntent(MOUNTED, "r");
937 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
941 while (feof(fp) == 0 && ferror(fp) == 0) {
942 if (llapi_is_lustre_mnt(mnt))
951 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
953 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
955 fprintf(stderr, "no lustre_lite mounted.\n");
962 int lfs_join(int argc, char **argv)
964 char *name_head, *name_tail;
971 fd = open(name_head, O_WRONLY);
973 fprintf(stderr, "Can not open name_head %s rc=%d\n",
977 size = lseek(fd, 0, SEEK_END);
978 if (size % JOIN_FILE_ALIGN) {
979 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
980 name_head, (long long)size, JOIN_FILE_ALIGN);
985 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
989 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
995 #ifdef HAVE_QUOTA_SUPPORT
996 static int lfs_quotachown(int argc, char **argv)
1002 while ((c = getopt(argc, argv, "i")) != -1) {
1008 fprintf(stderr, "error: %s: option '-%c' "
1009 "unrecognized\n", argv[0], c);
1015 rc = llapi_quotachown(argv[optind], flag);
1017 fprintf(stderr,"error: change file owner/group failed.\n");
1022 static int lfs_quotacheck(int argc, char **argv)
1024 int c, check_type = 0;
1026 struct if_quotacheck qchk;
1027 struct if_quotactl qctl;
1028 char *obd_type = qchk.obd_type;
1029 char *obd_uuid = qchk.obd_uuid.uuid;
1032 memset(&qchk, 0, sizeof(qchk));
1035 while ((c = getopt(argc, argv, "ug")) != -1) {
1044 fprintf(stderr, "error: %s: option '-%c' "
1045 "unrecognized\n", argv[0], c);
1052 else /* do quotacheck for both user & group quota by default */
1060 memset(&qctl, 0, sizeof(qctl));
1061 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1062 qctl.qc_id = QFMT_LDISKFS;
1063 qctl.qc_type = check_type;
1064 rc = llapi_quotactl(mnt, &qctl);
1066 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1070 rc = llapi_quotacheck(mnt, check_type);
1072 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1076 rc = llapi_poll_quotacheck(mnt, &qchk);
1079 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1080 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1084 memset(&qctl, 0, sizeof(qctl));
1085 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1086 qctl.qc_id = QFMT_LDISKFS;
1087 qctl.qc_type = check_type;
1088 rc = llapi_quotactl(mnt, &qctl);
1091 fprintf(stderr, "%s %s ",
1092 qctl.obd_type, qctl.obd_uuid.uuid);
1093 fprintf(stderr, "%s turn on quota failed: %s\n",
1094 argv[0], strerror(errno));
1101 static int lfs_quotaon(int argc, char **argv)
1105 struct if_quotactl qctl;
1106 char *obd_type = qctl.obd_type;
1107 char *obd_uuid = qctl.obd_uuid.uuid;
1110 memset(&qctl, 0, sizeof(qctl));
1111 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1112 qctl.qc_id = QFMT_LDISKFS;
1115 while ((c = getopt(argc, argv, "ugf")) != -1) {
1118 qctl.qc_type |= 0x01;
1121 qctl.qc_type |= 0x02;
1124 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1127 fprintf(stderr, "error: %s: option '-%c' "
1128 "unrecognized\n", argv[0], c);
1141 rc = llapi_quotactl(mnt, &qctl);
1144 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1145 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1152 static int lfs_quotaoff(int argc, char **argv)
1156 struct if_quotactl qctl;
1157 char *obd_type = qctl.obd_type;
1158 char *obd_uuid = qctl.obd_uuid.uuid;
1161 memset(&qctl, 0, sizeof(qctl));
1162 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1165 while ((c = getopt(argc, argv, "ug")) != -1) {
1168 qctl.qc_type |= 0x01;
1171 qctl.qc_type |= 0x02;
1174 fprintf(stderr, "error: %s: option '-%c' "
1175 "unrecognized\n", argv[0], c);
1188 rc = llapi_quotactl(mnt, &qctl);
1191 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1192 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1199 static int name2id(unsigned int *id, char *name, int type)
1201 if (type == USRQUOTA) {
1202 struct passwd *entry;
1204 if (!(entry = getpwnam(name))) {
1210 *id = entry->pw_uid;
1212 struct group *entry;
1214 if (!(entry = getgrnam(name))) {
1220 *id = entry->gr_gid;
1226 static int id2name(char **name, unsigned int id, int type)
1228 if (type == USRQUOTA) {
1229 struct passwd *entry;
1231 if (!(entry = getpwuid(id))) {
1237 *name = entry->pw_name;
1239 struct group *entry;
1241 if (!(entry = getgrgid(id))) {
1247 *name = entry->gr_name;
1253 #define ARG2INT(nr, str, msg) \
1256 nr = strtol(str, &endp, 0); \
1258 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1263 int lfs_setquota(int argc, char **argv)
1267 struct if_quotactl qctl;
1268 char *obd_type = qctl.obd_type;
1269 char *obd_uuid = qctl.obd_uuid.uuid;
1272 memset(&qctl, 0, sizeof(qctl));
1273 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1276 while ((c = getopt(argc, argv, "ugt")) != -1) {
1279 qctl.qc_type |= 0x01;
1282 qctl.qc_type |= 0x02;
1285 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1288 fprintf(stderr, "error: %s: option '-%c' "
1289 "unrecognized\n", argv[0], c);
1297 if (qctl.qc_type == UGQUOTA) {
1298 fprintf(stderr, "error: user and group quotas can't be set "
1303 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1304 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1306 if (optind + 6 != argc)
1309 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1311 fprintf(stderr, "error: find id for name %s failed: %s\n",
1312 argv[optind - 1], strerror(errno));
1316 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1317 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1318 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1319 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1321 dqb->dqb_valid = QIF_LIMITS;
1323 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1325 if (optind + 3 != argc)
1328 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1329 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1334 rc = llapi_quotactl(mnt, &qctl);
1337 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1338 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1345 static inline char *type2name(int check_type)
1347 if (check_type == USRQUOTA)
1349 else if (check_type == GRPQUOTA)
1356 static void grace2str(time_t seconds,char *buf)
1358 uint minutes, hours, days;
1360 minutes = (seconds + 30) / 60;
1361 hours = minutes / 60;
1366 snprintf(buf, 40, "%ddays", days);
1368 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1372 static void diff2str(time_t seconds, char *buf, time_t now)
1378 if (seconds <= now) {
1379 strcpy(buf, "none");
1382 grace2str(seconds - now, buf);
1385 static void print_quota_title(char *name, struct if_quotactl *qctl)
1387 printf("Disk quotas for %s %s (%cid %u):\n",
1388 type2name(qctl->qc_type), name,
1389 *type2name(qctl->qc_type), qctl->qc_id);
1390 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1392 "blocks", "quota", "limit", "grace",
1393 "files", "quota", "limit", "grace");
1396 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1402 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1403 int bover = 0, iover = 0;
1404 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1406 if (dqb->dqb_bhardlimit &&
1407 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1409 } else if (dqb->dqb_bsoftlimit &&
1410 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1411 if (dqb->dqb_btime > now) {
1418 if (dqb->dqb_ihardlimit &&
1419 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1421 } else if (dqb->dqb_isoftlimit &&
1422 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1423 if (dqb->dqb_btime > now) {
1430 #if 0 /* XXX: always print quotas even when no usages */
1431 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1437 if (strlen(mnt) > 15)
1438 printf("%s\n%15s", mnt, "");
1440 printf("%15s", mnt);
1443 diff2str(dqb->dqb_btime, timebuf, now);
1445 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1446 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1447 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1448 printf(" %7s%c %6s %7s %7s",
1449 numbuf[0], bover ? '*' : ' ', numbuf[1],
1450 numbuf[2], bover > 1 ? timebuf : "");
1453 diff2str(dqb->dqb_itime, timebuf, now);
1455 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1456 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1457 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1459 printf(" %7s%c %6s %7s %7s",
1460 numbuf[0], iover ? '*' : ' ', numbuf[1],
1461 numbuf[2], iover > 1 ? timebuf : "");
1464 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1465 qctl->qc_cmd == Q_GETOINFO) {
1469 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1470 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1471 printf("Block grace time: %s; Inode grace time: %s\n",
1472 bgtimebuf, igtimebuf);
1476 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1480 /* XXX: this is a flag to mark that only mds quota is wanted */
1481 qctl->qc_dqblk.dqb_valid = 1;
1482 rc = llapi_quotactl(mnt, qctl);
1484 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1487 qctl->qc_dqblk.dqb_valid = 0;
1489 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1492 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1495 struct obd_uuid uuids[1024], *uuidp;
1496 int obdcount = 1024;
1501 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1505 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1507 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1511 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1512 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1514 /* XXX clear this flag to get quota from osts */
1515 qctl->qc_dqblk.dqb_valid = 0;
1516 rc = llapi_quotactl(mnt, qctl);
1518 fprintf(stderr, "%s quotactl failed: %s\n",
1519 uuidp->uuid, strerror(errno));
1523 print_quota(uuidp->uuid, qctl, 1);
1531 static int lfs_quota(int argc, char **argv)
1534 char *name = NULL, *mnt;
1535 struct if_quotactl qctl;
1536 char *obd_type = qctl.obd_type;
1537 char *obd_uuid = qctl.obd_uuid.uuid;
1540 memset(&qctl, 0, sizeof(qctl));
1541 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1544 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1547 qctl.qc_type |= 0x01;
1550 qctl.qc_type |= 0x02;
1553 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1556 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1559 fprintf(stderr, "error: %s: option '-%c' "
1560 "unrecognized\n", argv[0], c);
1568 if (qctl.qc_type == UGQUOTA) {
1569 fprintf(stderr, "error: user or group can't be specified"
1574 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1575 if (optind + 2 != argc)
1578 name = argv[optind++];
1579 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1581 fprintf(stderr, "error: find id for name %s failed: %s\n",
1582 name, strerror(errno));
1585 print_quota_title(name, &qctl);
1586 } else if (optind + 1 != argc) {
1592 rc = llapi_quotactl(mnt, &qctl);
1595 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1596 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1601 rc = id2name(&name, getuid(), qctl.qc_type);
1608 print_quota(mnt, &qctl, 0);
1610 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1611 print_mds_quota(mnt, &qctl);
1612 print_lov_quota(mnt, &qctl);
1617 #endif /* HAVE_QUOTA_SUPPORT */
1619 int main(int argc, char **argv)
1625 ptl_initialize(argc, argv);
1626 if (obd_initialize(argc, argv) < 0)
1628 if (dbg_initialize(argc, argv) < 0)
1631 Parser_init("lfs > ", cmdlist);
1634 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1636 rc = Parser_commands();
1639 obd_finalize(argc, argv);