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] [--maxdepth|-D N]\n"
103 " [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
104 "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
105 "\t !: used before --name means find exclude the regular expression pattern\n"
106 "If one of the options below is provided, find works the same as 'getstripe':\n"
107 "To list the striping info for a given filename or files in a directory or\n"
109 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
110 " [--recursive | -r] <dir|file> ..."},
111 {"check", lfs_check, 0,
112 "Display the status of MDS or OSTs (as specified in the command)\n"
113 "or all the servers (MDS and OSTs).\n"
114 "usage: check <osts|mds|servers>"},
115 {"catinfo", lfs_catinfo, 0,
116 "Show information of specified type logs.\n"
117 "usage: catinfo {keyword} [node name]\n"
118 "\tkeywords are one of followings: config, deletions.\n"
119 "\tnode name must be provided when use keyword config."},
120 {"join", lfs_join, 0,
121 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
122 "usage: join <filename_A> <filename_B>\n"},
123 {"osts", lfs_osts, 0, "osts"},
125 "report filesystem disk space usage or inodes usage"
127 "Usage: df [-i] [-h] [path]"},
128 #ifdef HAVE_QUOTA_SUPPORT
129 {"quotachown",lfs_quotachown, 0,
130 "Change files' owner or group on the specified filesystem.\n"
131 "usage: quotachown [-i] <filesystem>\n"
132 "\t-i: ignore error if file is not exist\n"},
133 {"quotacheck", lfs_quotacheck, 0,
134 "Scan the specified filesystem for disk usage, and create,\n"
135 "or update quota files.\n"
136 "usage: quotacheck [ -ug ] <filesystem>"},
137 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
138 "usage: quotaon [ -ugf ] <filesystem>"},
139 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
140 "usage: quotaoff [ -ug ] <filesystem>"},
141 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
142 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
143 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
144 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
145 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
147 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
148 "usage: flushctx [-k] [mountpoint...]"},
149 {"getfacl", lfs_getfacl, 0,
150 "Get file access control list in remote client.\n"
151 "usage: getfacl [-dRLPvh] file"},
152 {"setfacl", lfs_setfacl, 0,
153 "Set file access control list in remote client.\n"
154 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
155 {"help", Parser_help, 0, "help"},
156 {"exit", Parser_quit, 0, "quit"},
157 {"quit", Parser_quit, 0, "quit"},
162 static int lfs_setstripe(int argc, char **argv)
166 unsigned long st_size;
167 int st_offset, st_count;
171 char *stripe_size_arg = NULL;
172 char *stripe_off_arg = NULL;
173 char *stripe_count_arg = NULL;
175 struct option long_opts[] = {
176 {"size", required_argument, 0, 's'},
177 {"count", required_argument, 0, 'c'},
178 {"index", required_argument, 0, 'i'},
179 {"delete", no_argument, 0, 'd'},
186 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
187 /* for compatibility with the existing positional parameter
191 } else if (argc == 5 &&
192 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
193 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
194 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
195 /* for compatibility with the existing positional parameter
198 stripe_size_arg = argv[2];
199 stripe_off_arg = argv[3];
200 stripe_count_arg = argv[4];
203 while ((c = getopt_long(argc, argv, "c:di:s:",
204 long_opts, NULL)) >= 0)
211 stripe_count_arg = optarg;
214 /* delete the default striping pattern */
218 stripe_off_arg = optarg;
221 stripe_size_arg = optarg;
226 fprintf(stderr, "error: %s: option '%s' "
228 argv[0], argv[optind - 1]);
233 fname = argv[optind];
239 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
240 stripe_count_arg != NULL)) {
241 fprintf(stderr, "error: %s: cannot specify -d with "
242 "-s, -c or -i options\n",
247 if (optind != argc - 1) {
248 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
250 argv[0], argv[argc - 1]);
254 /* get the stripe size */
255 if (stripe_size_arg != NULL) {
256 st_size = strtoul(stripe_size_arg, &end, 0);
258 if ((*end == 'k' || *end == 'K') &&
260 (st_size & (~0UL << (32 - 10))) == 0) {
262 } else if ((*end == 'm' || *end == 'M') &&
264 (st_size & (~0UL << (32 - 20))) == 0) {
266 } else if ((*end == 'g' || *end == 'G') &&
268 (st_size & (~0UL << (32 - 30))) == 0) {
271 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
272 argv[0], stripe_size_arg);
277 /* get the stripe offset */
278 if (stripe_off_arg != NULL) {
279 st_offset = strtoul(stripe_off_arg, &end, 0);
281 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
282 argv[0], stripe_off_arg);
286 /* get the stripe count */
287 if (stripe_count_arg != NULL) {
288 st_count = strtoul(stripe_count_arg, &end, 0);
290 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
291 argv[0], stripe_count_arg);
296 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
298 fprintf(stderr, "error: %s: create stripe file failed\n",
304 static int set_time(time_t *time, time_t *set, char *str)
311 else if (str[0] == '-')
317 t = strtol(str, NULL, 0);
318 if (*time < t * 24 * 60 * 60) {
321 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
325 *set = *time - t * 24 * 60 * 60;
329 static int lfs_find(int argc, char **argv)
336 int quiet, verbose, recursive;
337 struct find_param param;
338 struct obd_uuid *obduuid = NULL;
340 struct option long_opts[] = {
341 /* New find options. */
342 {"atime", required_argument, 0, 'A'},
343 {"ctime", required_argument, 0, 'C'},
344 {"mtime", required_argument, 0, 'M'},
345 {"maxdepth", required_argument, 0, 'D'},
346 {"name", required_argument, 0, 'n'},
347 /* --obd is considered as a new option. */
348 {"obd", required_argument, 0, 'O'},
349 {"ost", required_argument, 0, 'O'},
350 {"print", no_argument, 0, 'P'},
351 {"print0", no_argument, 0, 'p'},
352 /* Old find options. */
353 {"quiet", no_argument, 0, 'q'},
354 {"recursive", no_argument, 0, 'r'},
355 {"verbose", no_argument, 0, 'v'},
368 quiet = verbose = recursive = 0;
370 memset(¶m, 0, sizeof(param));
372 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrv",
373 long_opts, NULL)) >= 0)
379 /* '!' is part of option */
380 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
381 if (!isoption && pathend != -1) {
382 fprintf(stderr, "err: %s: filename|dirname must either "
383 "precede options or follow options\n",
387 if (!isoption && pathstart == -1)
388 pathstart = optind - 1;
389 if (isoption && pathstart != -1 && pathend == -1) {
390 pathend = optind - 2;
391 if ((c == 1 && strcmp(optarg, "!") == 0) ||
392 c == 'P' || c == 'p' || c == 'O' ||
393 c == 'q' || c == 'r' || c == 'v')
394 pathend = optind - 1;
401 if (strcmp(optarg, "!") == 0)
405 xtime = ¶m.atime;
406 xsign = ¶m.asign;
409 xtime = ¶m.ctime;
410 xsign = ¶m.csign;
414 xtime = ¶m.mtime;
415 xsign = ¶m.msign;
419 if (optarg[0] == '-')
421 else if (optarg[0] == '+')
426 strcat(timestr, optarg);
430 ret = set_time(&t, xtime, optarg);
437 depth = strtol(optarg, 0, 0);
441 param.pattern = (char *)optarg;
443 param.exclude_pattern = 1;
445 param.exclude_pattern = 0;
450 "error: %s: only one obduuid allowed",
454 obduuid = (struct obd_uuid *)optarg;
478 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
479 argv[0], argv[optind - 1]);
484 if (pathstart == -1) {
485 fprintf(stderr, "error: %s: no filename|pathname\n",
488 } else if (pathend == -1) {
493 param.obduuid = obduuid;
495 param.maxdepth = depth;
496 param.zeroend = zeroend;
499 param.recursive = recursive;
500 param.verbose = verbose;
502 param.maxdepth = recursive ? -1 : 1;
507 ret = llapi_find(argv[pathstart], ¶m);
509 ret = llapi_getstripe(argv[pathstart], ¶m);
510 } while (++pathstart < pathend && !ret);
513 fprintf(stderr, "error: %s failed for %s.\n",
514 argv[0], argv[optind - 1]);
518 static int lfs_getstripe(int argc, char **argv)
520 struct option long_opts[] = {
522 {"quiet", 0, 0, 'q'},
523 {"recursive", 0, 0, 'r'},
524 {"verbose", 0, 0, 'v'},
527 char short_opts[] = "hO:qrv";
528 int quiet, verbose, recursive, c, rc;
529 struct obd_uuid *obduuid = NULL;
530 struct find_param param;
533 quiet = verbose = recursive = 0;
534 while ((c = getopt_long(argc, argv, short_opts,
535 long_opts, NULL)) != -1) {
540 "error: %s: only one obduuid allowed",
544 obduuid = (struct obd_uuid *)optarg;
560 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
561 argv[0], argv[optind - 1]);
569 memset(¶m, 0, sizeof(param));
570 param.recursive = recursive;
571 param.verbose = verbose;
573 param.obduuid = obduuid;
574 param.maxdepth = recursive ? -1 : 1;
577 rc = llapi_getstripe(argv[optind], ¶m);
578 } while (++optind < argc && !rc);
581 fprintf(stderr, "error: %s failed for %s.\n",
582 argv[0], argv[optind - 1]);
586 static int lfs_osts(int argc, char **argv)
589 struct mntent *mnt = NULL;
590 struct obd_uuid *obduuid = NULL;
591 struct find_param param;
597 fp = setmntent(MOUNTED, "r");
600 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
604 memset(¶m, 0, sizeof(param));
605 param.obduuid = obduuid;
606 while (feof(fp) == 0 && ferror(fp) ==0) {
607 if (llapi_is_lustre_mnt(mnt)) {
608 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
611 "error: %s: failed on %s\n",
612 argv[0], mnt->mnt_dir);
622 #define COOK(value) \
625 while (value > 1024) { \
639 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
641 char rpath[PATH_MAX] = {'\0'};
643 int rc, len, out_len = 0;
645 if (!realpath(path, rpath)) {
647 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
648 path, strerror(-rc));
654 while (feof(fp) == 0 && ferror(fp) == 0) {
655 if (llapi_is_lustre_mnt(mnt)) {
656 len = strlen(mnt->mnt_dir);
658 !strncmp(rpath, mnt->mnt_dir, len)) {
660 memset(mntdir, 0, dir_len);
661 strncpy(mntdir, mnt->mnt_dir, dir_len);
670 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
674 static int showdf(char *mntdir, struct obd_statfs *stat,
675 char *uuid, int ishow, int cooked,
676 char *type, int index, int rc)
678 long long avail, used, total;
680 char *suffix = "KMGTPEZY";
681 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
689 avail = stat->os_ffree;
690 used = stat->os_files - stat->os_ffree;
691 total = stat->os_files;
693 int shift = cooked ? 0 : 10;
695 avail = (stat->os_bavail * stat->os_bsize) >> shift;
696 used = stat->os_blocks - stat->os_bavail;
697 used = (used * stat->os_bsize) >> shift;
698 total = (stat->os_blocks * stat->os_bsize) >> shift;
702 ratio = (double)used / (double)total;
708 cook_val = (double)total;
711 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
713 sprintf(tbuf, CDF, total);
715 cook_val = (double)used;
718 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
720 sprintf(ubuf, CDF, used);
722 cook_val = (double)avail;
725 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
727 sprintf(abuf, CDF, avail);
729 sprintf(tbuf, CDF, total);
730 sprintf(ubuf, CDF, used);
731 sprintf(abuf, CDF, avail);
734 sprintf(rbuf, RDF, (int)(ratio * 100));
735 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
736 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
738 printf("[%s:%d]\n", type, index);
744 printf(UUF": inactive device\n", uuid);
747 printf(UUF": %s\n", uuid, strerror(-rc));
754 static int mntdf(char *mntdir, int ishow, int cooked)
756 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
757 struct obd_uuid uuid_buf;
762 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
763 "UUID", "Inodes", "IUsed", "IFree",
764 "IUse%", "Mounted on");
766 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
767 "UUID", cooked ? "bytes" : "1K-blocks",
768 "Used", "Available", "Use%", "Mounted on");
770 for (index = 0; ; index++) {
771 memset(&stat_buf, 0, sizeof(struct obd_statfs));
772 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
773 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
774 &stat_buf, &uuid_buf);
781 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
782 rc == -ENODATA || rc == 0) {
783 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
787 "error: llapi_obd_statfs(%s): %s (%d)\n",
788 uuid_buf.uuid, strerror(-rc), rc);
792 sum.os_ffree += stat_buf.os_ffree;
793 sum.os_files += stat_buf.os_files;
797 for (index = 0; ; index++) {
798 memset(&stat_buf, 0, sizeof(struct obd_statfs));
799 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
800 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
801 &stat_buf, &uuid_buf);
808 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
809 rc == -ENODATA || rc == 0) {
810 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
814 "error: llapi_obd_statfs failed: %s (%d)\n",
819 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
820 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
821 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
826 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
831 static int lfs_df(int argc, char **argv)
835 struct mntent *mnt = NULL;
836 char mntdir[PATH_MAX] = {'\0'};
837 int ishow = 0, cooked = 0;
841 while ((c = getopt(argc, argv, "ih")) != -1) {
856 fp = setmntent(MOUNTED, "r");
859 fprintf(stderr, "error: %s: open %s failed( %s )\n",
860 argv[0], MOUNTED, strerror(errno));
864 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
870 rc = mntdf(mntdir, ishow, cooked);
875 while (feof(fp) == 0 && ferror(fp) == 0) {
876 if (llapi_is_lustre_mnt(mnt)) {
877 rc = mntdf(mnt->mnt_dir, ishow, cooked);
890 static int lfs_check(int argc, char **argv)
894 struct mntent *mnt = NULL;
903 obd_types[0] = obd_type1;
904 obd_types[1] = obd_type2;
906 if (strcmp(argv[1], "osts") == 0) {
907 strcpy(obd_types[0], "osc");
908 } else if (strcmp(argv[1], "mds") == 0) {
909 strcpy(obd_types[0], "mdc");
910 } else if (strcmp(argv[1], "servers") == 0) {
912 strcpy(obd_types[0], "osc");
913 strcpy(obd_types[1], "mdc");
915 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
920 fp = setmntent(MOUNTED, "r");
922 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
926 while (feof(fp) == 0 && ferror(fp) ==0) {
927 if (llapi_is_lustre_mnt(mnt))
935 fprintf(stderr, "No suitable Lustre mount found\n");
939 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
942 fprintf(stderr, "error: %s: %s status failed\n",
949 static int lfs_catinfo(int argc, char **argv)
952 struct mntent *mnt = NULL;
955 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
958 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
961 fp = setmntent(MOUNTED, "r");
963 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
967 while (feof(fp) == 0 && ferror(fp) == 0) {
968 if (llapi_is_lustre_mnt(mnt))
977 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
979 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
981 fprintf(stderr, "no lustre_lite mounted.\n");
988 int lfs_join(int argc, char **argv)
990 char *name_head, *name_tail;
997 fd = open(name_head, O_WRONLY);
999 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1003 size = lseek(fd, 0, SEEK_END);
1004 if (size % JOIN_FILE_ALIGN) {
1005 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1006 name_head, (long long)size, JOIN_FILE_ALIGN);
1010 name_tail = argv[2];
1011 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1015 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1021 #ifdef HAVE_QUOTA_SUPPORT
1022 static int lfs_quotachown(int argc, char **argv)
1028 while ((c = getopt(argc, argv, "i")) != -1) {
1034 fprintf(stderr, "error: %s: option '-%c' "
1035 "unrecognized\n", argv[0], c);
1041 rc = llapi_quotachown(argv[optind], flag);
1043 fprintf(stderr,"error: change file owner/group failed.\n");
1048 static int lfs_quotacheck(int argc, char **argv)
1050 int c, check_type = 0;
1052 struct if_quotacheck qchk;
1053 struct if_quotactl qctl;
1054 char *obd_type = qchk.obd_type;
1055 char *obd_uuid = qchk.obd_uuid.uuid;
1058 memset(&qchk, 0, sizeof(qchk));
1061 while ((c = getopt(argc, argv, "ug")) != -1) {
1070 fprintf(stderr, "error: %s: option '-%c' "
1071 "unrecognized\n", argv[0], c);
1078 else /* do quotacheck for both user & group quota by default */
1086 memset(&qctl, 0, sizeof(qctl));
1087 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1088 qctl.qc_id = QFMT_LDISKFS;
1089 qctl.qc_type = check_type;
1090 rc = llapi_quotactl(mnt, &qctl);
1092 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1096 rc = llapi_quotacheck(mnt, check_type);
1098 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1102 rc = llapi_poll_quotacheck(mnt, &qchk);
1105 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1106 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1110 memset(&qctl, 0, sizeof(qctl));
1111 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1112 qctl.qc_id = QFMT_LDISKFS;
1113 qctl.qc_type = check_type;
1114 rc = llapi_quotactl(mnt, &qctl);
1117 fprintf(stderr, "%s %s ",
1118 qctl.obd_type, qctl.obd_uuid.uuid);
1119 fprintf(stderr, "%s turn on quota failed: %s\n",
1120 argv[0], strerror(errno));
1127 static int lfs_quotaon(int argc, char **argv)
1131 struct if_quotactl qctl;
1132 char *obd_type = qctl.obd_type;
1133 char *obd_uuid = qctl.obd_uuid.uuid;
1136 memset(&qctl, 0, sizeof(qctl));
1137 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1138 qctl.qc_id = QFMT_LDISKFS;
1141 while ((c = getopt(argc, argv, "ugf")) != -1) {
1144 qctl.qc_type |= 0x01;
1147 qctl.qc_type |= 0x02;
1150 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1153 fprintf(stderr, "error: %s: option '-%c' "
1154 "unrecognized\n", argv[0], c);
1167 rc = llapi_quotactl(mnt, &qctl);
1170 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1171 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1178 static int lfs_quotaoff(int argc, char **argv)
1182 struct if_quotactl qctl;
1183 char *obd_type = qctl.obd_type;
1184 char *obd_uuid = qctl.obd_uuid.uuid;
1187 memset(&qctl, 0, sizeof(qctl));
1188 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1191 while ((c = getopt(argc, argv, "ug")) != -1) {
1194 qctl.qc_type |= 0x01;
1197 qctl.qc_type |= 0x02;
1200 fprintf(stderr, "error: %s: option '-%c' "
1201 "unrecognized\n", argv[0], c);
1214 rc = llapi_quotactl(mnt, &qctl);
1217 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1218 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1225 static int name2id(unsigned int *id, char *name, int type)
1227 if (type == USRQUOTA) {
1228 struct passwd *entry;
1230 if (!(entry = getpwnam(name))) {
1236 *id = entry->pw_uid;
1238 struct group *entry;
1240 if (!(entry = getgrnam(name))) {
1246 *id = entry->gr_gid;
1252 static int id2name(char **name, unsigned int id, int type)
1254 if (type == USRQUOTA) {
1255 struct passwd *entry;
1257 if (!(entry = getpwuid(id))) {
1263 *name = entry->pw_name;
1265 struct group *entry;
1267 if (!(entry = getgrgid(id))) {
1273 *name = entry->gr_name;
1279 #define ARG2INT(nr, str, msg) \
1282 nr = strtol(str, &endp, 0); \
1284 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1289 int lfs_setquota(int argc, char **argv)
1293 struct if_quotactl qctl;
1294 char *obd_type = qctl.obd_type;
1295 char *obd_uuid = qctl.obd_uuid.uuid;
1298 memset(&qctl, 0, sizeof(qctl));
1299 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1302 while ((c = getopt(argc, argv, "ugt")) != -1) {
1305 qctl.qc_type |= 0x01;
1308 qctl.qc_type |= 0x02;
1311 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1314 fprintf(stderr, "error: %s: option '-%c' "
1315 "unrecognized\n", argv[0], c);
1323 if (qctl.qc_type == UGQUOTA) {
1324 fprintf(stderr, "error: user and group quotas can't be set "
1329 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1330 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1332 if (optind + 6 != argc)
1335 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1337 fprintf(stderr, "error: find id for name %s failed: %s\n",
1338 argv[optind - 1], strerror(errno));
1342 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1343 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1344 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1345 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1347 dqb->dqb_valid = QIF_LIMITS;
1349 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1351 if (optind + 3 != argc)
1354 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1355 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1360 rc = llapi_quotactl(mnt, &qctl);
1363 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1364 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1371 static inline char *type2name(int check_type)
1373 if (check_type == USRQUOTA)
1375 else if (check_type == GRPQUOTA)
1382 static void grace2str(time_t seconds,char *buf)
1384 uint minutes, hours, days;
1386 minutes = (seconds + 30) / 60;
1387 hours = minutes / 60;
1392 snprintf(buf, 40, "%ddays", days);
1394 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1398 static void diff2str(time_t seconds, char *buf, time_t now)
1404 if (seconds <= now) {
1405 strcpy(buf, "none");
1408 grace2str(seconds - now, buf);
1411 static void print_quota_title(char *name, struct if_quotactl *qctl)
1413 printf("Disk quotas for %s %s (%cid %u):\n",
1414 type2name(qctl->qc_type), name,
1415 *type2name(qctl->qc_type), qctl->qc_id);
1416 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1418 "blocks", "quota", "limit", "grace",
1419 "files", "quota", "limit", "grace");
1422 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1428 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1429 int bover = 0, iover = 0;
1430 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1432 if (dqb->dqb_bhardlimit &&
1433 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1435 } else if (dqb->dqb_bsoftlimit &&
1436 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1437 if (dqb->dqb_btime > now) {
1444 if (dqb->dqb_ihardlimit &&
1445 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1447 } else if (dqb->dqb_isoftlimit &&
1448 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1449 if (dqb->dqb_btime > now) {
1456 #if 0 /* XXX: always print quotas even when no usages */
1457 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1463 if (strlen(mnt) > 15)
1464 printf("%s\n%15s", mnt, "");
1466 printf("%15s", mnt);
1469 diff2str(dqb->dqb_btime, timebuf, now);
1471 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1472 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1473 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1474 printf(" %7s%c %6s %7s %7s",
1475 numbuf[0], bover ? '*' : ' ', numbuf[1],
1476 numbuf[2], bover > 1 ? timebuf : "");
1479 diff2str(dqb->dqb_itime, timebuf, now);
1481 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1482 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1483 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1485 printf(" %7s%c %6s %7s %7s",
1486 numbuf[0], iover ? '*' : ' ', numbuf[1],
1487 numbuf[2], iover > 1 ? timebuf : "");
1490 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1491 qctl->qc_cmd == Q_GETOINFO) {
1495 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1496 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1497 printf("Block grace time: %s; Inode grace time: %s\n",
1498 bgtimebuf, igtimebuf);
1502 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1506 /* XXX: this is a flag to mark that only mds quota is wanted */
1507 qctl->qc_dqblk.dqb_valid = 1;
1508 rc = llapi_quotactl(mnt, qctl);
1510 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1513 qctl->qc_dqblk.dqb_valid = 0;
1515 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1518 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1521 struct obd_uuid uuids[1024], *uuidp;
1522 int obdcount = 1024;
1527 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1531 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1533 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1537 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1538 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1540 /* XXX clear this flag to get quota from osts */
1541 qctl->qc_dqblk.dqb_valid = 0;
1542 rc = llapi_quotactl(mnt, qctl);
1544 fprintf(stderr, "%s quotactl failed: %s\n",
1545 uuidp->uuid, strerror(errno));
1549 print_quota(uuidp->uuid, qctl, 1);
1557 static int lfs_quota(int argc, char **argv)
1560 char *name = NULL, *mnt;
1561 struct if_quotactl qctl;
1562 char *obd_type = qctl.obd_type;
1563 char *obd_uuid = qctl.obd_uuid.uuid;
1566 memset(&qctl, 0, sizeof(qctl));
1567 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1570 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1573 qctl.qc_type |= 0x01;
1576 qctl.qc_type |= 0x02;
1579 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1582 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1585 fprintf(stderr, "error: %s: option '-%c' "
1586 "unrecognized\n", argv[0], c);
1594 if (qctl.qc_type == UGQUOTA) {
1595 fprintf(stderr, "error: user or group can't be specified"
1600 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1601 if (optind + 2 != argc)
1604 name = argv[optind++];
1605 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1607 fprintf(stderr, "error: find id for name %s failed: %s\n",
1608 name, strerror(errno));
1611 print_quota_title(name, &qctl);
1612 } else if (optind + 1 != argc) {
1618 rc = llapi_quotactl(mnt, &qctl);
1621 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1622 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1627 rc = id2name(&name, getuid(), qctl.qc_type);
1634 print_quota(mnt, &qctl, 0);
1636 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1637 print_mds_quota(mnt, &qctl);
1638 print_lov_quota(mnt, &qctl);
1643 #endif /* HAVE_QUOTA_SUPPORT */
1645 static int flushctx_ioctl(char *mp)
1649 fd = open(mp, O_RDONLY);
1651 fprintf(stderr, "flushctx: error open %s: %s\n",
1652 mp, strerror(errno));
1656 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1658 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1659 mp, strerror(errno));
1665 static int lfs_flushctx(int argc, char **argv)
1667 int kdestroy = 0, c;
1669 char procline[PATH_MAX], *line;
1673 while ((c = getopt(argc, argv, "k")) != -1) {
1679 fprintf(stderr, "error: %s: option '-%c' "
1680 "unrecognized\n", argv[0], c);
1686 system("kdestroy > /dev/null");
1688 if (optind >= argc) {
1689 /* flush for all mounted lustre fs. */
1690 proc = fopen("/proc/mounts", "r");
1692 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1697 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1702 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1703 fprintf(stderr, "%s: unexpected format in "
1709 if (strcmp(fs, "lustre") != 0)
1711 /* we use '@' to determine it's a client. are there
1712 * any other better way?
1714 if (strchr(dev, '@') == NULL)
1717 if (flushctx_ioctl(mp))
1721 /* flush fs as specified */
1722 while (optind < argc) {
1723 if (flushctx_ioctl(argv[optind++]))
1732 * We assume one and only one filename is supplied as the
1735 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1737 char *dname, *rpath = NULL;
1738 char path[PATH_MAX], cwd[PATH_MAX];
1746 /* FIXME the premise is there is no sub-mounted filesystems under this
1747 * mounted lustre tree. */
1748 strncpy(fname, argv[argc - 1], PATH_MAX);
1750 /* get path prefix */
1751 dname = dirname(fname);
1753 /* try to resolve the pathname into relative to the root of the mounted
1754 * lustre filesystem.
1756 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1757 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1761 if (chdir(dname) == -1) {
1762 fprintf(stderr, "chdir to %s failed: %s\n",
1763 dname, strerror(errno));
1767 if (getcwd(path, sizeof(path)) == NULL) {
1768 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1772 if (chdir(cwd) == -1) {
1773 fprintf(stderr, "chdir back to %s: %s\n",
1774 cwd, strerror(errno));
1778 strncat(path, "/", PATH_MAX);
1779 strncpy(fname, argv[argc - 1], PATH_MAX);
1780 strncat(path, basename(fname), PATH_MAX);
1782 fp = setmntent(MOUNTED, "r");
1784 fprintf(stderr, "setmntent %s failed: %s\n",
1785 MOUNTED, strerror(errno));
1790 mnt = getmntent(fp);
1794 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
1797 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1798 rpath = path + strlen(mnt->mnt_dir);
1804 /* remove char '/' from rpath to be a relative path */
1805 while (rpath && *rpath == '/') rpath++;
1809 "%s: file %s doesn't belong to a lustre file system!\n",
1810 argv[0], argv[argc - 1]);
1814 for (i = 0; i < argc - 1; i++) {
1815 strncat(cmd, argv[i], PATH_MAX);
1816 strncat(cmd, " ", PATH_MAX);
1818 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1819 strncpy(fname, argv[argc - 1], sizeof(fname));
1824 static int lfs_getfacl(int argc, char **argv)
1826 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1828 if (acl_cmd_parse(argc, argv, fname, cmd))
1831 return llapi_getfacl(fname, cmd);
1834 static int lfs_setfacl(int argc, char **argv)
1836 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1838 if (acl_cmd_parse(argc, argv, fname, cmd))
1841 return llapi_setfacl(fname, cmd);
1844 int main(int argc, char **argv)
1850 ptl_initialize(argc, argv);
1851 if (obd_initialize(argc, argv) < 0)
1853 if (dbg_initialize(argc, argv) < 0)
1856 Parser_init("lfs > ", cmdlist);
1859 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1861 rc = Parser_commands();
1864 obd_finalize(argc, argv);