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.
34 #include <sys/types.h>
43 #include <lnet/api-support.h>
44 #include <lnet/lnetctl.h>
46 #include <liblustre.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre/liblustreapi.h>
49 #include <lustre/lustre_user.h>
54 unsigned int libcfs_subsystem_debug = 0;
57 static int lfs_setstripe(int argc, char **argv);
58 static int lfs_find(int argc, char **argv);
59 static int lfs_getstripe(int argc, char **argv);
60 static int lfs_osts(int argc, char **argv);
61 static int lfs_df(int argc, char **argv);
62 static int lfs_check(int argc, char **argv);
63 static int lfs_catinfo(int argc, char **argv);
64 #ifdef HAVE_QUOTA_SUPPORT
65 static int lfs_quotachown(int argc, char **argv);
66 static int lfs_quotacheck(int argc, char **argv);
67 static int lfs_quotaon(int argc, char **argv);
68 static int lfs_quotaoff(int argc, char **argv);
69 static int lfs_setquota(int argc, char **argv);
70 static int lfs_quota(int argc, char **argv);
72 static int lfs_flushctx(int argc, char **argv);
73 static int lfs_join(int argc, char **argv);
74 static int lfs_getfacl(int argc, char **argv);
75 static int lfs_setfacl(int argc, char **argv);
77 /* all avaialable commands */
78 command_t cmdlist[] = {
79 {"setstripe", lfs_setstripe, 0,
80 "Create a new file with a specific striping pattern or\n"
81 "set the default striping pattern on an existing directory or\n"
82 "delete the default striping pattern from an existing directory\n"
83 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
85 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
86 " [--index|-i stripe_index]\n"
87 " [--count|-c stripe_count]\n"
89 " setstripe -d <dirname> (to delete default striping)\n"
90 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
91 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
92 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
93 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
94 {"getstripe", lfs_getstripe, 0,
95 "To list the striping info for a given filename or files in a\n"
96 "directory or recursively for all files in a directory tree.\n"
97 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
98 " [--recursive | -r] <dir|file> ..."},
100 "To find files that match given parameters recursively in a directory tree.\n"
101 "usage: find <dir/file> ... \n"
102 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
103 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
104 " [--print|-p] [--obd|-O <uuid>] [[!] --type|-t <filetype>]\n"
105 "\t !: used before an option indicates 'NOT' the requested attribute\n"
106 "\t -: used before an value indicates 'AT MOST' the requested value\n"
107 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
108 {"check", lfs_check, 0,
109 "Display the status of MDS or OSTs (as specified in the command)\n"
110 "or all the servers (MDS and OSTs).\n"
111 "usage: check <osts|mds|servers>"},
112 {"catinfo", lfs_catinfo, 0,
113 "Show information of specified type logs.\n"
114 "usage: catinfo {keyword} [node name]\n"
115 "\tkeywords are one of followings: config, deletions.\n"
116 "\tnode name must be provided when use keyword config."},
117 {"join", lfs_join, 0,
118 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
119 "usage: join <filename_A> <filename_B>\n"},
120 {"osts", lfs_osts, 0, "osts"},
122 "report filesystem disk space usage or inodes usage"
124 "Usage: df [-i] [-h] [path]"},
125 #ifdef HAVE_QUOTA_SUPPORT
126 {"quotachown",lfs_quotachown, 0,
127 "Change files' owner or group on the specified filesystem.\n"
128 "usage: quotachown [-i] <filesystem>\n"
129 "\t-i: ignore error if file is not exist\n"},
130 {"quotacheck", lfs_quotacheck, 0,
131 "Scan the specified filesystem for disk usage, and create,\n"
132 "or update quota files.\n"
133 "usage: quotacheck [ -ug ] <filesystem>"},
134 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
135 "usage: quotaon [ -ugf ] <filesystem>"},
136 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
137 "usage: quotaoff [ -ug ] <filesystem>"},
138 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
139 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
140 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
141 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
142 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
144 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
145 "usage: flushctx [-k] [mountpoint...]"},
146 {"getfacl", lfs_getfacl, 0,
147 "Get file access control list in remote client.\n"
148 "usage: getfacl [-dRLPvh] file"},
149 {"setfacl", lfs_setfacl, 0,
150 "Set file access control list in remote client.\n"
151 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
152 {"help", Parser_help, 0, "help"},
153 {"exit", Parser_quit, 0, "quit"},
154 {"quit", Parser_quit, 0, "quit"},
159 static int lfs_setstripe(int argc, char **argv)
163 unsigned long st_size;
164 int st_offset, st_count;
168 char *stripe_size_arg = NULL;
169 char *stripe_off_arg = NULL;
170 char *stripe_count_arg = NULL;
172 struct option long_opts[] = {
173 {"size", required_argument, 0, 's'},
174 {"count", required_argument, 0, 'c'},
175 {"index", required_argument, 0, 'i'},
176 {"delete", no_argument, 0, 'd'},
183 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
184 /* for compatibility with the existing positional parameter
188 } else if (argc == 5 &&
189 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
190 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
191 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
192 /* for compatibility with the existing positional parameter
195 stripe_size_arg = argv[2];
196 stripe_off_arg = argv[3];
197 stripe_count_arg = argv[4];
200 while ((c = getopt_long(argc, argv, "c:di:s:",
201 long_opts, NULL)) >= 0)
208 stripe_count_arg = optarg;
211 /* delete the default striping pattern */
215 stripe_off_arg = optarg;
218 stripe_size_arg = optarg;
223 fprintf(stderr, "error: %s: option '%s' "
225 argv[0], argv[optind - 1]);
230 fname = argv[optind];
236 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
237 stripe_count_arg != NULL)) {
238 fprintf(stderr, "error: %s: cannot specify -d with "
239 "-s, -c or -i options\n",
244 if (optind != argc - 1) {
245 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
247 argv[0], argv[argc - 1]);
251 /* get the stripe size */
252 if (stripe_size_arg != NULL) {
253 st_size = strtoul(stripe_size_arg, &end, 0);
255 if ((*end == 'k' || *end == 'K') &&
257 (st_size & (~0UL << (32 - 10))) == 0) {
259 } else if ((*end == 'm' || *end == 'M') &&
261 (st_size & (~0UL << (32 - 20))) == 0) {
263 } else if ((*end == 'g' || *end == 'G') &&
265 (st_size & (~0UL << (32 - 30))) == 0) {
268 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
269 argv[0], stripe_size_arg);
274 /* get the stripe offset */
275 if (stripe_off_arg != NULL) {
276 st_offset = strtoul(stripe_off_arg, &end, 0);
278 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
279 argv[0], stripe_off_arg);
283 /* get the stripe count */
284 if (stripe_count_arg != NULL) {
285 st_count = strtoul(stripe_count_arg, &end, 0);
287 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
288 argv[0], stripe_count_arg);
293 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
295 fprintf(stderr, "error: %s: create stripe file failed\n",
301 static int set_time(time_t *time, time_t *set, char *str)
308 else if (str[0] == '-')
314 t = strtol(str, NULL, 0);
315 if (*time < t * 24 * 60 * 60) {
318 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
322 *set = *time - t * 24 * 60 * 60;
326 static int lfs_find(int argc, char **argv)
331 struct find_param param = { .maxdepth = -1 };
333 struct option long_opts[] = {
334 /* New find options. */
335 {"atime", required_argument, 0, 'A'},
336 {"ctime", required_argument, 0, 'C'},
337 {"mtime", required_argument, 0, 'M'},
338 {"maxdepth", required_argument, 0, 'D'},
339 {"name", required_argument, 0, 'n'},
340 /* --obd is considered as a new option. */
341 {"obd", required_argument, 0, 'O'},
342 {"ost", required_argument, 0, 'O'},
343 {"print", no_argument, 0, 'P'},
344 {"print0", no_argument, 0, 'p'},
345 /* Old find options. */
346 {"quiet", no_argument, 0, 'q'},
347 {"recursive", no_argument, 0, 'r'},
348 {"type", required_argument, 0, 't'},
349 {"verbose", no_argument, 0, 'v'},
361 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrt:v",
362 long_opts, NULL)) >= 0) {
367 /* '!' is part of option */
368 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
369 if (!isoption && pathend != -1) {
370 fprintf(stderr, "err: %s: filename|dirname must either "
371 "precede options or follow options\n",
375 if (!isoption && pathstart == -1)
376 pathstart = optind - 1;
377 if (isoption && pathstart != -1 && pathend == -1) {
378 pathend = optind - 2;
379 if ((c == 1 && strcmp(optarg, "!") == 0) ||
380 c == 'P' || c == 'p' || c == 'O' ||
381 c == 'q' || c == 'r' || c == 'v')
382 pathend = optind - 1;
389 if (strcmp(optarg, "!") == 0)
393 xtime = ¶m.atime;
394 xsign = ¶m.asign;
397 xtime = ¶m.ctime;
398 xsign = ¶m.csign;
402 xtime = ¶m.mtime;
403 xsign = ¶m.msign;
407 if (optarg[0] == '-')
409 else if (optarg[0] == '+')
414 strcat(timestr, optarg);
418 ret = set_time(&t, xtime, optarg);
426 param.maxdepth = strtol(optarg, 0, 0);
430 param.pattern = (char *)optarg;
431 param.exclude_pattern = !!neg_opt;
436 "error: %s: only one obduuid allowed",
440 param.obduuid = (struct obd_uuid *)optarg;
458 param.exclude_type = !!neg_opt;
460 case 'b': param.type = S_IFBLK; break;
461 case 'c': param.type = S_IFCHR; break;
462 case 'd': param.type = S_IFDIR; break;
463 case 'f': param.type = S_IFREG; break;
464 case 'l': param.type = S_IFLNK; break;
465 case 'p': param.type = S_IFIFO; break;
466 case 's': param.type = S_IFSOCK; break;
467 #ifdef S_IFDOOR /* Solaris only */
468 case 'D': param.type = S_IFDOOR; break;
470 default: fprintf(stderr, "error: %s: bad type '%s'\n",
483 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
484 argv[0], argv[optind - 1]);
489 if (pathstart == -1) {
490 fprintf(stderr, "error: %s: no filename|pathname\n",
493 } else if (pathend == -1) {
501 static int deprecated_warning;
502 if (!deprecated_warning) {
503 fprintf(stderr, "lfs find: -q, -r, -v options "
504 "deprecated. Use 'lfs getstripe' instead.\n");
505 deprecated_warning = 1;
507 if (!param.recursive && param.maxdepth == -1)
513 ret = llapi_find(argv[pathstart], ¶m);
515 ret = llapi_getstripe(argv[pathstart], ¶m);
516 } while (++pathstart < pathend && !ret);
519 fprintf(stderr, "error: %s failed for %s.\n",
520 argv[0], argv[optind - 1]);
524 static int lfs_getstripe(int argc, char **argv)
526 struct option long_opts[] = {
528 {"quiet", 0, 0, 'q'},
529 {"recursive", 0, 0, 'r'},
530 {"verbose", 0, 0, 'v'},
533 char short_opts[] = "hO:qrv";
535 struct find_param param = { 0 };
538 while ((c = getopt_long(argc, argv, short_opts,
539 long_opts, NULL)) != -1) {
544 "error: %s: only one obduuid allowed",
548 param.obduuid = (struct obd_uuid *)optarg;
564 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
565 argv[0], argv[optind - 1]);
573 param.maxdepth = param.recursive ? -1 : 1;
576 rc = llapi_getstripe(argv[optind], ¶m);
577 } while (++optind < argc && !rc);
580 fprintf(stderr, "error: %s failed for %s.\n",
581 argv[0], argv[optind - 1]);
585 static int lfs_osts(int argc, char **argv)
588 struct mntent *mnt = NULL;
589 struct obd_uuid *obduuid = NULL;
590 struct find_param param;
596 fp = setmntent(MOUNTED, "r");
599 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
603 memset(¶m, 0, sizeof(param));
604 param.obduuid = obduuid;
605 while (feof(fp) == 0 && ferror(fp) ==0) {
606 if (llapi_is_lustre_mnt(mnt)) {
607 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
610 "error: %s: failed on %s\n",
611 argv[0], mnt->mnt_dir);
621 #define COOK(value) \
624 while (value > 1024) { \
638 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
640 char rpath[PATH_MAX] = {'\0'};
642 int rc, len, out_len = 0;
644 if (!realpath(path, rpath)) {
646 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
647 path, strerror(-rc));
653 while (feof(fp) == 0 && ferror(fp) == 0) {
654 if (llapi_is_lustre_mnt(mnt)) {
655 len = strlen(mnt->mnt_dir);
657 !strncmp(rpath, mnt->mnt_dir, len)) {
659 memset(mntdir, 0, dir_len);
660 strncpy(mntdir, mnt->mnt_dir, dir_len);
669 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
673 static int showdf(char *mntdir, struct obd_statfs *stat,
674 char *uuid, int ishow, int cooked,
675 char *type, int index, int rc)
677 long long avail, used, total;
679 char *suffix = "KMGTPEZY";
680 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
688 avail = stat->os_ffree;
689 used = stat->os_files - stat->os_ffree;
690 total = stat->os_files;
692 int shift = cooked ? 0 : 10;
694 avail = (stat->os_bavail * stat->os_bsize) >> shift;
695 used = stat->os_blocks - stat->os_bavail;
696 used = (used * stat->os_bsize) >> shift;
697 total = (stat->os_blocks * stat->os_bsize) >> shift;
701 ratio = (double)used / (double)total;
707 cook_val = (double)total;
710 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
712 sprintf(tbuf, CDF, total);
714 cook_val = (double)used;
717 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
719 sprintf(ubuf, CDF, used);
721 cook_val = (double)avail;
724 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
726 sprintf(abuf, CDF, avail);
728 sprintf(tbuf, CDF, total);
729 sprintf(ubuf, CDF, used);
730 sprintf(abuf, CDF, avail);
733 sprintf(rbuf, RDF, (int)(ratio * 100));
734 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
735 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
737 printf("[%s:%d]\n", type, index);
743 printf(UUF": inactive device\n", uuid);
746 printf(UUF": %s\n", uuid, strerror(-rc));
753 static int mntdf(char *mntdir, int ishow, int cooked)
755 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
756 struct obd_uuid uuid_buf;
761 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
762 "UUID", "Inodes", "IUsed", "IFree",
763 "IUse%", "Mounted on");
765 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
766 "UUID", cooked ? "bytes" : "1K-blocks",
767 "Used", "Available", "Use%", "Mounted on");
769 for (index = 0; ; index++) {
770 memset(&stat_buf, 0, sizeof(struct obd_statfs));
771 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
772 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
773 &stat_buf, &uuid_buf);
780 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
781 rc == -ENODATA || rc == 0) {
782 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
786 "error: llapi_obd_statfs(%s): %s (%d)\n",
787 uuid_buf.uuid, strerror(-rc), rc);
791 sum.os_ffree += stat_buf.os_ffree;
792 sum.os_files += stat_buf.os_files;
796 for (index = 0; ; index++) {
797 memset(&stat_buf, 0, sizeof(struct obd_statfs));
798 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
799 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
800 &stat_buf, &uuid_buf);
807 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
808 rc == -ENODATA || rc == 0) {
809 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
813 "error: llapi_obd_statfs failed: %s (%d)\n",
818 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
819 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
820 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
825 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
830 static int lfs_df(int argc, char **argv)
834 struct mntent *mnt = NULL;
835 char mntdir[PATH_MAX] = {'\0'};
836 int ishow = 0, cooked = 0;
840 while ((c = getopt(argc, argv, "ih")) != -1) {
855 fp = setmntent(MOUNTED, "r");
858 fprintf(stderr, "error: %s: open %s failed( %s )\n",
859 argv[0], MOUNTED, strerror(errno));
863 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
869 rc = mntdf(mntdir, ishow, cooked);
874 while (feof(fp) == 0 && ferror(fp) == 0) {
875 if (llapi_is_lustre_mnt(mnt)) {
876 rc = mntdf(mnt->mnt_dir, ishow, cooked);
889 static int lfs_check(int argc, char **argv)
893 struct mntent *mnt = NULL;
902 obd_types[0] = obd_type1;
903 obd_types[1] = obd_type2;
905 if (strcmp(argv[1], "osts") == 0) {
906 strcpy(obd_types[0], "osc");
907 } else if (strcmp(argv[1], "mds") == 0) {
908 strcpy(obd_types[0], "mdc");
909 } else if (strcmp(argv[1], "servers") == 0) {
911 strcpy(obd_types[0], "osc");
912 strcpy(obd_types[1], "mdc");
914 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
919 fp = setmntent(MOUNTED, "r");
921 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
925 while (feof(fp) == 0 && ferror(fp) ==0) {
926 if (llapi_is_lustre_mnt(mnt))
934 fprintf(stderr, "No suitable Lustre mount found\n");
938 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
941 fprintf(stderr, "error: %s: %s status failed\n",
948 static int lfs_catinfo(int argc, char **argv)
951 struct mntent *mnt = NULL;
954 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
957 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
960 fp = setmntent(MOUNTED, "r");
962 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
966 while (feof(fp) == 0 && ferror(fp) == 0) {
967 if (llapi_is_lustre_mnt(mnt))
976 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
978 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
980 fprintf(stderr, "no lustre_lite mounted.\n");
987 int lfs_join(int argc, char **argv)
989 char *name_head, *name_tail;
996 fd = open(name_head, O_WRONLY);
998 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1002 size = lseek(fd, 0, SEEK_END);
1003 if (size % JOIN_FILE_ALIGN) {
1004 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1005 name_head, (long long)size, JOIN_FILE_ALIGN);
1009 name_tail = argv[2];
1010 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1014 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1020 #ifdef HAVE_QUOTA_SUPPORT
1021 static int lfs_quotachown(int argc, char **argv)
1027 while ((c = getopt(argc, argv, "i")) != -1) {
1033 fprintf(stderr, "error: %s: option '-%c' "
1034 "unrecognized\n", argv[0], c);
1040 rc = llapi_quotachown(argv[optind], flag);
1042 fprintf(stderr,"error: change file owner/group failed.\n");
1047 static int lfs_quotacheck(int argc, char **argv)
1049 int c, check_type = 0;
1051 struct if_quotacheck qchk;
1052 struct if_quotactl qctl;
1053 char *obd_type = qchk.obd_type;
1054 char *obd_uuid = qchk.obd_uuid.uuid;
1057 memset(&qchk, 0, sizeof(qchk));
1060 while ((c = getopt(argc, argv, "ug")) != -1) {
1069 fprintf(stderr, "error: %s: option '-%c' "
1070 "unrecognized\n", argv[0], c);
1077 else /* do quotacheck for both user & group quota by default */
1085 memset(&qctl, 0, sizeof(qctl));
1086 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1087 qctl.qc_id = QFMT_LDISKFS;
1088 qctl.qc_type = check_type;
1089 rc = llapi_quotactl(mnt, &qctl);
1091 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1095 rc = llapi_quotacheck(mnt, check_type);
1097 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1101 rc = llapi_poll_quotacheck(mnt, &qchk);
1104 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1105 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1109 memset(&qctl, 0, sizeof(qctl));
1110 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1111 qctl.qc_id = QFMT_LDISKFS;
1112 qctl.qc_type = check_type;
1113 rc = llapi_quotactl(mnt, &qctl);
1116 fprintf(stderr, "%s %s ",
1117 qctl.obd_type, qctl.obd_uuid.uuid);
1118 fprintf(stderr, "%s turn on quota failed: %s\n",
1119 argv[0], strerror(errno));
1126 static int lfs_quotaon(int argc, char **argv)
1130 struct if_quotactl qctl;
1131 char *obd_type = qctl.obd_type;
1132 char *obd_uuid = qctl.obd_uuid.uuid;
1135 memset(&qctl, 0, sizeof(qctl));
1136 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1137 qctl.qc_id = QFMT_LDISKFS;
1140 while ((c = getopt(argc, argv, "ugf")) != -1) {
1143 qctl.qc_type |= 0x01;
1146 qctl.qc_type |= 0x02;
1149 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1152 fprintf(stderr, "error: %s: option '-%c' "
1153 "unrecognized\n", argv[0], c);
1160 else /* by default, enable quota for both user & group */
1161 qctl.qc_type = 0x02;
1168 rc = llapi_quotactl(mnt, &qctl);
1171 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1172 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1179 static int lfs_quotaoff(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_QUOTAOFF;
1192 while ((c = getopt(argc, argv, "ug")) != -1) {
1195 qctl.qc_type |= 0x01;
1198 qctl.qc_type |= 0x02;
1201 fprintf(stderr, "error: %s: option '-%c' "
1202 "unrecognized\n", argv[0], c);
1209 else /* by default, disable quota for both user & group */
1210 qctl.qc_type = 0x02;
1217 rc = llapi_quotactl(mnt, &qctl);
1220 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1221 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1228 static int name2id(unsigned int *id, char *name, int type)
1230 if (type == USRQUOTA) {
1231 struct passwd *entry;
1233 if (!(entry = getpwnam(name))) {
1239 *id = entry->pw_uid;
1241 struct group *entry;
1243 if (!(entry = getgrnam(name))) {
1249 *id = entry->gr_gid;
1255 static int id2name(char **name, unsigned int id, int type)
1257 if (type == USRQUOTA) {
1258 struct passwd *entry;
1260 if (!(entry = getpwuid(id))) {
1266 *name = entry->pw_name;
1268 struct group *entry;
1270 if (!(entry = getgrgid(id))) {
1276 *name = entry->gr_name;
1282 #define ARG2INT(nr, str, msg) \
1285 nr = strtol(str, &endp, 0); \
1287 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1292 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1294 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1295 * returns the value or ULONG_MAX on integer overflow or incorrect format
1297 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1298 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1299 * 3. empty integer value is interpreted as 0
1302 static unsigned long str2sec(const char* timestr) {
1303 const char spec[] = "smhdw";
1304 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1305 unsigned long val = 0;
1308 if (strpbrk(timestr, spec) == NULL) {
1309 /* no specifiers inside the time string,
1310 should treat it as an integer value */
1311 val = strtoul(timestr, &tail, 10);
1312 return *tail ? ULONG_MAX : val;
1315 /* format string is XXwXXdXXhXXmXXs */
1321 v = strtoul(timestr, &tail, 10);
1322 if (v == ULONG_MAX || *tail == '\0')
1323 /* value too large (ULONG_MAX or more)
1324 or missing specifier */
1327 ptr = strchr(spec, *tail);
1329 /* unknown specifier */
1334 /* check if product will overflow the type */
1335 if (!(v < ULONG_MAX / mult[ind]))
1338 ADD_OVERFLOW(val, mult[ind] * v);
1339 if (val == ULONG_MAX)
1351 int lfs_setquota(int argc, char **argv)
1355 struct if_quotactl qctl;
1356 char *obd_type = qctl.obd_type;
1357 char *obd_uuid = qctl.obd_uuid.uuid;
1360 memset(&qctl, 0, sizeof(qctl));
1361 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1364 while ((c = getopt(argc, argv, "ugt")) != -1) {
1367 qctl.qc_type |= 0x01;
1370 qctl.qc_type |= 0x02;
1373 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1376 fprintf(stderr, "error: %s: option '-%c' "
1377 "unrecognized\n", argv[0], c);
1385 if (qctl.qc_type == UGQUOTA) {
1386 fprintf(stderr, "error: user and group quotas can't be set "
1391 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1392 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1394 if (optind + 6 != argc)
1397 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1399 fprintf(stderr, "error: find id for name %s failed: %s\n",
1400 argv[optind - 1], strerror(errno));
1404 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1405 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1406 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1407 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1409 dqb->dqb_valid = QIF_LIMITS;
1411 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1413 if (optind + 3 != argc)
1416 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1417 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1420 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1421 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1428 rc = llapi_quotactl(mnt, &qctl);
1431 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1432 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1439 static inline char *type2name(int check_type)
1441 if (check_type == USRQUOTA)
1443 else if (check_type == GRPQUOTA)
1450 /* Converts seconds value into format string
1451 * result is returned in buf
1453 * 1. result is in descenting order: 1w2d3h4m5s
1454 * 2. zero fields are not filled (except for p. 3): 5d1s
1455 * 3. zero seconds value is presented as "0s"
1457 static void sec2str(time_t seconds, char *buf)
1459 const char spec[] = "smhdw";
1460 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1465 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1466 c = seconds / mult[i];
1468 if (c > 0 || (i == 0 && buf == tail))
1469 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1476 static void diff2str(time_t seconds, char *buf, time_t now)
1482 if (seconds <= now) {
1483 strcpy(buf, "none");
1486 sec2str(seconds - now, buf);
1489 static void print_quota_title(char *name, struct if_quotactl *qctl)
1491 printf("Disk quotas for %s %s (%cid %u):\n",
1492 type2name(qctl->qc_type), name,
1493 *type2name(qctl->qc_type), qctl->qc_id);
1494 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1496 "kbytes", "quota", "limit", "grace",
1497 "files", "quota", "limit", "grace");
1500 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1506 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1507 int bover = 0, iover = 0;
1508 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1510 if (dqb->dqb_bhardlimit &&
1511 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1513 } else if (dqb->dqb_bsoftlimit &&
1514 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1515 if (dqb->dqb_btime > now) {
1522 if (dqb->dqb_ihardlimit &&
1523 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1525 } else if (dqb->dqb_isoftlimit &&
1526 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1527 if (dqb->dqb_btime > now) {
1534 #if 0 /* XXX: always print quotas even when no usages */
1535 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1541 if (strlen(mnt) > 15)
1542 printf("%s\n%15s", mnt, "");
1544 printf("%15s", mnt);
1547 diff2str(dqb->dqb_btime, timebuf, now);
1549 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1550 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1551 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1552 printf(" %7s%c %6s %7s %7s",
1553 numbuf[0], bover ? '*' : ' ', numbuf[1],
1554 numbuf[2], bover > 1 ? timebuf : "");
1557 diff2str(dqb->dqb_itime, timebuf, now);
1559 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1560 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1561 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1563 printf(" %7s%c %6s %7s %7s",
1564 numbuf[0], iover ? '*' : ' ', numbuf[1],
1565 numbuf[2], iover > 1 ? timebuf : "");
1568 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1569 qctl->qc_cmd == Q_GETOINFO) {
1573 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1574 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1575 printf("Block grace time: %s; Inode grace time: %s\n",
1576 bgtimebuf, igtimebuf);
1580 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1584 /* XXX: this is a flag to mark that only mds quota is wanted */
1585 qctl->qc_dqblk.dqb_valid = 1;
1586 rc = llapi_quotactl(mnt, qctl);
1588 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1591 qctl->qc_dqblk.dqb_valid = 0;
1593 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1596 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1599 struct obd_uuid uuids[1024], *uuidp;
1600 int obdcount = 1024;
1605 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1609 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1611 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1615 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1616 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1618 /* XXX clear this flag to get quota from osts */
1619 qctl->qc_dqblk.dqb_valid = 0;
1620 rc = llapi_quotactl(mnt, qctl);
1622 fprintf(stderr, "%s quotactl failed: %s\n",
1623 uuidp->uuid, strerror(errno));
1627 print_quota(uuidp->uuid, qctl, 1);
1635 static int lfs_quota(int argc, char **argv)
1638 char *name = NULL, *mnt;
1639 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1641 char *obd_type = qctl.obd_type;
1642 char *obd_uuid = qctl.obd_uuid.uuid;
1646 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1649 qctl.qc_type = 0x01;
1652 qctl.qc_type = 0x02;
1655 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1658 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1661 fprintf(stderr, "error: %s: option '-%c' "
1662 "unrecognized\n", argv[0], c);
1671 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1672 if (optind + 2 != argc) {
1673 fprintf(stderr, "error: missing quota argument(s)\n");
1677 name = argv[optind++];
1678 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1680 fprintf(stderr,"error: can't find id for name %s: %s\n",
1681 name, strerror(errno));
1684 print_quota_title(name, &qctl);
1685 } else if (optind + 1 != argc) {
1686 fprintf(stderr, "error: missing quota info argument(s)\n");
1692 rc = llapi_quotactl(mnt, &qctl);
1695 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1696 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1701 rc = id2name(&name, getuid(), qctl.qc_type);
1708 print_quota(mnt, &qctl, 0);
1710 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1711 print_mds_quota(mnt, &qctl);
1712 print_lov_quota(mnt, &qctl);
1717 #endif /* HAVE_QUOTA_SUPPORT */
1719 static int flushctx_ioctl(char *mp)
1723 fd = open(mp, O_RDONLY);
1725 fprintf(stderr, "flushctx: error open %s: %s\n",
1726 mp, strerror(errno));
1730 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1732 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1733 mp, strerror(errno));
1739 static int lfs_flushctx(int argc, char **argv)
1741 int kdestroy = 0, c;
1743 char procline[PATH_MAX], *line;
1747 while ((c = getopt(argc, argv, "k")) != -1) {
1753 fprintf(stderr, "error: %s: option '-%c' "
1754 "unrecognized\n", argv[0], c);
1760 system("kdestroy > /dev/null");
1762 if (optind >= argc) {
1763 /* flush for all mounted lustre fs. */
1764 proc = fopen("/proc/mounts", "r");
1766 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1771 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1776 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1777 fprintf(stderr, "%s: unexpected format in "
1783 if (strcmp(fs, "lustre") != 0)
1785 /* we use '@' to determine it's a client. are there
1786 * any other better way?
1788 if (strchr(dev, '@') == NULL)
1791 if (flushctx_ioctl(mp))
1795 /* flush fs as specified */
1796 while (optind < argc) {
1797 if (flushctx_ioctl(argv[optind++]))
1806 * We assume one and only one filename is supplied as the
1809 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1811 char *dname, *rpath = NULL;
1812 char path[PATH_MAX], cwd[PATH_MAX];
1820 /* FIXME the premise is there is no sub-mounted filesystems under this
1821 * mounted lustre tree. */
1822 strncpy(fname, argv[argc - 1], PATH_MAX);
1824 /* get path prefix */
1825 dname = dirname(fname);
1827 /* try to resolve the pathname into relative to the root of the mounted
1828 * lustre filesystem.
1830 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1831 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1835 if (chdir(dname) == -1) {
1836 fprintf(stderr, "chdir to %s failed: %s\n",
1837 dname, strerror(errno));
1841 if (getcwd(path, sizeof(path)) == NULL) {
1842 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1846 if (chdir(cwd) == -1) {
1847 fprintf(stderr, "chdir back to %s: %s\n",
1848 cwd, strerror(errno));
1852 strncat(path, "/", PATH_MAX);
1853 strncpy(fname, argv[argc - 1], PATH_MAX);
1854 strncat(path, basename(fname), PATH_MAX);
1856 fp = setmntent(MOUNTED, "r");
1858 fprintf(stderr, "setmntent %s failed: %s\n",
1859 MOUNTED, strerror(errno));
1864 mnt = getmntent(fp);
1868 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
1871 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1872 rpath = path + strlen(mnt->mnt_dir);
1878 /* remove char '/' from rpath to be a relative path */
1879 while (rpath && *rpath == '/') rpath++;
1883 "%s: file %s doesn't belong to a lustre file system!\n",
1884 argv[0], argv[argc - 1]);
1888 for (i = 0; i < argc - 1; i++) {
1889 strncat(cmd, argv[i], PATH_MAX);
1890 strncat(cmd, " ", PATH_MAX);
1892 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1893 strncpy(fname, argv[argc - 1], sizeof(fname));
1898 static int lfs_getfacl(int argc, char **argv)
1900 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1902 if (acl_cmd_parse(argc, argv, fname, cmd))
1905 return llapi_getfacl(fname, cmd);
1908 static int lfs_setfacl(int argc, char **argv)
1910 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1912 if (acl_cmd_parse(argc, argv, fname, cmd))
1915 return llapi_setfacl(fname, cmd);
1918 int main(int argc, char **argv)
1924 ptl_initialize(argc, argv);
1925 if (obd_initialize(argc, argv) < 0)
1927 if (dbg_initialize(argc, argv) < 0)
1930 Parser_init("lfs > ", cmdlist);
1933 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1935 rc = Parser_commands();
1938 obd_finalize(argc, argv);