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>}|-t] <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)
334 struct find_param param = { .maxdepth = -1 };
336 struct option long_opts[] = {
337 /* New find options. */
338 {"atime", required_argument, 0, 'A'},
339 {"ctime", required_argument, 0, 'C'},
340 {"mtime", required_argument, 0, 'M'},
341 {"maxdepth", required_argument, 0, 'D'},
342 {"name", required_argument, 0, 'n'},
343 /* --obd is considered as a new option. */
344 {"obd", required_argument, 0, 'O'},
345 {"ost", required_argument, 0, 'O'},
346 {"print", no_argument, 0, 'P'},
347 {"print0", no_argument, 0, 'p'},
348 /* Old find options. */
349 {"quiet", no_argument, 0, 'q'},
350 {"recursive", no_argument, 0, 'r'},
351 {"verbose", no_argument, 0, 'v'},
363 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrv",
364 long_opts, NULL)) >= 0) {
369 /* '!' is part of option */
370 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
371 if (!isoption && pathend != -1) {
372 fprintf(stderr, "err: %s: filename|dirname must either "
373 "precede options or follow options\n",
377 if (!isoption && pathstart == -1)
378 pathstart = optind - 1;
379 if (isoption && pathstart != -1 && pathend == -1) {
380 pathend = optind - 2;
381 if ((c == 1 && strcmp(optarg, "!") == 0) ||
382 c == 'P' || c == 'p' || c == 'O' ||
383 c == 'q' || c == 'r' || c == 'v')
384 pathend = optind - 1;
391 if (strcmp(optarg, "!") == 0)
395 xtime = ¶m.atime;
396 xsign = ¶m.asign;
399 xtime = ¶m.ctime;
400 xsign = ¶m.csign;
404 xtime = ¶m.mtime;
405 xsign = ¶m.msign;
409 if (optarg[0] == '-')
411 else if (optarg[0] == '+')
416 strcat(timestr, optarg);
420 ret = set_time(&t, xtime, optarg);
427 param.maxdepth = strtol(optarg, 0, 0);
431 param.pattern = (char *)optarg;
433 param.exclude_pattern = 1;
435 param.exclude_pattern = 0;
440 "error: %s: only one obduuid allowed",
444 param.obduuid = (struct obd_uuid *)optarg;
468 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
469 argv[0], argv[optind - 1]);
474 if (pathstart == -1) {
475 fprintf(stderr, "error: %s: no filename|pathname\n",
478 } else if (pathend == -1) {
486 if (!param.recursive && param.maxdepth == -1)
492 ret = llapi_find(argv[pathstart], ¶m);
494 ret = llapi_getstripe(argv[pathstart], ¶m);
495 } while (++pathstart < pathend && !ret);
498 fprintf(stderr, "error: %s failed for %s.\n",
499 argv[0], argv[optind - 1]);
503 static int lfs_getstripe(int argc, char **argv)
505 struct option long_opts[] = {
507 {"quiet", 0, 0, 'q'},
508 {"recursive", 0, 0, 'r'},
509 {"verbose", 0, 0, 'v'},
512 char short_opts[] = "hO:qrv";
514 struct find_param param = { 0 };
517 while ((c = getopt_long(argc, argv, short_opts,
518 long_opts, NULL)) != -1) {
523 "error: %s: only one obduuid allowed",
527 param.obduuid = (struct obd_uuid *)optarg;
543 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
544 argv[0], argv[optind - 1]);
552 param.maxdepth = param.recursive ? -1 : 1;
555 rc = llapi_getstripe(argv[optind], ¶m);
556 } while (++optind < argc && !rc);
559 fprintf(stderr, "error: %s failed for %s.\n",
560 argv[0], argv[optind - 1]);
564 static int lfs_osts(int argc, char **argv)
567 struct mntent *mnt = NULL;
568 struct obd_uuid *obduuid = NULL;
569 struct find_param param;
575 fp = setmntent(MOUNTED, "r");
578 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
582 memset(¶m, 0, sizeof(param));
583 param.obduuid = obduuid;
584 while (feof(fp) == 0 && ferror(fp) ==0) {
585 if (llapi_is_lustre_mnt(mnt)) {
586 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
589 "error: %s: failed on %s\n",
590 argv[0], mnt->mnt_dir);
600 #define COOK(value) \
603 while (value > 1024) { \
617 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
619 char rpath[PATH_MAX] = {'\0'};
621 int rc, len, out_len = 0;
623 if (!realpath(path, rpath)) {
625 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
626 path, strerror(-rc));
632 while (feof(fp) == 0 && ferror(fp) == 0) {
633 if (llapi_is_lustre_mnt(mnt)) {
634 len = strlen(mnt->mnt_dir);
636 !strncmp(rpath, mnt->mnt_dir, len)) {
638 memset(mntdir, 0, dir_len);
639 strncpy(mntdir, mnt->mnt_dir, dir_len);
648 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
652 static int showdf(char *mntdir, struct obd_statfs *stat,
653 char *uuid, int ishow, int cooked,
654 char *type, int index, int rc)
656 long long avail, used, total;
658 char *suffix = "KMGTPEZY";
659 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
667 avail = stat->os_ffree;
668 used = stat->os_files - stat->os_ffree;
669 total = stat->os_files;
671 int shift = cooked ? 0 : 10;
673 avail = (stat->os_bavail * stat->os_bsize) >> shift;
674 used = stat->os_blocks - stat->os_bavail;
675 used = (used * stat->os_bsize) >> shift;
676 total = (stat->os_blocks * stat->os_bsize) >> shift;
680 ratio = (double)used / (double)total;
686 cook_val = (double)total;
689 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
691 sprintf(tbuf, CDF, total);
693 cook_val = (double)used;
696 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
698 sprintf(ubuf, CDF, used);
700 cook_val = (double)avail;
703 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
705 sprintf(abuf, CDF, avail);
707 sprintf(tbuf, CDF, total);
708 sprintf(ubuf, CDF, used);
709 sprintf(abuf, CDF, avail);
712 sprintf(rbuf, RDF, (int)(ratio * 100));
713 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
714 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
716 printf("[%s:%d]\n", type, index);
722 printf(UUF": inactive device\n", uuid);
725 printf(UUF": %s\n", uuid, strerror(-rc));
732 static int mntdf(char *mntdir, int ishow, int cooked)
734 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
735 struct obd_uuid uuid_buf;
740 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
741 "UUID", "Inodes", "IUsed", "IFree",
742 "IUse%", "Mounted on");
744 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
745 "UUID", cooked ? "bytes" : "1K-blocks",
746 "Used", "Available", "Use%", "Mounted on");
748 for (index = 0; ; index++) {
749 memset(&stat_buf, 0, sizeof(struct obd_statfs));
750 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
751 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
752 &stat_buf, &uuid_buf);
759 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
760 rc == -ENODATA || rc == 0) {
761 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
765 "error: llapi_obd_statfs(%s): %s (%d)\n",
766 uuid_buf.uuid, strerror(-rc), rc);
770 sum.os_ffree += stat_buf.os_ffree;
771 sum.os_files += stat_buf.os_files;
775 for (index = 0; ; index++) {
776 memset(&stat_buf, 0, sizeof(struct obd_statfs));
777 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
778 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
779 &stat_buf, &uuid_buf);
786 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
787 rc == -ENODATA || rc == 0) {
788 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
792 "error: llapi_obd_statfs failed: %s (%d)\n",
797 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
798 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
799 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
804 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
809 static int lfs_df(int argc, char **argv)
813 struct mntent *mnt = NULL;
814 char mntdir[PATH_MAX] = {'\0'};
815 int ishow = 0, cooked = 0;
819 while ((c = getopt(argc, argv, "ih")) != -1) {
834 fp = setmntent(MOUNTED, "r");
837 fprintf(stderr, "error: %s: open %s failed( %s )\n",
838 argv[0], MOUNTED, strerror(errno));
842 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
848 rc = mntdf(mntdir, ishow, cooked);
853 while (feof(fp) == 0 && ferror(fp) == 0) {
854 if (llapi_is_lustre_mnt(mnt)) {
855 rc = mntdf(mnt->mnt_dir, ishow, cooked);
868 static int lfs_check(int argc, char **argv)
872 struct mntent *mnt = NULL;
881 obd_types[0] = obd_type1;
882 obd_types[1] = obd_type2;
884 if (strcmp(argv[1], "osts") == 0) {
885 strcpy(obd_types[0], "osc");
886 } else if (strcmp(argv[1], "mds") == 0) {
887 strcpy(obd_types[0], "mdc");
888 } else if (strcmp(argv[1], "servers") == 0) {
890 strcpy(obd_types[0], "osc");
891 strcpy(obd_types[1], "mdc");
893 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
898 fp = setmntent(MOUNTED, "r");
900 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
904 while (feof(fp) == 0 && ferror(fp) ==0) {
905 if (llapi_is_lustre_mnt(mnt))
913 fprintf(stderr, "No suitable Lustre mount found\n");
917 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
920 fprintf(stderr, "error: %s: %s status failed\n",
927 static int lfs_catinfo(int argc, char **argv)
930 struct mntent *mnt = NULL;
933 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
936 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
939 fp = setmntent(MOUNTED, "r");
941 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
945 while (feof(fp) == 0 && ferror(fp) == 0) {
946 if (llapi_is_lustre_mnt(mnt))
955 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
957 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
959 fprintf(stderr, "no lustre_lite mounted.\n");
966 int lfs_join(int argc, char **argv)
968 char *name_head, *name_tail;
975 fd = open(name_head, O_WRONLY);
977 fprintf(stderr, "Can not open name_head %s rc=%d\n",
981 size = lseek(fd, 0, SEEK_END);
982 if (size % JOIN_FILE_ALIGN) {
983 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
984 name_head, (long long)size, JOIN_FILE_ALIGN);
989 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
993 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
999 #ifdef HAVE_QUOTA_SUPPORT
1000 static int lfs_quotachown(int argc, char **argv)
1006 while ((c = getopt(argc, argv, "i")) != -1) {
1012 fprintf(stderr, "error: %s: option '-%c' "
1013 "unrecognized\n", argv[0], c);
1019 rc = llapi_quotachown(argv[optind], flag);
1021 fprintf(stderr,"error: change file owner/group failed.\n");
1026 static int lfs_quotacheck(int argc, char **argv)
1028 int c, check_type = 0;
1030 struct if_quotacheck qchk;
1031 struct if_quotactl qctl;
1032 char *obd_type = qchk.obd_type;
1033 char *obd_uuid = qchk.obd_uuid.uuid;
1036 memset(&qchk, 0, sizeof(qchk));
1039 while ((c = getopt(argc, argv, "ug")) != -1) {
1048 fprintf(stderr, "error: %s: option '-%c' "
1049 "unrecognized\n", argv[0], c);
1056 else /* do quotacheck for both user & group quota by default */
1064 memset(&qctl, 0, sizeof(qctl));
1065 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1066 qctl.qc_id = QFMT_LDISKFS;
1067 qctl.qc_type = check_type;
1068 rc = llapi_quotactl(mnt, &qctl);
1070 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1074 rc = llapi_quotacheck(mnt, check_type);
1076 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1080 rc = llapi_poll_quotacheck(mnt, &qchk);
1083 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1084 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1088 memset(&qctl, 0, sizeof(qctl));
1089 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1090 qctl.qc_id = QFMT_LDISKFS;
1091 qctl.qc_type = check_type;
1092 rc = llapi_quotactl(mnt, &qctl);
1095 fprintf(stderr, "%s %s ",
1096 qctl.obd_type, qctl.obd_uuid.uuid);
1097 fprintf(stderr, "%s turn on quota failed: %s\n",
1098 argv[0], strerror(errno));
1105 static int lfs_quotaon(int argc, char **argv)
1109 struct if_quotactl qctl;
1110 char *obd_type = qctl.obd_type;
1111 char *obd_uuid = qctl.obd_uuid.uuid;
1114 memset(&qctl, 0, sizeof(qctl));
1115 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1116 qctl.qc_id = QFMT_LDISKFS;
1119 while ((c = getopt(argc, argv, "ugf")) != -1) {
1122 qctl.qc_type |= 0x01;
1125 qctl.qc_type |= 0x02;
1128 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1131 fprintf(stderr, "error: %s: option '-%c' "
1132 "unrecognized\n", argv[0], c);
1139 else /* by default, enable quota for both user & group */
1140 qctl.qc_type = 0x02;
1147 rc = llapi_quotactl(mnt, &qctl);
1150 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1151 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1158 static int lfs_quotaoff(int argc, char **argv)
1162 struct if_quotactl qctl;
1163 char *obd_type = qctl.obd_type;
1164 char *obd_uuid = qctl.obd_uuid.uuid;
1167 memset(&qctl, 0, sizeof(qctl));
1168 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1171 while ((c = getopt(argc, argv, "ug")) != -1) {
1174 qctl.qc_type |= 0x01;
1177 qctl.qc_type |= 0x02;
1180 fprintf(stderr, "error: %s: option '-%c' "
1181 "unrecognized\n", argv[0], c);
1188 else /* by default, disable quota for both user & group */
1189 qctl.qc_type = 0x02;
1196 rc = llapi_quotactl(mnt, &qctl);
1199 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1200 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1207 static int name2id(unsigned int *id, char *name, int type)
1209 if (type == USRQUOTA) {
1210 struct passwd *entry;
1212 if (!(entry = getpwnam(name))) {
1218 *id = entry->pw_uid;
1220 struct group *entry;
1222 if (!(entry = getgrnam(name))) {
1228 *id = entry->gr_gid;
1234 static int id2name(char **name, unsigned int id, int type)
1236 if (type == USRQUOTA) {
1237 struct passwd *entry;
1239 if (!(entry = getpwuid(id))) {
1245 *name = entry->pw_name;
1247 struct group *entry;
1249 if (!(entry = getgrgid(id))) {
1255 *name = entry->gr_name;
1261 #define ARG2INT(nr, str, msg) \
1264 nr = strtol(str, &endp, 0); \
1266 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1271 int lfs_setquota(int argc, char **argv)
1275 struct if_quotactl qctl;
1276 char *obd_type = qctl.obd_type;
1277 char *obd_uuid = qctl.obd_uuid.uuid;
1280 memset(&qctl, 0, sizeof(qctl));
1281 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1284 while ((c = getopt(argc, argv, "ugt")) != -1) {
1287 qctl.qc_type |= 0x01;
1290 qctl.qc_type |= 0x02;
1293 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1296 fprintf(stderr, "error: %s: option '-%c' "
1297 "unrecognized\n", argv[0], c);
1305 if (qctl.qc_type == UGQUOTA) {
1306 fprintf(stderr, "error: user and group quotas can't be set "
1311 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1312 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1314 if (optind + 6 != argc)
1317 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1319 fprintf(stderr, "error: find id for name %s failed: %s\n",
1320 argv[optind - 1], strerror(errno));
1324 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1325 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1326 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1327 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1329 dqb->dqb_valid = QIF_LIMITS;
1331 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1333 if (optind + 3 != argc)
1336 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1337 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1342 rc = llapi_quotactl(mnt, &qctl);
1345 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1346 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1353 static inline char *type2name(int check_type)
1355 if (check_type == USRQUOTA)
1357 else if (check_type == GRPQUOTA)
1364 static void grace2str(time_t seconds,char *buf)
1366 uint minutes, hours, days;
1368 minutes = (seconds + 30) / 60;
1369 hours = minutes / 60;
1374 snprintf(buf, 40, "%ddays", days);
1376 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1380 static void diff2str(time_t seconds, char *buf, time_t now)
1386 if (seconds <= now) {
1387 strcpy(buf, "none");
1390 grace2str(seconds - now, buf);
1393 static void print_quota_title(char *name, struct if_quotactl *qctl)
1395 printf("Disk quotas for %s %s (%cid %u):\n",
1396 type2name(qctl->qc_type), name,
1397 *type2name(qctl->qc_type), qctl->qc_id);
1398 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1400 "kbytes", "quota", "limit", "grace",
1401 "files", "quota", "limit", "grace");
1404 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1410 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1411 int bover = 0, iover = 0;
1412 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1414 if (dqb->dqb_bhardlimit &&
1415 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1417 } else if (dqb->dqb_bsoftlimit &&
1418 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1419 if (dqb->dqb_btime > now) {
1426 if (dqb->dqb_ihardlimit &&
1427 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1429 } else if (dqb->dqb_isoftlimit &&
1430 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1431 if (dqb->dqb_btime > now) {
1438 #if 0 /* XXX: always print quotas even when no usages */
1439 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1445 if (strlen(mnt) > 15)
1446 printf("%s\n%15s", mnt, "");
1448 printf("%15s", mnt);
1451 diff2str(dqb->dqb_btime, timebuf, now);
1453 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1454 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1455 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1456 printf(" %7s%c %6s %7s %7s",
1457 numbuf[0], bover ? '*' : ' ', numbuf[1],
1458 numbuf[2], bover > 1 ? timebuf : "");
1461 diff2str(dqb->dqb_itime, timebuf, now);
1463 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1464 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1465 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1467 printf(" %7s%c %6s %7s %7s",
1468 numbuf[0], iover ? '*' : ' ', numbuf[1],
1469 numbuf[2], iover > 1 ? timebuf : "");
1472 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1473 qctl->qc_cmd == Q_GETOINFO) {
1477 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1478 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1479 printf("Block grace time: %s; Inode grace time: %s\n",
1480 bgtimebuf, igtimebuf);
1484 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1488 /* XXX: this is a flag to mark that only mds quota is wanted */
1489 qctl->qc_dqblk.dqb_valid = 1;
1490 rc = llapi_quotactl(mnt, qctl);
1492 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1495 qctl->qc_dqblk.dqb_valid = 0;
1497 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1500 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1503 struct obd_uuid uuids[1024], *uuidp;
1504 int obdcount = 1024;
1509 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1513 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1515 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1519 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1520 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1522 /* XXX clear this flag to get quota from osts */
1523 qctl->qc_dqblk.dqb_valid = 0;
1524 rc = llapi_quotactl(mnt, qctl);
1526 fprintf(stderr, "%s quotactl failed: %s\n",
1527 uuidp->uuid, strerror(errno));
1531 print_quota(uuidp->uuid, qctl, 1);
1539 static int lfs_quota(int argc, char **argv)
1542 char *name = NULL, *mnt;
1543 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1545 char *obd_type = qctl.obd_type;
1546 char *obd_uuid = qctl.obd_uuid.uuid;
1550 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1553 qctl.qc_type = 0x01;
1556 qctl.qc_type = 0x02;
1559 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1562 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1565 fprintf(stderr, "error: %s: option '-%c' "
1566 "unrecognized\n", argv[0], c);
1575 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1576 if (optind + 2 != argc) {
1577 fprintf(stderr, "error: missing quota argument(s)\n");
1581 name = argv[optind++];
1582 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1584 fprintf(stderr,"error: can't find id for name %s: %s\n",
1585 name, strerror(errno));
1588 print_quota_title(name, &qctl);
1589 } else if (optind + 1 != argc) {
1590 fprintf(stderr, "error: missing quota info argument(s)\n");
1596 rc = llapi_quotactl(mnt, &qctl);
1599 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1600 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1605 rc = id2name(&name, getuid(), qctl.qc_type);
1612 print_quota(mnt, &qctl, 0);
1614 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1615 print_mds_quota(mnt, &qctl);
1616 print_lov_quota(mnt, &qctl);
1621 #endif /* HAVE_QUOTA_SUPPORT */
1623 static int flushctx_ioctl(char *mp)
1627 fd = open(mp, O_RDONLY);
1629 fprintf(stderr, "flushctx: error open %s: %s\n",
1630 mp, strerror(errno));
1634 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1636 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1637 mp, strerror(errno));
1643 static int lfs_flushctx(int argc, char **argv)
1645 int kdestroy = 0, c;
1647 char procline[PATH_MAX], *line;
1651 while ((c = getopt(argc, argv, "k")) != -1) {
1657 fprintf(stderr, "error: %s: option '-%c' "
1658 "unrecognized\n", argv[0], c);
1664 system("kdestroy > /dev/null");
1666 if (optind >= argc) {
1667 /* flush for all mounted lustre fs. */
1668 proc = fopen("/proc/mounts", "r");
1670 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1675 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1680 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1681 fprintf(stderr, "%s: unexpected format in "
1687 if (strcmp(fs, "lustre") != 0)
1689 /* we use '@' to determine it's a client. are there
1690 * any other better way?
1692 if (strchr(dev, '@') == NULL)
1695 if (flushctx_ioctl(mp))
1699 /* flush fs as specified */
1700 while (optind < argc) {
1701 if (flushctx_ioctl(argv[optind++]))
1710 * We assume one and only one filename is supplied as the
1713 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1715 char *dname, *rpath = NULL;
1716 char path[PATH_MAX], cwd[PATH_MAX];
1724 /* FIXME the premise is there is no sub-mounted filesystems under this
1725 * mounted lustre tree. */
1726 strncpy(fname, argv[argc - 1], PATH_MAX);
1728 /* get path prefix */
1729 dname = dirname(fname);
1731 /* try to resolve the pathname into relative to the root of the mounted
1732 * lustre filesystem.
1734 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1735 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1739 if (chdir(dname) == -1) {
1740 fprintf(stderr, "chdir to %s failed: %s\n",
1741 dname, strerror(errno));
1745 if (getcwd(path, sizeof(path)) == NULL) {
1746 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1750 if (chdir(cwd) == -1) {
1751 fprintf(stderr, "chdir back to %s: %s\n",
1752 cwd, strerror(errno));
1756 strncat(path, "/", PATH_MAX);
1757 strncpy(fname, argv[argc - 1], PATH_MAX);
1758 strncat(path, basename(fname), PATH_MAX);
1760 fp = setmntent(MOUNTED, "r");
1762 fprintf(stderr, "setmntent %s failed: %s\n",
1763 MOUNTED, strerror(errno));
1768 mnt = getmntent(fp);
1772 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
1775 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1776 rpath = path + strlen(mnt->mnt_dir);
1782 /* remove char '/' from rpath to be a relative path */
1783 while (rpath && *rpath == '/') rpath++;
1787 "%s: file %s doesn't belong to a lustre file system!\n",
1788 argv[0], argv[argc - 1]);
1792 for (i = 0; i < argc - 1; i++) {
1793 strncat(cmd, argv[i], PATH_MAX);
1794 strncat(cmd, " ", PATH_MAX);
1796 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1797 strncpy(fname, argv[argc - 1], sizeof(fname));
1802 static int lfs_getfacl(int argc, char **argv)
1804 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1806 if (acl_cmd_parse(argc, argv, fname, cmd))
1809 return llapi_getfacl(fname, cmd);
1812 static int lfs_setfacl(int argc, char **argv)
1814 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1816 if (acl_cmd_parse(argc, argv, fname, cmd))
1819 return llapi_setfacl(fname, cmd);
1822 int main(int argc, char **argv)
1828 ptl_initialize(argc, argv);
1829 if (obd_initialize(argc, argv) < 0)
1831 if (dbg_initialize(argc, argv) < 0)
1834 Parser_init("lfs > ", cmdlist);
1837 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1839 rc = Parser_commands();
1842 obd_finalize(argc, argv);