1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Robert Read <rread@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <sys/types.h>
44 #include <lnet/api-support.h>
45 #include <lnet/lnetctl.h>
47 #include <liblustre.h>
48 #include <lustre/lustre_idl.h>
49 #include <lustre/liblustreapi.h>
50 #include <lustre/lustre_user.h>
55 unsigned int libcfs_subsystem_debug = 0;
58 static int lfs_setstripe(int argc, char **argv);
59 static int lfs_find(int argc, char **argv);
60 static int lfs_getstripe(int argc, char **argv);
61 static int lfs_osts(int argc, char **argv);
62 static int lfs_df(int argc, char **argv);
63 static int lfs_check(int argc, char **argv);
64 static int lfs_catinfo(int argc, char **argv);
65 #ifdef HAVE_QUOTA_SUPPORT
66 static int lfs_quotachown(int argc, char **argv);
67 static int lfs_quotacheck(int argc, char **argv);
68 static int lfs_quotaon(int argc, char **argv);
69 static int lfs_quotaoff(int argc, char **argv);
70 static int lfs_setquota(int argc, char **argv);
71 static int lfs_quota(int argc, char **argv);
73 static int lfs_flushctx(int argc, char **argv);
74 static int lfs_join(int argc, char **argv);
75 static int lfs_getfacl(int argc, char **argv);
76 static int lfs_setfacl(int argc, char **argv);
78 /* all avaialable commands */
79 command_t cmdlist[] = {
80 {"setstripe", lfs_setstripe, 0,
81 "Create a new file with a specific striping pattern or\n"
82 "set the default striping pattern on an existing directory or\n"
83 "delete the default striping pattern from an existing directory\n"
84 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
86 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
87 " [--index|-i stripe_index]\n"
88 " [--count|-c stripe_count]\n"
90 " setstripe -d <dirname> (to delete default striping)\n"
91 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\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)
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 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
259 argv[0], stripe_size_arg);
264 /* get the stripe offset */
265 if (stripe_off_arg != NULL) {
266 st_offset = strtoul(stripe_off_arg, &end, 0);
268 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
269 argv[0], stripe_off_arg);
273 /* get the stripe count */
274 if (stripe_count_arg != NULL) {
275 st_count = strtoul(stripe_count_arg, &end, 0);
277 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
278 argv[0], stripe_count_arg);
283 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
285 fprintf(stderr, "error: %s: create stripe file failed\n",
291 static int set_time(time_t *time, time_t *set, char *str)
298 else if (str[0] == '-')
304 t = strtol(str, NULL, 0);
305 if (*time < t * 24 * 60 * 60) {
308 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
312 *set = *time - t * 24 * 60 * 60;
316 static int lfs_find(int argc, char **argv)
323 int quiet, verbose, recursive;
324 struct find_param param;
325 struct obd_uuid *obduuid = NULL;
327 struct option long_opts[] = {
328 /* New find options. */
329 {"atime", required_argument, 0, 'A'},
330 {"ctime", required_argument, 0, 'C'},
331 {"mtime", required_argument, 0, 'M'},
332 {"maxdepth", required_argument, 0, 'D'},
333 {"name", required_argument, 0, 'n'},
334 /* --obd is considered as a new option. */
335 {"obd", required_argument, 0, 'O'},
336 {"ost", required_argument, 0, 'O'},
337 {"print", no_argument, 0, 'P'},
338 {"print0", no_argument, 0, 'p'},
339 /* Old find options. */
340 {"quiet", no_argument, 0, 'q'},
341 {"recursive", no_argument, 0, 'r'},
342 {"verbose", no_argument, 0, 'v'},
355 quiet = verbose = recursive = 0;
357 memset(¶m, 0, sizeof(param));
359 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpOqrv",
360 long_opts, NULL)) >= 0)
366 /* '!' is part of option */
367 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
368 if (!isoption && pathend != -1) {
369 fprintf(stderr, "err: %s: filename|dirname must either "
370 "precede options or follow options\n",
374 if (!isoption && pathstart == -1)
375 pathstart = optind - 1;
376 if (isoption && pathstart != -1 && pathend == -1) {
377 pathend = optind - 2;
378 if ((c == 1 && strcmp(optarg, "!") == 0) ||
379 c == 'P' || c == 'p' || c == 'O' ||
380 c == 'q' || c == 'r' || c == 'v')
381 pathend = optind - 1;
388 if (strcmp(optarg, "!") == 0)
392 xtime = ¶m.atime;
393 xsign = ¶m.asign;
396 xtime = ¶m.ctime;
397 xsign = ¶m.csign;
401 xtime = ¶m.mtime;
402 xsign = ¶m.msign;
406 if (optarg[0] == '-')
408 else if (optarg[0] == '+')
413 strcat(timestr, optarg);
417 ret = set_time(&t, xtime, optarg);
424 depth = strtol(optarg, 0, 0);
428 param.pattern = (char *)optarg;
430 param.exclude_pattern = 1;
432 param.exclude_pattern = 0;
437 "error: %s: only one obduuid allowed",
441 obduuid = (struct obd_uuid *)optarg;
465 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
466 argv[0], argv[optind - 1]);
471 if (pathstart == -1) {
472 fprintf(stderr, "error: %s: no filename|pathname\n",
475 } else if (pathend == -1) {
480 param.obduuid = obduuid;
482 param.maxdepth = depth;
483 param.zeroend = zeroend;
486 param.recursive = recursive;
487 param.verbose = verbose;
489 param.maxdepth = recursive ? -1 : 1;
494 ret = llapi_find(argv[pathstart], ¶m);
496 ret = llapi_getstripe(argv[pathstart], ¶m);
497 } while (++pathstart < pathend && !ret);
500 fprintf(stderr, "error: %s failed for %s.\n",
501 argv[0], argv[optind - 1]);
505 static int lfs_getstripe(int argc, char **argv)
507 struct option long_opts[] = {
509 {"quiet", 0, 0, 'q'},
510 {"recursive", 0, 0, 'r'},
511 {"verbose", 0, 0, 'v'},
514 char short_opts[] = "hO:qrv";
515 int quiet, verbose, recursive, c, rc;
516 struct obd_uuid *obduuid = NULL;
517 struct find_param param;
520 quiet = verbose = recursive = 0;
521 while ((c = getopt_long(argc, argv, short_opts,
522 long_opts, NULL)) != -1) {
527 "error: %s: only one obduuid allowed",
531 obduuid = (struct obd_uuid *)optarg;
547 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
548 argv[0], argv[optind - 1]);
556 memset(¶m, 0, sizeof(param));
557 param.recursive = recursive;
558 param.verbose = verbose;
560 param.obduuid = obduuid;
561 param.maxdepth = recursive ? -1 : 1;
564 rc = llapi_getstripe(argv[optind], ¶m);
565 } while (++optind < argc && !rc);
568 fprintf(stderr, "error: %s failed for %s.\n",
569 argv[0], argv[optind - 1]);
573 static int lfs_osts(int argc, char **argv)
576 struct mntent *mnt = NULL;
577 struct obd_uuid *obduuid = NULL;
578 struct find_param param;
584 fp = setmntent(MOUNTED, "r");
587 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
591 memset(¶m, 0, sizeof(param));
592 param.obduuid = obduuid;
593 while (feof(fp) == 0 && ferror(fp) ==0) {
594 if (llapi_is_lustre_mnt(mnt)) {
595 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
598 "error: %s: failed on %s\n",
599 argv[0], mnt->mnt_dir);
609 #define COOK(value) \
612 while (value > 1024) { \
626 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
628 char rpath[PATH_MAX] = {'\0'};
630 int rc, len, out_len = 0;
632 if (!realpath(path, rpath)) {
634 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
635 path, strerror(-rc));
641 while (feof(fp) == 0 && ferror(fp) == 0) {
642 if (llapi_is_lustre_mnt(mnt)) {
643 len = strlen(mnt->mnt_dir);
645 !strncmp(rpath, mnt->mnt_dir, len)) {
647 memset(mntdir, 0, dir_len);
648 strncpy(mntdir, mnt->mnt_dir, dir_len);
657 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
661 static int showdf(char *mntdir, struct obd_statfs *stat,
662 char *uuid, int ishow, int cooked,
663 char *type, int index, int rc)
665 long long avail, used, total;
667 char *suffix = "KMGTPEZY";
668 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
676 avail = stat->os_ffree;
677 used = stat->os_files - stat->os_ffree;
678 total = stat->os_files;
680 int shift = cooked ? 0 : 10;
682 avail = (stat->os_bavail * stat->os_bsize) >> shift;
683 used = stat->os_blocks - stat->os_bavail;
684 used = (used * stat->os_bsize) >> shift;
685 total = (stat->os_blocks * stat->os_bsize) >> shift;
689 ratio = (double)used / (double)total;
695 cook_val = (double)total;
698 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
700 sprintf(tbuf, CDF, total);
702 cook_val = (double)used;
705 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
707 sprintf(ubuf, CDF, used);
709 cook_val = (double)avail;
712 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
714 sprintf(abuf, CDF, avail);
716 sprintf(tbuf, CDF, total);
717 sprintf(ubuf, CDF, used);
718 sprintf(abuf, CDF, avail);
721 sprintf(rbuf, RDF, (int)(ratio * 100));
722 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
723 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
725 printf("[%s:%d]\n", type, index);
731 printf(UUF": inactive device\n", uuid);
734 printf(UUF": %s\n", uuid, strerror(-rc));
741 static int mntdf(char *mntdir, int ishow, int cooked)
743 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
744 struct obd_uuid uuid_buf;
749 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
750 "UUID", "Inodes", "IUsed", "IFree",
751 "IUse%", "Mounted on");
753 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
754 "UUID", cooked ? "bytes" : "1K-blocks",
755 "Used", "Available", "Use%", "Mounted on");
757 for (index = 0; ; index++) {
758 memset(&stat_buf, 0, sizeof(struct obd_statfs));
759 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
760 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
761 &stat_buf, &uuid_buf);
768 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
769 rc == -ENODATA || rc == 0) {
770 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
774 "error: llapi_obd_statfs(%s): %s (%d)\n",
775 uuid_buf.uuid, strerror(-rc), rc);
779 sum.os_ffree += stat_buf.os_ffree;
780 sum.os_files += stat_buf.os_files;
784 for (index = 0; ; index++) {
785 memset(&stat_buf, 0, sizeof(struct obd_statfs));
786 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
787 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
788 &stat_buf, &uuid_buf);
795 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
796 rc == -ENODATA || rc == 0) {
797 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
801 "error: llapi_obd_statfs failed: %s (%d)\n",
806 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
807 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
808 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
813 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
818 static int lfs_df(int argc, char **argv)
822 struct mntent *mnt = NULL;
823 char mntdir[PATH_MAX] = {'\0'};
824 int ishow = 0, cooked = 0;
828 while ((c = getopt(argc, argv, "ih")) != -1) {
843 fp = setmntent(MOUNTED, "r");
846 fprintf(stderr, "error: %s: open %s failed( %s )\n",
847 argv[0], MOUNTED, strerror(errno));
851 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
857 rc = mntdf(mntdir, ishow, cooked);
862 while (feof(fp) == 0 && ferror(fp) == 0) {
863 if (llapi_is_lustre_mnt(mnt)) {
864 rc = mntdf(mnt->mnt_dir, ishow, cooked);
877 static int lfs_check(int argc, char **argv)
881 struct mntent *mnt = NULL;
890 obd_types[0] = obd_type1;
891 obd_types[1] = obd_type2;
893 if (strcmp(argv[1], "osts") == 0) {
894 strcpy(obd_types[0], "osc");
895 } else if (strcmp(argv[1], "mds") == 0) {
896 strcpy(obd_types[0], "mdc");
897 } else if (strcmp(argv[1], "servers") == 0) {
899 strcpy(obd_types[0], "osc");
900 strcpy(obd_types[1], "mdc");
902 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
907 fp = setmntent(MOUNTED, "r");
909 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
913 while (feof(fp) == 0 && ferror(fp) ==0) {
914 if (llapi_is_lustre_mnt(mnt))
922 fprintf(stderr, "No suitable Lustre mount found\n");
926 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
929 fprintf(stderr, "error: %s: %s status failed\n",
936 static int lfs_catinfo(int argc, char **argv)
939 struct mntent *mnt = NULL;
942 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
945 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
948 fp = setmntent(MOUNTED, "r");
950 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
954 while (feof(fp) == 0 && ferror(fp) == 0) {
955 if (llapi_is_lustre_mnt(mnt))
964 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
966 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
968 fprintf(stderr, "no lustre_lite mounted.\n");
975 int lfs_join(int argc, char **argv)
977 char *name_head, *name_tail;
984 fd = open(name_head, O_WRONLY);
986 fprintf(stderr, "Can not open name_head %s rc=%d\n",
990 size = lseek(fd, 0, SEEK_END);
991 if (size % JOIN_FILE_ALIGN) {
992 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
993 name_head, (long long)size, JOIN_FILE_ALIGN);
998 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1002 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1008 #ifdef HAVE_QUOTA_SUPPORT
1009 static int lfs_quotachown(int argc, char **argv)
1015 while ((c = getopt(argc, argv, "i")) != -1) {
1021 fprintf(stderr, "error: %s: option '-%c' "
1022 "unrecognized\n", argv[0], c);
1028 rc = llapi_quotachown(argv[optind], flag);
1030 fprintf(stderr,"error: change file owner/group failed.\n");
1035 static int lfs_quotacheck(int argc, char **argv)
1037 int c, check_type = 0;
1039 struct if_quotacheck qchk;
1040 struct if_quotactl qctl;
1041 char *obd_type = qchk.obd_type;
1042 char *obd_uuid = qchk.obd_uuid.uuid;
1045 memset(&qchk, 0, sizeof(qchk));
1048 while ((c = getopt(argc, argv, "ug")) != -1) {
1057 fprintf(stderr, "error: %s: option '-%c' "
1058 "unrecognized\n", argv[0], c);
1065 else /* do quotacheck for both user & group quota by default */
1073 memset(&qctl, 0, sizeof(qctl));
1074 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1075 qctl.qc_id = QFMT_LDISKFS;
1076 qctl.qc_type = check_type;
1077 rc = llapi_quotactl(mnt, &qctl);
1079 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1083 rc = llapi_quotacheck(mnt, check_type);
1085 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1089 rc = llapi_poll_quotacheck(mnt, &qchk);
1092 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1093 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1097 memset(&qctl, 0, sizeof(qctl));
1098 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1099 qctl.qc_id = QFMT_LDISKFS;
1100 qctl.qc_type = check_type;
1101 rc = llapi_quotactl(mnt, &qctl);
1104 fprintf(stderr, "%s %s ",
1105 qctl.obd_type, qctl.obd_uuid.uuid);
1106 fprintf(stderr, "%s turn on quota failed: %s\n",
1107 argv[0], strerror(errno));
1114 static int lfs_quotaon(int argc, char **argv)
1118 struct if_quotactl qctl;
1119 char *obd_type = qctl.obd_type;
1120 char *obd_uuid = qctl.obd_uuid.uuid;
1123 memset(&qctl, 0, sizeof(qctl));
1124 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1125 qctl.qc_id = QFMT_LDISKFS;
1128 while ((c = getopt(argc, argv, "ugf")) != -1) {
1131 qctl.qc_type |= 0x01;
1134 qctl.qc_type |= 0x02;
1137 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1140 fprintf(stderr, "error: %s: option '-%c' "
1141 "unrecognized\n", argv[0], c);
1154 rc = llapi_quotactl(mnt, &qctl);
1157 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1158 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1165 static int lfs_quotaoff(int argc, char **argv)
1169 struct if_quotactl qctl;
1170 char *obd_type = qctl.obd_type;
1171 char *obd_uuid = qctl.obd_uuid.uuid;
1174 memset(&qctl, 0, sizeof(qctl));
1175 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1178 while ((c = getopt(argc, argv, "ug")) != -1) {
1181 qctl.qc_type |= 0x01;
1184 qctl.qc_type |= 0x02;
1187 fprintf(stderr, "error: %s: option '-%c' "
1188 "unrecognized\n", argv[0], c);
1201 rc = llapi_quotactl(mnt, &qctl);
1204 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1205 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1212 static int name2id(unsigned int *id, char *name, int type)
1214 if (type == USRQUOTA) {
1215 struct passwd *entry;
1217 if (!(entry = getpwnam(name))) {
1223 *id = entry->pw_uid;
1225 struct group *entry;
1227 if (!(entry = getgrnam(name))) {
1233 *id = entry->gr_gid;
1239 static int id2name(char **name, unsigned int id, int type)
1241 if (type == USRQUOTA) {
1242 struct passwd *entry;
1244 if (!(entry = getpwuid(id))) {
1250 *name = entry->pw_name;
1252 struct group *entry;
1254 if (!(entry = getgrgid(id))) {
1260 *name = entry->gr_name;
1266 #define ARG2INT(nr, str, msg) \
1269 nr = strtol(str, &endp, 0); \
1271 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1276 int lfs_setquota(int argc, char **argv)
1280 struct if_quotactl qctl;
1281 char *obd_type = qctl.obd_type;
1282 char *obd_uuid = qctl.obd_uuid.uuid;
1285 memset(&qctl, 0, sizeof(qctl));
1286 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1289 while ((c = getopt(argc, argv, "ugt")) != -1) {
1292 qctl.qc_type |= 0x01;
1295 qctl.qc_type |= 0x02;
1298 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1301 fprintf(stderr, "error: %s: option '-%c' "
1302 "unrecognized\n", argv[0], c);
1310 if (qctl.qc_type == UGQUOTA) {
1311 fprintf(stderr, "error: user and group quotas can't be set "
1316 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1317 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1319 if (optind + 6 != argc)
1322 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1324 fprintf(stderr, "error: find id for name %s failed: %s\n",
1325 argv[optind - 1], strerror(errno));
1329 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1330 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1331 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1332 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1334 dqb->dqb_valid = QIF_LIMITS;
1336 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1338 if (optind + 3 != argc)
1341 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1342 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1347 rc = llapi_quotactl(mnt, &qctl);
1350 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1351 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1358 static inline char *type2name(int check_type)
1360 if (check_type == USRQUOTA)
1362 else if (check_type == GRPQUOTA)
1369 static void grace2str(time_t seconds,char *buf)
1371 uint minutes, hours, days;
1373 minutes = (seconds + 30) / 60;
1374 hours = minutes / 60;
1379 snprintf(buf, 40, "%ddays", days);
1381 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1385 static void diff2str(time_t seconds, char *buf, time_t now)
1391 if (seconds <= now) {
1392 strcpy(buf, "none");
1395 grace2str(seconds - now, buf);
1398 static void print_quota_title(char *name, struct if_quotactl *qctl)
1400 printf("Disk quotas for %s %s (%cid %u):\n",
1401 type2name(qctl->qc_type), name,
1402 *type2name(qctl->qc_type), qctl->qc_id);
1403 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1405 "blocks", "quota", "limit", "grace",
1406 "files", "quota", "limit", "grace");
1409 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1415 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1416 int bover = 0, iover = 0;
1417 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1419 if (dqb->dqb_bhardlimit &&
1420 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1422 } else if (dqb->dqb_bsoftlimit &&
1423 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1424 if (dqb->dqb_btime > now) {
1431 if (dqb->dqb_ihardlimit &&
1432 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1434 } else if (dqb->dqb_isoftlimit &&
1435 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1436 if (dqb->dqb_btime > now) {
1443 #if 0 /* XXX: always print quotas even when no usages */
1444 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1450 if (strlen(mnt) > 15)
1451 printf("%s\n%15s", mnt, "");
1453 printf("%15s", mnt);
1456 diff2str(dqb->dqb_btime, timebuf, now);
1458 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1459 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1460 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1461 printf(" %7s%c %6s %7s %7s",
1462 numbuf[0], bover ? '*' : ' ', numbuf[1],
1463 numbuf[2], bover > 1 ? timebuf : "");
1466 diff2str(dqb->dqb_itime, timebuf, now);
1468 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1469 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1470 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1472 printf(" %7s%c %6s %7s %7s",
1473 numbuf[0], iover ? '*' : ' ', numbuf[1],
1474 numbuf[2], iover > 1 ? timebuf : "");
1477 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1478 qctl->qc_cmd == Q_GETOINFO) {
1482 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1483 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1484 printf("Block grace time: %s; Inode grace time: %s\n",
1485 bgtimebuf, igtimebuf);
1489 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1493 /* XXX: this is a flag to mark that only mds quota is wanted */
1494 qctl->qc_dqblk.dqb_valid = 1;
1495 rc = llapi_quotactl(mnt, qctl);
1497 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1500 qctl->qc_dqblk.dqb_valid = 0;
1502 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1505 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1508 struct obd_uuid uuids[1024], *uuidp;
1509 int obdcount = 1024;
1514 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1518 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1520 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1524 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1525 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1527 /* XXX clear this flag to get quota from osts */
1528 qctl->qc_dqblk.dqb_valid = 0;
1529 rc = llapi_quotactl(mnt, qctl);
1531 fprintf(stderr, "%s quotactl failed: %s\n",
1532 uuidp->uuid, strerror(errno));
1536 print_quota(uuidp->uuid, qctl, 1);
1544 static int lfs_quota(int argc, char **argv)
1547 char *name = NULL, *mnt;
1548 struct if_quotactl qctl;
1549 char *obd_type = qctl.obd_type;
1550 char *obd_uuid = qctl.obd_uuid.uuid;
1553 memset(&qctl, 0, sizeof(qctl));
1554 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1557 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1560 qctl.qc_type |= 0x01;
1563 qctl.qc_type |= 0x02;
1566 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1569 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1572 fprintf(stderr, "error: %s: option '-%c' "
1573 "unrecognized\n", argv[0], c);
1581 if (qctl.qc_type == UGQUOTA) {
1582 fprintf(stderr, "error: user or group can't be specified"
1587 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1588 if (optind + 2 != argc)
1591 name = argv[optind++];
1592 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1594 fprintf(stderr, "error: find id for name %s failed: %s\n",
1595 name, strerror(errno));
1598 print_quota_title(name, &qctl);
1599 } else if (optind + 1 != argc) {
1605 rc = llapi_quotactl(mnt, &qctl);
1608 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1609 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1614 rc = id2name(&name, getuid(), qctl.qc_type);
1621 print_quota(mnt, &qctl, 0);
1623 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1624 print_mds_quota(mnt, &qctl);
1625 print_lov_quota(mnt, &qctl);
1630 #endif /* HAVE_QUOTA_SUPPORT */
1632 static int flushctx_ioctl(char *mp)
1636 fd = open(mp, O_RDONLY);
1638 fprintf(stderr, "flushctx: error open %s: %s\n",
1639 mp, strerror(errno));
1643 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1645 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1646 mp, strerror(errno));
1652 static int lfs_flushctx(int argc, char **argv)
1654 int kdestroy = 0, c;
1656 char procline[PATH_MAX], *line;
1660 while ((c = getopt(argc, argv, "k")) != -1) {
1666 fprintf(stderr, "error: %s: option '-%c' "
1667 "unrecognized\n", argv[0], c);
1673 system("kdestroy > /dev/null");
1675 if (optind >= argc) {
1676 /* flush for all mounted lustre fs. */
1677 proc = fopen("/proc/mounts", "r");
1679 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1684 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1689 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1690 fprintf(stderr, "%s: unexpected format in "
1696 if (strcmp(fs, "lustre") != 0)
1698 /* we use '@' to determine it's a client. are there
1699 * any other better way?
1701 if (strchr(dev, '@') == NULL)
1704 if (flushctx_ioctl(mp))
1708 /* flush fs as specified */
1709 while (optind < argc) {
1710 if (flushctx_ioctl(argv[optind++]))
1719 * We assume one and only one filename is supplied as the
1722 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1724 char *dname, *rpath = NULL;
1725 char path[PATH_MAX], cwd[PATH_MAX];
1733 /* FIXME the premise is there is no sub-mounted filesystems under this
1734 * mounted lustre tree. */
1735 strncpy(fname, argv[argc - 1], PATH_MAX);
1737 /* get path prefix */
1738 dname = dirname(fname);
1740 /* try to resolve the pathname into relative to the root of the mounted
1741 * lustre filesystem.
1743 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1744 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1748 if (chdir(dname) == -1) {
1749 fprintf(stderr, "chdir to %s failed: %s\n",
1750 dname, strerror(errno));
1754 if (getcwd(path, sizeof(path)) == NULL) {
1755 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1759 if (chdir(cwd) == -1) {
1760 fprintf(stderr, "chdir back to %s: %s\n",
1761 cwd, strerror(errno));
1765 strncat(path, "/", PATH_MAX);
1766 strncpy(fname, argv[argc - 1], PATH_MAX);
1767 strncat(path, basename(fname), PATH_MAX);
1769 fp = setmntent(MOUNTED, "r");
1771 fprintf(stderr, "setmntent %s failed: %s\n",
1772 MOUNTED, strerror(errno));
1777 mnt = getmntent(fp);
1781 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
1784 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1785 rpath = path + strlen(mnt->mnt_dir);
1791 /* remove char '/' from rpath to be a relative path */
1792 while (rpath && *rpath == '/') rpath++;
1796 "%s: file %s doesn't belong to a lustre file system!\n",
1797 argv[0], argv[argc - 1]);
1801 for (i = 0; i < argc - 1; i++) {
1802 strncat(cmd, argv[i], PATH_MAX);
1803 strncat(cmd, " ", PATH_MAX);
1805 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1806 strncpy(fname, argv[argc - 1], sizeof(fname));
1811 static int lfs_getfacl(int argc, char **argv)
1813 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1815 if (acl_cmd_parse(argc, argv, fname, cmd))
1818 return llapi_getfacl(fname, cmd);
1821 static int lfs_setfacl(int argc, char **argv)
1823 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1825 if (acl_cmd_parse(argc, argv, fname, cmd))
1828 return llapi_setfacl(fname, cmd);
1831 int main(int argc, char **argv)
1837 ptl_initialize(argc, argv);
1838 if (obd_initialize(argc, argv) < 0)
1840 if (dbg_initialize(argc, argv) < 0)
1843 Parser_init("lfs > ", cmdlist);
1846 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1848 rc = Parser_commands();
1851 obd_finalize(argc, argv);