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_lsetfacl(int argc, char **argv);
75 static int lfs_lgetfacl(int argc, char **argv);
76 static int lfs_rsetfacl(int argc, char **argv);
77 static int lfs_rgetfacl(int argc, char **argv);
78 static int lfs_cp(int argc, char **argv);
79 static int lfs_ls(int argc, char **argv);
81 /* all avaialable commands */
82 command_t cmdlist[] = {
83 {"setstripe", lfs_setstripe, 0,
84 "Create a new file with a specific striping pattern or\n"
85 "set the default striping pattern on an existing directory or\n"
86 "delete the default striping pattern from an existing directory\n"
87 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
89 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
90 " [--index|-i stripe_index]\n"
91 " [--count|-c stripe_count]\n"
93 " setstripe -d <dirname> (to delete default striping)\n"
94 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
95 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
96 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
97 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
98 {"getstripe", lfs_getstripe, 0,
99 "To list the striping info for a given filename or files in a\n"
100 "directory or recursively for all files in a directory tree.\n"
101 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
102 " [--recursive | -r] <dir|file> ..."},
103 {"find", lfs_find, 0,
104 "To find files that match given parameters recursively in a directory tree.\n"
105 "usage: find <dir/file> ... \n"
106 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
107 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
108 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
109 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
110 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
111 "\t !: used before an option indicates 'NOT' the requested attribute\n"
112 "\t -: used before an value indicates 'AT MOST' the requested value\n"
113 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
114 {"check", lfs_check, 0,
115 "Display the status of MDS or OSTs (as specified in the command)\n"
116 "or all the servers (MDS and OSTs).\n"
117 "usage: check <osts|mds|servers>"},
118 {"catinfo", lfs_catinfo, 0,
119 "Show information of specified type logs.\n"
120 "usage: catinfo {keyword} [node name]\n"
121 "\tkeywords are one of followings: config, deletions.\n"
122 "\tnode name must be provided when use keyword config."},
123 {"join", lfs_join, 0,
124 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
125 "usage: join <filename_A> <filename_B>\n"},
126 {"osts", lfs_osts, 0, "osts"},
128 "report filesystem disk space usage or inodes usage"
130 "Usage: df [-i] [-h] [path]"},
131 #ifdef HAVE_QUOTA_SUPPORT
132 {"quotachown",lfs_quotachown, 0,
133 "Change files' owner or group on the specified filesystem.\n"
134 "usage: quotachown [-i] <filesystem>\n"
135 "\t-i: ignore error if file is not exist\n"},
136 {"quotacheck", lfs_quotacheck, 0,
137 "Scan the specified filesystem for disk usage, and create,\n"
138 "or update quota files.\n"
139 "usage: quotacheck [ -ug ] <filesystem>"},
140 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
141 "usage: quotaon [ -ugf ] <filesystem>"},
142 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
143 "usage: quotaoff [ -ug ] <filesystem>"},
144 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
145 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
146 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
147 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
148 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
150 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
151 "usage: flushctx [-k] [mountpoint...]"},
152 {"lsetfacl", lfs_lsetfacl, 0,
153 "Remote user setfacl for user/group on the same remote client.\n"
154 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
155 {"lgetfacl", lfs_lgetfacl, 0,
156 "Remote user getfacl for user/group on the same remote client.\n"
157 "usage: lgetfacl [-dRLPvh] file ..."},
158 {"rsetfacl", lfs_rsetfacl, 0,
159 "Remote user setfacl for user/group on other clients.\n"
160 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
161 {"rgetfacl", lfs_rgetfacl, 0,
162 "Remote user getfacl for user/group on other clients.\n"
163 "usage: rgetfacl [-dRLPvh] file ..."},
165 "Remote user copy files and directories.\n"
166 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
168 "Remote user list directory contents.\n"
169 "usage: ls [OPTION]... [FILE]..."},
170 {"help", Parser_help, 0, "help"},
171 {"exit", Parser_quit, 0, "quit"},
172 {"quit", Parser_quit, 0, "quit"},
177 static int lfs_setstripe(int argc, char **argv)
181 unsigned long long st_size;
182 int st_offset, st_count;
186 char *stripe_size_arg = NULL;
187 char *stripe_off_arg = NULL;
188 char *stripe_count_arg = NULL;
189 unsigned long long size_units;
191 struct option long_opts[] = {
192 {"size", required_argument, 0, 's'},
193 {"count", required_argument, 0, 'c'},
194 {"index", required_argument, 0, 'i'},
195 {"delete", no_argument, 0, 'd'},
202 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
203 /* for compatibility with the existing positional parameter
207 } else if (argc == 5 &&
208 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
209 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
210 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
211 /* for compatibility with the existing positional parameter
214 stripe_size_arg = argv[2];
215 stripe_off_arg = argv[3];
216 stripe_count_arg = argv[4];
220 while ((c = getopt_long(argc, argv, "c:di:s:",
221 long_opts, NULL)) >= 0) {
227 stripe_count_arg = optarg;
230 /* delete the default striping pattern */
234 stripe_off_arg = optarg;
237 stripe_size_arg = optarg;
242 fprintf(stderr, "error: %s: option '%s' "
244 argv[0], argv[optind - 1]);
249 fname = argv[optind];
255 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
256 stripe_count_arg != NULL)) {
257 fprintf(stderr, "error: %s: cannot specify -d with "
258 "-s, -c or -i options\n",
263 if (optind != argc - 1) {
264 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
266 argv[0], argv[argc - 1]);
270 /* get the stripe size */
271 if (stripe_size_arg != NULL) {
272 result = parse_size(stripe_size_arg, &st_size, &size_units);
274 fprintf(stderr,"error: bad size '%s'\n",
279 /* get the stripe offset */
280 if (stripe_off_arg != NULL) {
281 st_offset = strtoul(stripe_off_arg, &end, 0);
283 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
284 argv[0], stripe_off_arg);
288 /* get the stripe count */
289 if (stripe_count_arg != NULL) {
290 st_count = strtoul(stripe_count_arg, &end, 0);
292 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
293 argv[0], stripe_count_arg);
298 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
300 fprintf(stderr, "error: %s: create stripe file failed\n",
306 static int set_time(time_t *time, time_t *set, char *str)
313 else if (str[0] == '-')
319 t = strtol(str, NULL, 0);
320 if (*time < t * 24 * 60 * 60) {
323 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
327 *set = *time - t * 24 * 60 * 60;
331 static int name2id(unsigned int *id, char *name, int type)
333 if (type == USRQUOTA) {
334 struct passwd *entry;
336 if (!(entry = getpwnam(name))) {
346 if (!(entry = getgrnam(name))) {
358 static int id2name(char **name, unsigned int id, int type)
360 if (type == USRQUOTA) {
361 struct passwd *entry;
363 if (!(entry = getpwuid(id))) {
369 *name = entry->pw_name;
373 if (!(entry = getgrgid(id))) {
379 *name = entry->gr_name;
385 static int lfs_find(int argc, char **argv)
390 struct find_param param = { .maxdepth = -1 };
392 struct option long_opts[] = {
393 /* New find options. */
394 {"atime", required_argument, 0, 'A'},
395 {"ctime", required_argument, 0, 'C'},
396 {"mtime", required_argument, 0, 'M'},
397 {"maxdepth", required_argument, 0, 'D'},
398 {"gid", required_argument, 0, 'g'},
399 {"group", required_argument, 0, 'G'},
400 {"uid", required_argument, 0, 'u'},
401 {"user", required_argument, 0, 'U'},
402 {"name", required_argument, 0, 'n'},
403 /* --obd is considered as a new option. */
404 {"obd", required_argument, 0, 'O'},
405 {"ost", required_argument, 0, 'O'},
406 {"print", no_argument, 0, 'P'},
407 {"print0", no_argument, 0, 'p'},
408 /* Old find options. */
409 {"quiet", no_argument, 0, 'q'},
410 {"recursive", no_argument, 0, 'r'},
411 {"size", required_argument, 0, 's'},
412 {"type", required_argument, 0, 't'},
413 {"verbose", no_argument, 0, 'v'},
427 while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
428 long_opts, NULL)) >= 0) {
433 /* '!' is part of option */
434 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
435 if (!isoption && pathend != -1) {
436 fprintf(stderr, "err: %s: filename|dirname must either "
437 "precede options or follow options\n",
441 if (!isoption && pathstart == -1)
442 pathstart = optind - 1;
443 if (isoption && pathstart != -1 && pathend == -1) {
444 pathend = optind - 2;
445 if ((c == 1 && strcmp(optarg, "!") == 0) ||
446 c == 'P' || c == 'p' || c == 'O' ||
447 c == 'q' || c == 'r' || c == 'v')
448 pathend = optind - 1;
455 if (strcmp(optarg, "!") == 0)
459 xtime = ¶m.atime;
460 xsign = ¶m.asign;
463 xtime = ¶m.ctime;
464 xsign = ¶m.csign;
468 xtime = ¶m.mtime;
469 xsign = ¶m.msign;
473 if (optarg[0] == '-')
475 else if (optarg[0] == '+')
484 ret = set_time(&t, xtime, optarg);
492 param.maxdepth = strtol(optarg, 0, 0);
496 param.gid = strtol(optarg, &endptr, 10);
497 if (optarg == endptr) {
498 fprintf(stderr, "Bad gid: %s\n", optarg);
501 param.exclude_gid = !!neg_opt;
506 ret = name2id(¶m.gid, optarg, GRPQUOTA);
508 fprintf(stderr, "Group: %s cannot be found.\n", optarg);
511 param.exclude_gid = !!neg_opt;
516 param.uid = strtol(optarg, &endptr, 10);
517 if (optarg == endptr) {
518 fprintf(stderr, "Bad uid: %s\n", optarg);
521 param.exclude_uid = !!neg_opt;
526 ret = name2id(¶m.uid, optarg, USRQUOTA);
528 fprintf(stderr, "User: %s cannot be found.\n", optarg);
531 param.exclude_uid = !!neg_opt;
536 param.pattern = (char *)optarg;
537 param.exclude_pattern = !!neg_opt;
540 char *buf, *token, *next, *p;
543 len = strlen((char *)optarg);
547 strcpy(buf, (char *)optarg);
549 if (param.num_alloc_obds == 0) {
550 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
551 sizeof(struct obd_uuid));
552 if (param.obduuid == NULL)
554 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
557 for (token = buf; token && *token; token = next) {
558 p = strchr(token, ',');
564 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
588 param.exclude_type = !!neg_opt;
590 case 'b': param.type = S_IFBLK; break;
591 case 'c': param.type = S_IFCHR; break;
592 case 'd': param.type = S_IFDIR; break;
593 case 'f': param.type = S_IFREG; break;
594 case 'l': param.type = S_IFLNK; break;
595 case 'p': param.type = S_IFIFO; break;
596 case 's': param.type = S_IFSOCK; break;
597 #ifdef S_IFDOOR /* Solaris only */
598 case 'D': param.type = S_IFDOOR; break;
600 default: fprintf(stderr, "error: %s: bad type '%s'\n",
607 if (optarg[0] == '-')
609 else if (optarg[0] == '+')
618 if (optarg[0] == '+')
619 param.size_sign = -1;
620 else if (optarg[0] == '-')
621 param.size_sign = +1;
625 ret = parse_size(optarg, ¶m.size,¶m.size_units);
627 fprintf(stderr,"error: bad size '%s'\n",
640 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
641 argv[0], argv[optind - 1]);
646 if (pathstart == -1) {
647 fprintf(stderr, "error: %s: no filename|pathname\n",
650 } else if (pathend == -1) {
658 static int deprecated_warning;
659 if (!deprecated_warning) {
660 fprintf(stderr, "lfs find: -q, -r, -v options "
661 "deprecated. Use 'lfs getstripe' instead.\n");
662 deprecated_warning = 1;
664 if (!param.recursive && param.maxdepth == -1)
670 ret = llapi_find(argv[pathstart], ¶m);
672 ret = llapi_getstripe(argv[pathstart], ¶m);
673 } while (++pathstart < pathend && !ret);
676 fprintf(stderr, "error: %s failed for %s.\n",
677 argv[0], argv[optind - 1]);
679 if (param.obduuid && param.num_alloc_obds)
685 static int lfs_getstripe(int argc, char **argv)
687 struct option long_opts[] = {
689 {"quiet", 0, 0, 'q'},
690 {"recursive", 0, 0, 'r'},
691 {"verbose", 0, 0, 'v'},
694 char short_opts[] = "hO:qrv";
696 struct find_param param = { 0 };
699 while ((c = getopt_long(argc, argv, short_opts,
700 long_opts, NULL)) != -1) {
705 "error: %s: only one obduuid allowed",
709 param.obduuid = (struct obd_uuid *)optarg;
725 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
726 argv[0], argv[optind - 1]);
734 param.maxdepth = param.recursive ? -1 : 1;
737 rc = llapi_getstripe(argv[optind], ¶m);
738 } while (++optind < argc && !rc);
741 fprintf(stderr, "error: %s failed for %s.\n",
742 argv[0], argv[optind - 1]);
746 static int lfs_osts(int argc, char **argv)
749 struct mntent *mnt = NULL;
750 struct find_param param;
756 fp = setmntent(MOUNTED, "r");
759 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
763 while (feof(fp) == 0 && ferror(fp) ==0) {
764 memset(¶m, 0, sizeof(param));
765 if (llapi_is_lustre_mnt(mnt)) {
766 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
769 "error: %s: failed on %s\n",
770 argv[0], mnt->mnt_dir);
780 #define COOK(value) \
783 while (value > 1024) { \
797 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
799 char rpath[PATH_MAX] = {'\0'};
801 int rc, len, out_len = 0;
803 if (!realpath(path, rpath)) {
805 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
806 path, strerror(-rc));
812 while (feof(fp) == 0 && ferror(fp) == 0) {
813 if (llapi_is_lustre_mnt(mnt)) {
814 len = strlen(mnt->mnt_dir);
816 !strncmp(rpath, mnt->mnt_dir, len)) {
818 memset(mntdir, 0, dir_len);
819 strncpy(mntdir, mnt->mnt_dir, dir_len);
828 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
832 static int showdf(char *mntdir, struct obd_statfs *stat,
833 char *uuid, int ishow, int cooked,
834 char *type, int index, int rc)
836 long long avail, used, total;
838 char *suffix = "KMGTPEZY";
839 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
840 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
848 avail = stat->os_ffree;
849 used = stat->os_files - stat->os_ffree;
850 total = stat->os_files;
852 int shift = cooked ? 0 : 10;
854 avail = (stat->os_bavail * stat->os_bsize) >> shift;
855 used = ((stat->os_blocks - stat->os_bfree) *
856 stat->os_bsize) >> shift;
857 total = (stat->os_blocks * stat->os_bsize) >> shift;
861 ratio = (double)used / (double)total;
867 cook_val = (double)total;
870 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
872 sprintf(tbuf, CDF, total);
874 cook_val = (double)used;
877 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
879 sprintf(ubuf, CDF, used);
881 cook_val = (double)avail;
884 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
886 sprintf(abuf, CDF, avail);
888 sprintf(tbuf, CDF, total);
889 sprintf(ubuf, CDF, used);
890 sprintf(abuf, CDF, avail);
893 sprintf(rbuf, RDF, (int)(ratio * 100));
894 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
895 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
897 printf("[%s:%d]\n", type, index);
903 printf(UUF": inactive device\n", uuid);
906 printf(UUF": %s\n", uuid, strerror(-rc));
913 static int mntdf(char *mntdir, int ishow, int cooked)
915 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
916 struct obd_uuid uuid_buf;
921 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
922 "UUID", "Inodes", "IUsed", "IFree",
923 "IUse%", "Mounted on");
925 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
926 "UUID", cooked ? "bytes" : "1K-blocks",
927 "Used", "Available", "Use%", "Mounted on");
929 for (index = 0; ; index++) {
930 memset(&stat_buf, 0, sizeof(struct obd_statfs));
931 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
932 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
933 &stat_buf, &uuid_buf);
940 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
941 rc == -ENODATA || rc == 0) {
942 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
946 "error: llapi_obd_statfs(%s): %s (%d)\n",
947 uuid_buf.uuid, strerror(-rc), rc);
951 sum.os_ffree += stat_buf.os_ffree;
952 sum.os_files += stat_buf.os_files;
956 for (index = 0; ; index++) {
957 memset(&stat_buf, 0, sizeof(struct obd_statfs));
958 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
959 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
960 &stat_buf, &uuid_buf);
967 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
968 rc == -ENODATA || rc == 0) {
969 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
973 "error: llapi_obd_statfs failed: %s (%d)\n",
978 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
979 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
980 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
985 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
990 static int lfs_df(int argc, char **argv)
994 struct mntent *mnt = NULL;
996 int ishow = 0, cooked = 0;
1000 while ((c = getopt(argc, argv, "ih")) != -1) {
1013 path = argv[optind];
1015 fp = setmntent(MOUNTED, "r");
1018 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1019 argv[0], MOUNTED, strerror(errno));
1023 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1024 fprintf(stderr, "error: cannot allocate %d bytes\n",
1028 memset(mntdir, 0, PATH_MAX);
1031 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1038 rc = mntdf(mntdir, ishow, cooked);
1042 mnt = getmntent(fp);
1043 while (feof(fp) == 0 && ferror(fp) == 0) {
1044 if (llapi_is_lustre_mnt(mnt)) {
1045 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1050 mnt = getmntent(fp);
1059 static int lfs_check(int argc, char **argv)
1063 struct mntent *mnt = NULL;
1072 obd_types[0] = obd_type1;
1073 obd_types[1] = obd_type2;
1075 if (strcmp(argv[1], "osts") == 0) {
1076 strcpy(obd_types[0], "osc");
1077 } else if (strcmp(argv[1], "mds") == 0) {
1078 strcpy(obd_types[0], "mdc");
1079 } else if (strcmp(argv[1], "servers") == 0) {
1081 strcpy(obd_types[0], "osc");
1082 strcpy(obd_types[1], "mdc");
1084 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1089 fp = setmntent(MOUNTED, "r");
1091 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1094 mnt = getmntent(fp);
1095 while (feof(fp) == 0 && ferror(fp) ==0) {
1096 if (llapi_is_lustre_mnt(mnt))
1098 mnt = getmntent(fp);
1104 fprintf(stderr, "No suitable Lustre mount found\n");
1108 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1111 fprintf(stderr, "error: %s: %s status failed\n",
1118 static int lfs_catinfo(int argc, char **argv)
1121 struct mntent *mnt = NULL;
1124 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1127 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1130 fp = setmntent(MOUNTED, "r");
1132 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1135 mnt = getmntent(fp);
1136 while (feof(fp) == 0 && ferror(fp) == 0) {
1137 if (llapi_is_lustre_mnt(mnt))
1139 mnt = getmntent(fp);
1146 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1148 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1150 fprintf(stderr, "no lustre_lite mounted.\n");
1157 int lfs_join(int argc, char **argv)
1159 char *name_head, *name_tail;
1165 name_head = argv[1];
1166 fd = open(name_head, O_WRONLY);
1168 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1172 size = lseek(fd, 0, SEEK_END);
1173 if (size % JOIN_FILE_ALIGN) {
1174 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1175 name_head, (long long)size, JOIN_FILE_ALIGN);
1179 name_tail = argv[2];
1180 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1184 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1190 #ifdef HAVE_QUOTA_SUPPORT
1191 static int lfs_quotachown(int argc, char **argv)
1198 while ((c = getopt(argc, argv, "i")) != -1) {
1204 fprintf(stderr, "error: %s: option '-%c' "
1205 "unrecognized\n", argv[0], c);
1211 rc = llapi_quotachown(argv[optind], flag);
1213 fprintf(stderr,"error: change file owner/group failed.\n");
1218 static int lfs_quotacheck(int argc, char **argv)
1220 int c, check_type = 0;
1222 struct if_quotacheck qchk;
1223 struct if_quotactl qctl;
1224 char *obd_type = qchk.obd_type;
1225 char *obd_uuid = qchk.obd_uuid.uuid;
1228 memset(&qchk, 0, sizeof(qchk));
1231 while ((c = getopt(argc, argv, "ug")) != -1) {
1240 fprintf(stderr, "error: %s: option '-%c' "
1241 "unrecognized\n", argv[0], c);
1248 else /* do quotacheck for both user & group quota by default */
1256 memset(&qctl, 0, sizeof(qctl));
1257 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1258 qctl.qc_id = QFMT_LDISKFS;
1259 qctl.qc_type = check_type;
1260 rc = llapi_quotactl(mnt, &qctl);
1262 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1266 rc = llapi_quotacheck(mnt, check_type);
1268 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1272 rc = llapi_poll_quotacheck(mnt, &qchk);
1275 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1276 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1280 memset(&qctl, 0, sizeof(qctl));
1281 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1282 qctl.qc_id = QFMT_LDISKFS;
1283 qctl.qc_type = check_type;
1284 rc = llapi_quotactl(mnt, &qctl);
1287 fprintf(stderr, "%s %s ",
1288 qctl.obd_type, qctl.obd_uuid.uuid);
1289 fprintf(stderr, "%s turn on quota failed: %s\n",
1290 argv[0], strerror(errno));
1297 static int lfs_quotaon(int argc, char **argv)
1301 struct if_quotactl qctl;
1302 char *obd_type = qctl.obd_type;
1303 char *obd_uuid = qctl.obd_uuid.uuid;
1306 memset(&qctl, 0, sizeof(qctl));
1307 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1308 qctl.qc_id = QFMT_LDISKFS;
1311 while ((c = getopt(argc, argv, "ugf")) != -1) {
1314 qctl.qc_type |= 0x01;
1317 qctl.qc_type |= 0x02;
1320 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1323 fprintf(stderr, "error: %s: option '-%c' "
1324 "unrecognized\n", argv[0], c);
1331 else /* by default, enable quota for both user & group */
1332 qctl.qc_type = 0x02;
1339 rc = llapi_quotactl(mnt, &qctl);
1342 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1343 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1350 static int lfs_quotaoff(int argc, char **argv)
1354 struct if_quotactl qctl;
1355 char *obd_type = qctl.obd_type;
1356 char *obd_uuid = qctl.obd_uuid.uuid;
1359 memset(&qctl, 0, sizeof(qctl));
1360 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1363 while ((c = getopt(argc, argv, "ug")) != -1) {
1366 qctl.qc_type |= 0x01;
1369 qctl.qc_type |= 0x02;
1372 fprintf(stderr, "error: %s: option '-%c' "
1373 "unrecognized\n", argv[0], c);
1380 else /* by default, disable quota for both user & group */
1381 qctl.qc_type = 0x02;
1388 rc = llapi_quotactl(mnt, &qctl);
1391 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1392 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1399 #define ARG2INT(nr, str, msg) \
1402 nr = strtol(str, &endp, 0); \
1404 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1409 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1411 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1412 * returns the value or ULONG_MAX on integer overflow or incorrect format
1414 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1415 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1416 * 3. empty integer value is interpreted as 0
1419 static unsigned long str2sec(const char* timestr) {
1420 const char spec[] = "smhdw";
1421 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1422 unsigned long val = 0;
1425 if (strpbrk(timestr, spec) == NULL) {
1426 /* no specifiers inside the time string,
1427 should treat it as an integer value */
1428 val = strtoul(timestr, &tail, 10);
1429 return *tail ? ULONG_MAX : val;
1432 /* format string is XXwXXdXXhXXmXXs */
1438 v = strtoul(timestr, &tail, 10);
1439 if (v == ULONG_MAX || *tail == '\0')
1440 /* value too large (ULONG_MAX or more)
1441 or missing specifier */
1444 ptr = strchr(spec, *tail);
1446 /* unknown specifier */
1451 /* check if product will overflow the type */
1452 if (!(v < ULONG_MAX / mult[ind]))
1455 ADD_OVERFLOW(val, mult[ind] * v);
1456 if (val == ULONG_MAX)
1468 int lfs_setquota(int argc, char **argv)
1472 struct if_quotactl qctl;
1473 char *obd_type = qctl.obd_type;
1474 char *obd_uuid = qctl.obd_uuid.uuid;
1477 memset(&qctl, 0, sizeof(qctl));
1478 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1481 while ((c = getopt(argc, argv, "ugt")) != -1) {
1484 qctl.qc_type |= 0x01;
1487 qctl.qc_type |= 0x02;
1490 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1493 fprintf(stderr, "error: %s: option '-%c' "
1494 "unrecognized\n", argv[0], c);
1502 if (qctl.qc_type == UGQUOTA) {
1503 fprintf(stderr, "error: user and group quotas can't be set "
1508 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1509 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1511 if (optind + 6 != argc)
1514 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1516 fprintf(stderr, "error: find id for name %s failed: %s\n",
1517 argv[optind - 1], strerror(errno));
1521 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1522 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1523 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1524 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1526 dqb->dqb_valid = QIF_LIMITS;
1528 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1530 if (optind + 3 != argc)
1533 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1534 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1537 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1538 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1545 rc = llapi_quotactl(mnt, &qctl);
1548 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1549 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1556 static inline char *type2name(int check_type)
1558 if (check_type == USRQUOTA)
1560 else if (check_type == GRPQUOTA)
1567 /* Converts seconds value into format string
1568 * result is returned in buf
1570 * 1. result is in descenting order: 1w2d3h4m5s
1571 * 2. zero fields are not filled (except for p. 3): 5d1s
1572 * 3. zero seconds value is presented as "0s"
1574 static void sec2str(time_t seconds, char *buf)
1576 const char spec[] = "smhdw";
1577 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1582 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1583 c = seconds / mult[i];
1585 if (c > 0 || (i == 0 && buf == tail))
1586 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1593 static void diff2str(time_t seconds, char *buf, time_t now)
1599 if (seconds <= now) {
1600 strcpy(buf, "none");
1603 sec2str(seconds - now, buf);
1606 static void print_quota_title(char *name, struct if_quotactl *qctl)
1608 printf("Disk quotas for %s %s (%cid %u):\n",
1609 type2name(qctl->qc_type), name,
1610 *type2name(qctl->qc_type), qctl->qc_id);
1611 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1613 "kbytes", "quota", "limit", "grace",
1614 "files", "quota", "limit", "grace");
1617 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1623 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1624 int bover = 0, iover = 0;
1625 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1627 if (dqb->dqb_bhardlimit &&
1628 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1630 } else if (dqb->dqb_bsoftlimit &&
1631 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1632 if (dqb->dqb_btime > now) {
1639 if (dqb->dqb_ihardlimit &&
1640 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1642 } else if (dqb->dqb_isoftlimit &&
1643 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1644 if (dqb->dqb_btime > now) {
1651 #if 0 /* XXX: always print quotas even when no usages */
1652 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1658 if (strlen(mnt) > 15)
1659 printf("%s\n%15s", mnt, "");
1661 printf("%15s", mnt);
1664 diff2str(dqb->dqb_btime, timebuf, now);
1666 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1667 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1668 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1669 printf(" %7s%c %6s %7s %7s",
1670 numbuf[0], bover ? '*' : ' ', numbuf[1],
1671 numbuf[2], bover > 1 ? timebuf : "");
1674 diff2str(dqb->dqb_itime, timebuf, now);
1676 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1677 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1678 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1680 printf(" %7s%c %6s %7s %7s",
1681 numbuf[0], iover ? '*' : ' ', numbuf[1],
1682 numbuf[2], iover > 1 ? timebuf : "");
1685 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1686 qctl->qc_cmd == Q_GETOINFO) {
1690 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1691 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1692 printf("Block grace time: %s; Inode grace time: %s\n",
1693 bgtimebuf, igtimebuf);
1697 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1701 /* XXX: this is a flag to mark that only mds quota is wanted */
1702 qctl->qc_dqblk.dqb_valid = 1;
1703 rc = llapi_quotactl(mnt, qctl);
1705 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1708 qctl->qc_dqblk.dqb_valid = 0;
1710 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1713 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1716 struct obd_uuid *uuids = NULL, *uuidp;
1717 int obdcount = 1024;
1722 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1726 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1727 sizeof(struct obd_uuid));
1732 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1734 struct obd_uuid *uuids_temp;
1736 if (rc == -EOVERFLOW) {
1737 uuids_temp = realloc(uuids, obdcount *
1738 sizeof(struct obd_uuid));
1739 if (uuids_temp != NULL)
1740 goto retry_get_uuids;
1745 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1749 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1750 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1752 /* XXX clear this flag to get quota from osts */
1753 qctl->qc_dqblk.dqb_valid = 0;
1754 rc = llapi_quotactl(mnt, qctl);
1756 fprintf(stderr, "%s quotactl failed: %s\n",
1757 uuidp->uuid, strerror(errno));
1761 print_quota(uuidp->uuid, qctl, 1);
1769 static int lfs_quota(int argc, char **argv)
1772 char *name = NULL, *mnt;
1773 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1775 char *obd_type = qctl.obd_type;
1776 char *obd_uuid = qctl.obd_uuid.uuid;
1780 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1783 qctl.qc_type = 0x01;
1786 qctl.qc_type = 0x02;
1789 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1792 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1795 fprintf(stderr, "error: %s: option '-%c' "
1796 "unrecognized\n", argv[0], c);
1805 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1806 if (optind + 2 != argc) {
1807 fprintf(stderr, "error: missing quota argument(s)\n");
1811 name = argv[optind++];
1812 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1814 fprintf(stderr,"error: can't find id for name %s: %s\n",
1815 name, strerror(errno));
1818 print_quota_title(name, &qctl);
1819 } else if (optind + 1 != argc) {
1820 fprintf(stderr, "error: missing quota info argument(s)\n");
1826 rc = llapi_quotactl(mnt, &qctl);
1829 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1830 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1835 rc = id2name(&name, getuid(), qctl.qc_type);
1842 print_quota(mnt, &qctl, 0);
1844 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1845 print_mds_quota(mnt, &qctl);
1846 print_lov_quota(mnt, &qctl);
1851 #endif /* HAVE_QUOTA_SUPPORT */
1853 static int flushctx_ioctl(char *mp)
1857 fd = open(mp, O_RDONLY);
1859 fprintf(stderr, "flushctx: error open %s: %s\n",
1860 mp, strerror(errno));
1864 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1866 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1867 mp, strerror(errno));
1873 static int lfs_flushctx(int argc, char **argv)
1875 int kdestroy = 0, c;
1877 char procline[PATH_MAX], *line;
1881 while ((c = getopt(argc, argv, "k")) != -1) {
1887 fprintf(stderr, "error: %s: option '-%c' "
1888 "unrecognized\n", argv[0], c);
1894 system("kdestroy > /dev/null");
1896 if (optind >= argc) {
1897 /* flush for all mounted lustre fs. */
1898 proc = fopen("/proc/mounts", "r");
1900 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1905 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1910 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1911 fprintf(stderr, "%s: unexpected format in "
1917 if (strcmp(fs, "lustre") != 0)
1919 /* we use '@' to determine it's a client. are there
1920 * any other better way?
1922 if (strchr(dev, '@') == NULL)
1925 if (flushctx_ioctl(mp))
1929 /* flush fs as specified */
1930 while (optind < argc) {
1931 if (flushctx_ioctl(argv[optind++]))
1939 static int lfs_lsetfacl(int argc, char **argv)
1942 return(llapi_lsetfacl(argc, argv));
1945 static int lfs_lgetfacl(int argc, char **argv)
1948 return(llapi_lgetfacl(argc, argv));
1951 static int lfs_rsetfacl(int argc, char **argv)
1954 return(llapi_rsetfacl(argc, argv));
1957 static int lfs_rgetfacl(int argc, char **argv)
1960 return(llapi_rgetfacl(argc, argv));
1963 static int lfs_cp(int argc, char **argv)
1965 return(llapi_cp(argc, argv));
1968 static int lfs_ls(int argc, char **argv)
1970 return(llapi_ls(argc, argv));
1973 int main(int argc, char **argv)
1979 ptl_initialize(argc, argv);
1980 if (obd_initialize(argc, argv) < 0)
1982 if (dbg_initialize(argc, argv) < 0)
1985 Parser_init("lfs > ", cmdlist);
1988 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1990 rc = Parser_commands();
1993 obd_finalize(argc, argv);