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 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
93 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
94 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
95 {"getstripe", lfs_getstripe, 0,
96 "To list the striping info for a given filename or files in a\n"
97 "directory or recursively for all files in a directory tree.\n"
98 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
99 " [--recursive | -r] <dir|file> ..."},
100 {"find", lfs_find, 0,
101 "To find files that match given parameters recursively in a directory tree.\n"
102 "usage: find <dir/file> ... \n"
103 " [[!] --atime|-A N] [[!] --mtime|-M N] [[!] --ctime|-C N] [--maxdepth|-D N]\n"
104 " [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
105 "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
106 "\t !: used before --name means find exclude the regular expression pattern\n"
107 "If one of the options below is provided, find works the same as 'getstripe':\n"
108 "To list the striping info for a given filename or files in a directory or\n"
110 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
111 " [--recursive | -r] <dir|file> ..."},
112 {"check", lfs_check, 0,
113 "Display the status of MDS or OSTs (as specified in the command)\n"
114 "or all the servers (MDS and OSTs).\n"
115 "usage: check <osts|mds|servers>"},
116 {"catinfo", lfs_catinfo, 0,
117 "Show information of specified type logs.\n"
118 "usage: catinfo {keyword} [node name]\n"
119 "\tkeywords are one of followings: config, deletions.\n"
120 "\tnode name must be provided when use keyword config."},
121 {"join", lfs_join, 0,
122 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
123 "usage: join <filename_A> <filename_B>\n"},
124 {"osts", lfs_osts, 0, "osts"},
126 "report filesystem disk space usage or inodes usage"
128 "Usage: df [-i] [-h] [path]"},
129 #ifdef HAVE_QUOTA_SUPPORT
130 {"quotachown",lfs_quotachown, 0,
131 "Change files' owner or group on the specified filesystem.\n"
132 "usage: quotachown [-i] <filesystem>\n"
133 "\t-i: ignore error if file is not exist\n"},
134 {"quotacheck", lfs_quotacheck, 0,
135 "Scan the specified filesystem for disk usage, and create,\n"
136 "or update quota files.\n"
137 "usage: quotacheck [ -ug ] <filesystem>"},
138 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
139 "usage: quotaon [ -ugf ] <filesystem>"},
140 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
141 "usage: quotaoff [ -ug ] <filesystem>"},
142 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
143 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
144 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
145 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
146 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
148 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
149 "usage: flushctx [-k] [mountpoint...]"},
150 {"getfacl", lfs_getfacl, 0,
151 "Get file access control list in remote client.\n"
152 "usage: getfacl [-dRLPvh] file"},
153 {"setfacl", lfs_setfacl, 0,
154 "Set file access control list in remote client.\n"
155 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
156 {"help", Parser_help, 0, "help"},
157 {"exit", Parser_quit, 0, "quit"},
158 {"quit", Parser_quit, 0, "quit"},
163 static int lfs_setstripe(int argc, char **argv)
167 unsigned long st_size;
168 int st_offset, st_count;
172 char *stripe_size_arg = NULL;
173 char *stripe_off_arg = NULL;
174 char *stripe_count_arg = NULL;
176 struct option long_opts[] = {
177 {"size", required_argument, 0, 's'},
178 {"count", required_argument, 0, 'c'},
179 {"index", required_argument, 0, 'i'},
180 {"delete", no_argument, 0, 'd'},
187 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
188 /* for compatibility with the existing positional parameter
192 } else if (argc == 5 &&
193 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
194 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
195 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
196 /* for compatibility with the existing positional parameter
199 stripe_size_arg = argv[2];
200 stripe_off_arg = argv[3];
201 stripe_count_arg = argv[4];
204 while ((c = getopt_long(argc, argv, "c:di:s:",
205 long_opts, NULL)) >= 0)
212 stripe_count_arg = optarg;
215 /* delete the default striping pattern */
219 stripe_off_arg = optarg;
222 stripe_size_arg = optarg;
227 fprintf(stderr, "error: %s: option '%s' "
229 argv[0], argv[optind - 1]);
234 fname = argv[optind];
240 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
241 stripe_count_arg != NULL)) {
242 fprintf(stderr, "error: %s: cannot specify -d with "
243 "-s, -c or -i options\n",
248 if (optind != argc - 1) {
249 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
251 argv[0], argv[argc - 1]);
255 /* get the stripe size */
256 if (stripe_size_arg != NULL) {
257 st_size = strtoul(stripe_size_arg, &end, 0);
259 if ((*end == 'k' || *end == 'K') &&
261 (st_size & (~0UL << (32 - 10))) == 0) {
263 } else if ((*end == 'm' || *end == 'M') &&
265 (st_size & (~0UL << (32 - 20))) == 0) {
267 } else if ((*end == 'g' || *end == 'G') &&
269 (st_size & (~0UL << (32 - 30))) == 0) {
272 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
273 argv[0], stripe_size_arg);
278 /* get the stripe offset */
279 if (stripe_off_arg != NULL) {
280 st_offset = strtoul(stripe_off_arg, &end, 0);
282 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
283 argv[0], stripe_off_arg);
287 /* get the stripe count */
288 if (stripe_count_arg != NULL) {
289 st_count = strtoul(stripe_count_arg, &end, 0);
291 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
292 argv[0], stripe_count_arg);
297 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
299 fprintf(stderr, "error: %s: create stripe file failed\n",
305 static int set_time(time_t *time, time_t *set, char *str)
312 else if (str[0] == '-')
318 t = strtol(str, NULL, 0);
319 if (*time < t * 24 * 60 * 60) {
322 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
326 *set = *time - t * 24 * 60 * 60;
330 static int lfs_find(int argc, char **argv)
337 int quiet, verbose, recursive;
338 struct find_param param;
339 struct obd_uuid *obduuid = NULL;
341 struct option long_opts[] = {
342 /* New find options. */
343 {"atime", required_argument, 0, 'A'},
344 {"ctime", required_argument, 0, 'C'},
345 {"mtime", required_argument, 0, 'M'},
346 {"maxdepth", required_argument, 0, 'D'},
347 {"name", required_argument, 0, 'n'},
348 /* --obd is considered as a new option. */
349 {"obd", required_argument, 0, 'O'},
350 {"ost", required_argument, 0, 'O'},
351 {"print", no_argument, 0, 'P'},
352 {"print0", no_argument, 0, 'p'},
353 /* Old find options. */
354 {"quiet", no_argument, 0, 'q'},
355 {"recursive", no_argument, 0, 'r'},
356 {"verbose", no_argument, 0, 'v'},
369 quiet = verbose = recursive = 0;
371 memset(¶m, 0, sizeof(param));
373 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrv",
374 long_opts, NULL)) >= 0)
380 /* '!' is part of option */
381 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
382 if (!isoption && pathend != -1) {
383 fprintf(stderr, "err: %s: filename|dirname must either "
384 "precede options or follow options\n",
388 if (!isoption && pathstart == -1)
389 pathstart = optind - 1;
390 if (isoption && pathstart != -1 && pathend == -1) {
391 pathend = optind - 2;
392 if ((c == 1 && strcmp(optarg, "!") == 0) ||
393 c == 'P' || c == 'p' || c == 'O' ||
394 c == 'q' || c == 'r' || c == 'v')
395 pathend = optind - 1;
402 if (strcmp(optarg, "!") == 0)
406 xtime = ¶m.atime;
407 xsign = ¶m.asign;
410 xtime = ¶m.ctime;
411 xsign = ¶m.csign;
415 xtime = ¶m.mtime;
416 xsign = ¶m.msign;
420 if (optarg[0] == '-')
422 else if (optarg[0] == '+')
427 strcat(timestr, optarg);
431 ret = set_time(&t, xtime, optarg);
438 depth = strtol(optarg, 0, 0);
442 param.pattern = (char *)optarg;
444 param.exclude_pattern = 1;
446 param.exclude_pattern = 0;
451 "error: %s: only one obduuid allowed",
455 obduuid = (struct obd_uuid *)optarg;
479 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
480 argv[0], argv[optind - 1]);
485 if (pathstart == -1) {
486 fprintf(stderr, "error: %s: no filename|pathname\n",
489 } else if (pathend == -1) {
494 param.obduuid = obduuid;
496 param.maxdepth = depth;
497 param.zeroend = zeroend;
500 param.recursive = recursive;
501 param.verbose = verbose;
503 param.maxdepth = recursive ? -1 : 1;
508 ret = llapi_find(argv[pathstart], ¶m);
510 ret = llapi_getstripe(argv[pathstart], ¶m);
511 } while (++pathstart < pathend && !ret);
514 fprintf(stderr, "error: %s failed for %s.\n",
515 argv[0], argv[optind - 1]);
519 static int lfs_getstripe(int argc, char **argv)
521 struct option long_opts[] = {
523 {"quiet", 0, 0, 'q'},
524 {"recursive", 0, 0, 'r'},
525 {"verbose", 0, 0, 'v'},
528 char short_opts[] = "hO:qrv";
529 int quiet, verbose, recursive, c, rc;
530 struct obd_uuid *obduuid = NULL;
531 struct find_param param;
534 quiet = verbose = recursive = 0;
535 while ((c = getopt_long(argc, argv, short_opts,
536 long_opts, NULL)) != -1) {
541 "error: %s: only one obduuid allowed",
545 obduuid = (struct obd_uuid *)optarg;
561 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
562 argv[0], argv[optind - 1]);
570 memset(¶m, 0, sizeof(param));
571 param.recursive = recursive;
572 param.verbose = verbose;
574 param.obduuid = obduuid;
575 param.maxdepth = recursive ? -1 : 1;
578 rc = llapi_getstripe(argv[optind], ¶m);
579 } while (++optind < argc && !rc);
582 fprintf(stderr, "error: %s failed for %s.\n",
583 argv[0], argv[optind - 1]);
587 static int lfs_osts(int argc, char **argv)
590 struct mntent *mnt = NULL;
591 struct obd_uuid *obduuid = NULL;
592 struct find_param param;
598 fp = setmntent(MOUNTED, "r");
601 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
605 memset(¶m, 0, sizeof(param));
606 param.obduuid = obduuid;
607 while (feof(fp) == 0 && ferror(fp) ==0) {
608 if (llapi_is_lustre_mnt(mnt)) {
609 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
612 "error: %s: failed on %s\n",
613 argv[0], mnt->mnt_dir);
623 #define COOK(value) \
626 while (value > 1024) { \
640 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
642 char rpath[PATH_MAX] = {'\0'};
644 int rc, len, out_len = 0;
646 if (!realpath(path, rpath)) {
648 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
649 path, strerror(-rc));
655 while (feof(fp) == 0 && ferror(fp) == 0) {
656 if (llapi_is_lustre_mnt(mnt)) {
657 len = strlen(mnt->mnt_dir);
659 !strncmp(rpath, mnt->mnt_dir, len)) {
661 memset(mntdir, 0, dir_len);
662 strncpy(mntdir, mnt->mnt_dir, dir_len);
671 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
675 static int showdf(char *mntdir, struct obd_statfs *stat,
676 char *uuid, int ishow, int cooked,
677 char *type, int index, int rc)
679 long long avail, used, total;
681 char *suffix = "KMGTPEZY";
682 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
690 avail = stat->os_ffree;
691 used = stat->os_files - stat->os_ffree;
692 total = stat->os_files;
694 int shift = cooked ? 0 : 10;
696 avail = (stat->os_bavail * stat->os_bsize) >> shift;
697 used = stat->os_blocks - stat->os_bavail;
698 used = (used * stat->os_bsize) >> shift;
699 total = (stat->os_blocks * stat->os_bsize) >> shift;
703 ratio = (double)used / (double)total;
709 cook_val = (double)total;
712 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
714 sprintf(tbuf, CDF, total);
716 cook_val = (double)used;
719 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
721 sprintf(ubuf, CDF, used);
723 cook_val = (double)avail;
726 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
728 sprintf(abuf, CDF, avail);
730 sprintf(tbuf, CDF, total);
731 sprintf(ubuf, CDF, used);
732 sprintf(abuf, CDF, avail);
735 sprintf(rbuf, RDF, (int)(ratio * 100));
736 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
737 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
739 printf("[%s:%d]\n", type, index);
745 printf(UUF": inactive device\n", uuid);
748 printf(UUF": %s\n", uuid, strerror(-rc));
755 static int mntdf(char *mntdir, int ishow, int cooked)
757 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
758 struct obd_uuid uuid_buf;
763 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
764 "UUID", "Inodes", "IUsed", "IFree",
765 "IUse%", "Mounted on");
767 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
768 "UUID", cooked ? "bytes" : "1K-blocks",
769 "Used", "Available", "Use%", "Mounted on");
771 for (index = 0; ; index++) {
772 memset(&stat_buf, 0, sizeof(struct obd_statfs));
773 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
774 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
775 &stat_buf, &uuid_buf);
782 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
783 rc == -ENODATA || rc == 0) {
784 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
788 "error: llapi_obd_statfs(%s): %s (%d)\n",
789 uuid_buf.uuid, strerror(-rc), rc);
793 sum.os_ffree += stat_buf.os_ffree;
794 sum.os_files += stat_buf.os_files;
798 for (index = 0; ; index++) {
799 memset(&stat_buf, 0, sizeof(struct obd_statfs));
800 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
801 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
802 &stat_buf, &uuid_buf);
809 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
810 rc == -ENODATA || rc == 0) {
811 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
815 "error: llapi_obd_statfs failed: %s (%d)\n",
820 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
821 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
822 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
827 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
832 static int lfs_df(int argc, char **argv)
836 struct mntent *mnt = NULL;
837 char mntdir[PATH_MAX] = {'\0'};
838 int ishow = 0, cooked = 0;
842 while ((c = getopt(argc, argv, "ih")) != -1) {
857 fp = setmntent(MOUNTED, "r");
860 fprintf(stderr, "error: %s: open %s failed( %s )\n",
861 argv[0], MOUNTED, strerror(errno));
865 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
871 rc = mntdf(mntdir, ishow, cooked);
876 while (feof(fp) == 0 && ferror(fp) == 0) {
877 if (llapi_is_lustre_mnt(mnt)) {
878 rc = mntdf(mnt->mnt_dir, ishow, cooked);
891 static int lfs_check(int argc, char **argv)
895 struct mntent *mnt = NULL;
904 obd_types[0] = obd_type1;
905 obd_types[1] = obd_type2;
907 if (strcmp(argv[1], "osts") == 0) {
908 strcpy(obd_types[0], "osc");
909 } else if (strcmp(argv[1], "mds") == 0) {
910 strcpy(obd_types[0], "mdc");
911 } else if (strcmp(argv[1], "servers") == 0) {
913 strcpy(obd_types[0], "osc");
914 strcpy(obd_types[1], "mdc");
916 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
921 fp = setmntent(MOUNTED, "r");
923 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
927 while (feof(fp) == 0 && ferror(fp) ==0) {
928 if (llapi_is_lustre_mnt(mnt))
936 fprintf(stderr, "No suitable Lustre mount found\n");
940 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
943 fprintf(stderr, "error: %s: %s status failed\n",
950 static int lfs_catinfo(int argc, char **argv)
953 struct mntent *mnt = NULL;
956 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
959 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
962 fp = setmntent(MOUNTED, "r");
964 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
968 while (feof(fp) == 0 && ferror(fp) == 0) {
969 if (llapi_is_lustre_mnt(mnt))
978 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
980 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
982 fprintf(stderr, "no lustre_lite mounted.\n");
989 int lfs_join(int argc, char **argv)
991 char *name_head, *name_tail;
998 fd = open(name_head, O_WRONLY);
1000 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1004 size = lseek(fd, 0, SEEK_END);
1005 if (size % JOIN_FILE_ALIGN) {
1006 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1007 name_head, (long long)size, JOIN_FILE_ALIGN);
1011 name_tail = argv[2];
1012 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1016 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1022 #ifdef HAVE_QUOTA_SUPPORT
1023 static int lfs_quotachown(int argc, char **argv)
1029 while ((c = getopt(argc, argv, "i")) != -1) {
1035 fprintf(stderr, "error: %s: option '-%c' "
1036 "unrecognized\n", argv[0], c);
1042 rc = llapi_quotachown(argv[optind], flag);
1044 fprintf(stderr,"error: change file owner/group failed.\n");
1049 static int lfs_quotacheck(int argc, char **argv)
1051 int c, check_type = 0;
1053 struct if_quotacheck qchk;
1054 struct if_quotactl qctl;
1055 char *obd_type = qchk.obd_type;
1056 char *obd_uuid = qchk.obd_uuid.uuid;
1059 memset(&qchk, 0, sizeof(qchk));
1062 while ((c = getopt(argc, argv, "ug")) != -1) {
1071 fprintf(stderr, "error: %s: option '-%c' "
1072 "unrecognized\n", argv[0], c);
1079 else /* do quotacheck for both user & group quota by default */
1087 memset(&qctl, 0, sizeof(qctl));
1088 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1089 qctl.qc_id = QFMT_LDISKFS;
1090 qctl.qc_type = check_type;
1091 rc = llapi_quotactl(mnt, &qctl);
1093 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1097 rc = llapi_quotacheck(mnt, check_type);
1099 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1103 rc = llapi_poll_quotacheck(mnt, &qchk);
1106 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1107 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1111 memset(&qctl, 0, sizeof(qctl));
1112 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1113 qctl.qc_id = QFMT_LDISKFS;
1114 qctl.qc_type = check_type;
1115 rc = llapi_quotactl(mnt, &qctl);
1118 fprintf(stderr, "%s %s ",
1119 qctl.obd_type, qctl.obd_uuid.uuid);
1120 fprintf(stderr, "%s turn on quota failed: %s\n",
1121 argv[0], strerror(errno));
1128 static int lfs_quotaon(int argc, char **argv)
1132 struct if_quotactl qctl;
1133 char *obd_type = qctl.obd_type;
1134 char *obd_uuid = qctl.obd_uuid.uuid;
1137 memset(&qctl, 0, sizeof(qctl));
1138 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1139 qctl.qc_id = QFMT_LDISKFS;
1142 while ((c = getopt(argc, argv, "ugf")) != -1) {
1145 qctl.qc_type |= 0x01;
1148 qctl.qc_type |= 0x02;
1151 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1154 fprintf(stderr, "error: %s: option '-%c' "
1155 "unrecognized\n", argv[0], c);
1168 rc = llapi_quotactl(mnt, &qctl);
1171 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1172 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1179 static int lfs_quotaoff(int argc, char **argv)
1183 struct if_quotactl qctl;
1184 char *obd_type = qctl.obd_type;
1185 char *obd_uuid = qctl.obd_uuid.uuid;
1188 memset(&qctl, 0, sizeof(qctl));
1189 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1192 while ((c = getopt(argc, argv, "ug")) != -1) {
1195 qctl.qc_type |= 0x01;
1198 qctl.qc_type |= 0x02;
1201 fprintf(stderr, "error: %s: option '-%c' "
1202 "unrecognized\n", argv[0], c);
1215 rc = llapi_quotactl(mnt, &qctl);
1218 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1219 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1226 static int name2id(unsigned int *id, char *name, int type)
1228 if (type == USRQUOTA) {
1229 struct passwd *entry;
1231 if (!(entry = getpwnam(name))) {
1237 *id = entry->pw_uid;
1239 struct group *entry;
1241 if (!(entry = getgrnam(name))) {
1247 *id = entry->gr_gid;
1253 static int id2name(char **name, unsigned int id, int type)
1255 if (type == USRQUOTA) {
1256 struct passwd *entry;
1258 if (!(entry = getpwuid(id))) {
1264 *name = entry->pw_name;
1266 struct group *entry;
1268 if (!(entry = getgrgid(id))) {
1274 *name = entry->gr_name;
1280 #define ARG2INT(nr, str, msg) \
1283 nr = strtol(str, &endp, 0); \
1285 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1290 int lfs_setquota(int argc, char **argv)
1294 struct if_quotactl qctl;
1295 char *obd_type = qctl.obd_type;
1296 char *obd_uuid = qctl.obd_uuid.uuid;
1299 memset(&qctl, 0, sizeof(qctl));
1300 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1303 while ((c = getopt(argc, argv, "ugt")) != -1) {
1306 qctl.qc_type |= 0x01;
1309 qctl.qc_type |= 0x02;
1312 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1315 fprintf(stderr, "error: %s: option '-%c' "
1316 "unrecognized\n", argv[0], c);
1324 if (qctl.qc_type == UGQUOTA) {
1325 fprintf(stderr, "error: user and group quotas can't be set "
1330 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1331 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1333 if (optind + 6 != argc)
1336 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1338 fprintf(stderr, "error: find id for name %s failed: %s\n",
1339 argv[optind - 1], strerror(errno));
1343 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1344 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1345 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1346 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1348 dqb->dqb_valid = QIF_LIMITS;
1350 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1352 if (optind + 3 != argc)
1355 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1356 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1361 rc = llapi_quotactl(mnt, &qctl);
1364 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1365 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1372 static inline char *type2name(int check_type)
1374 if (check_type == USRQUOTA)
1376 else if (check_type == GRPQUOTA)
1383 static void grace2str(time_t seconds,char *buf)
1385 uint minutes, hours, days;
1387 minutes = (seconds + 30) / 60;
1388 hours = minutes / 60;
1393 snprintf(buf, 40, "%ddays", days);
1395 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1399 static void diff2str(time_t seconds, char *buf, time_t now)
1405 if (seconds <= now) {
1406 strcpy(buf, "none");
1409 grace2str(seconds - now, buf);
1412 static void print_quota_title(char *name, struct if_quotactl *qctl)
1414 printf("Disk quotas for %s %s (%cid %u):\n",
1415 type2name(qctl->qc_type), name,
1416 *type2name(qctl->qc_type), qctl->qc_id);
1417 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1419 "blocks", "quota", "limit", "grace",
1420 "files", "quota", "limit", "grace");
1423 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1429 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1430 int bover = 0, iover = 0;
1431 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1433 if (dqb->dqb_bhardlimit &&
1434 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1436 } else if (dqb->dqb_bsoftlimit &&
1437 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1438 if (dqb->dqb_btime > now) {
1445 if (dqb->dqb_ihardlimit &&
1446 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1448 } else if (dqb->dqb_isoftlimit &&
1449 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1450 if (dqb->dqb_btime > now) {
1457 #if 0 /* XXX: always print quotas even when no usages */
1458 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1464 if (strlen(mnt) > 15)
1465 printf("%s\n%15s", mnt, "");
1467 printf("%15s", mnt);
1470 diff2str(dqb->dqb_btime, timebuf, now);
1472 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1473 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1474 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1475 printf(" %7s%c %6s %7s %7s",
1476 numbuf[0], bover ? '*' : ' ', numbuf[1],
1477 numbuf[2], bover > 1 ? timebuf : "");
1480 diff2str(dqb->dqb_itime, timebuf, now);
1482 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1483 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1484 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1486 printf(" %7s%c %6s %7s %7s",
1487 numbuf[0], iover ? '*' : ' ', numbuf[1],
1488 numbuf[2], iover > 1 ? timebuf : "");
1491 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1492 qctl->qc_cmd == Q_GETOINFO) {
1496 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1497 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1498 printf("Block grace time: %s; Inode grace time: %s\n",
1499 bgtimebuf, igtimebuf);
1503 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1507 /* XXX: this is a flag to mark that only mds quota is wanted */
1508 qctl->qc_dqblk.dqb_valid = 1;
1509 rc = llapi_quotactl(mnt, qctl);
1511 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1514 qctl->qc_dqblk.dqb_valid = 0;
1516 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1519 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1522 struct obd_uuid uuids[1024], *uuidp;
1523 int obdcount = 1024;
1528 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1532 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1534 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1538 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1539 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1541 /* XXX clear this flag to get quota from osts */
1542 qctl->qc_dqblk.dqb_valid = 0;
1543 rc = llapi_quotactl(mnt, qctl);
1545 fprintf(stderr, "%s quotactl failed: %s\n",
1546 uuidp->uuid, strerror(errno));
1550 print_quota(uuidp->uuid, qctl, 1);
1558 static int lfs_quota(int argc, char **argv)
1561 char *name = NULL, *mnt;
1562 struct if_quotactl qctl;
1563 char *obd_type = qctl.obd_type;
1564 char *obd_uuid = qctl.obd_uuid.uuid;
1567 memset(&qctl, 0, sizeof(qctl));
1568 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1571 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1574 qctl.qc_type |= 0x01;
1577 qctl.qc_type |= 0x02;
1580 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1583 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1586 fprintf(stderr, "error: %s: option '-%c' "
1587 "unrecognized\n", argv[0], c);
1595 if (qctl.qc_type == UGQUOTA) {
1596 fprintf(stderr, "error: user or group can't be specified"
1601 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1602 if (optind + 2 != argc)
1605 name = argv[optind++];
1606 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1608 fprintf(stderr, "error: find id for name %s failed: %s\n",
1609 name, strerror(errno));
1612 print_quota_title(name, &qctl);
1613 } else if (optind + 1 != argc) {
1619 rc = llapi_quotactl(mnt, &qctl);
1622 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1623 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1628 rc = id2name(&name, getuid(), qctl.qc_type);
1635 print_quota(mnt, &qctl, 0);
1637 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1638 print_mds_quota(mnt, &qctl);
1639 print_lov_quota(mnt, &qctl);
1644 #endif /* HAVE_QUOTA_SUPPORT */
1646 static int flushctx_ioctl(char *mp)
1650 fd = open(mp, O_RDONLY);
1652 fprintf(stderr, "flushctx: error open %s: %s\n",
1653 mp, strerror(errno));
1657 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1659 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1660 mp, strerror(errno));
1666 static int lfs_flushctx(int argc, char **argv)
1668 int kdestroy = 0, c;
1670 char procline[PATH_MAX], *line;
1674 while ((c = getopt(argc, argv, "k")) != -1) {
1680 fprintf(stderr, "error: %s: option '-%c' "
1681 "unrecognized\n", argv[0], c);
1687 system("kdestroy > /dev/null");
1689 if (optind >= argc) {
1690 /* flush for all mounted lustre fs. */
1691 proc = fopen("/proc/mounts", "r");
1693 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1698 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1703 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1704 fprintf(stderr, "%s: unexpected format in "
1710 if (strcmp(fs, "lustre") != 0)
1712 /* we use '@' to determine it's a client. are there
1713 * any other better way?
1715 if (strchr(dev, '@') == NULL)
1718 if (flushctx_ioctl(mp))
1722 /* flush fs as specified */
1723 while (optind < argc) {
1724 if (flushctx_ioctl(argv[optind++]))
1733 * We assume one and only one filename is supplied as the
1736 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1738 char *dname, *rpath = NULL;
1739 char path[PATH_MAX], cwd[PATH_MAX];
1747 /* FIXME the premise is there is no sub-mounted filesystems under this
1748 * mounted lustre tree. */
1749 strncpy(fname, argv[argc - 1], PATH_MAX);
1751 /* get path prefix */
1752 dname = dirname(fname);
1754 /* try to resolve the pathname into relative to the root of the mounted
1755 * lustre filesystem.
1757 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1758 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1762 if (chdir(dname) == -1) {
1763 fprintf(stderr, "chdir to %s failed: %s\n",
1764 dname, strerror(errno));
1768 if (getcwd(path, sizeof(path)) == NULL) {
1769 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1773 if (chdir(cwd) == -1) {
1774 fprintf(stderr, "chdir back to %s: %s\n",
1775 cwd, strerror(errno));
1779 strncat(path, "/", PATH_MAX);
1780 strncpy(fname, argv[argc - 1], PATH_MAX);
1781 strncat(path, basename(fname), PATH_MAX);
1783 fp = setmntent(MOUNTED, "r");
1785 fprintf(stderr, "setmntent %s failed: %s\n",
1786 MOUNTED, strerror(errno));
1791 mnt = getmntent(fp);
1795 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
1798 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1799 rpath = path + strlen(mnt->mnt_dir);
1805 /* remove char '/' from rpath to be a relative path */
1806 while (rpath && *rpath == '/') rpath++;
1810 "%s: file %s doesn't belong to a lustre file system!\n",
1811 argv[0], argv[argc - 1]);
1815 for (i = 0; i < argc - 1; i++) {
1816 strncat(cmd, argv[i], PATH_MAX);
1817 strncat(cmd, " ", PATH_MAX);
1819 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1820 strncpy(fname, argv[argc - 1], sizeof(fname));
1825 static int lfs_getfacl(int argc, char **argv)
1827 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1829 if (acl_cmd_parse(argc, argv, fname, cmd))
1832 return llapi_getfacl(fname, cmd);
1835 static int lfs_setfacl(int argc, char **argv)
1837 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1839 if (acl_cmd_parse(argc, argv, fname, cmd))
1842 return llapi_setfacl(fname, cmd);
1845 int main(int argc, char **argv)
1851 ptl_initialize(argc, argv);
1852 if (obd_initialize(argc, argv) < 0)
1854 if (dbg_initialize(argc, argv) < 0)
1857 Parser_init("lfs > ", cmdlist);
1860 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1862 rc = Parser_commands();
1865 obd_finalize(argc, argv);