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 param.gid = strtol(optarg, &endptr, 10);
507 if (optarg == endptr) {
508 ret = name2id(¶m.gid, optarg, GRPQUOTA);
510 fprintf(stderr, "Group/GID: %s cannot "
511 "be found.\n", optarg);
515 param.exclude_gid = !!neg_opt;
520 param.uid = strtol(optarg, &endptr, 10);
521 if (optarg == endptr) {
522 fprintf(stderr, "Bad uid: %s\n", optarg);
525 param.exclude_uid = !!neg_opt;
530 param.uid = strtol(optarg, &endptr, 10);
531 if (optarg == endptr) {
532 ret = name2id(¶m.uid, optarg, USRQUOTA);
534 fprintf(stderr, "User/UID: %s cannot "
535 "be found.\n", optarg);
539 param.exclude_uid = !!neg_opt;
544 param.pattern = (char *)optarg;
545 param.exclude_pattern = !!neg_opt;
548 char *buf, *token, *next, *p;
551 len = strlen((char *)optarg);
555 strcpy(buf, (char *)optarg);
557 if (param.num_alloc_obds == 0) {
558 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
559 sizeof(struct obd_uuid));
560 if (param.obduuid == NULL)
562 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
565 for (token = buf; token && *token; token = next) {
566 p = strchr(token, ',');
572 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
596 param.exclude_type = !!neg_opt;
598 case 'b': param.type = S_IFBLK; break;
599 case 'c': param.type = S_IFCHR; break;
600 case 'd': param.type = S_IFDIR; break;
601 case 'f': param.type = S_IFREG; break;
602 case 'l': param.type = S_IFLNK; break;
603 case 'p': param.type = S_IFIFO; break;
604 case 's': param.type = S_IFSOCK; break;
605 #ifdef S_IFDOOR /* Solaris only */
606 case 'D': param.type = S_IFDOOR; break;
608 default: fprintf(stderr, "error: %s: bad type '%s'\n",
615 if (optarg[0] == '-')
617 else if (optarg[0] == '+')
626 if (optarg[0] == '+')
627 param.size_sign = -1;
628 else if (optarg[0] == '-')
629 param.size_sign = +1;
633 ret = parse_size(optarg, ¶m.size,¶m.size_units);
635 fprintf(stderr,"error: bad size '%s'\n",
648 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
649 argv[0], argv[optind - 1]);
654 if (pathstart == -1) {
655 fprintf(stderr, "error: %s: no filename|pathname\n",
658 } else if (pathend == -1) {
666 static int deprecated_warning;
667 if (!deprecated_warning) {
668 fprintf(stderr, "lfs find: -q, -r, -v options "
669 "deprecated. Use 'lfs getstripe' instead.\n");
670 deprecated_warning = 1;
672 if (!param.recursive && param.maxdepth == -1)
678 ret = llapi_find(argv[pathstart], ¶m);
680 ret = llapi_getstripe(argv[pathstart], ¶m);
681 } while (++pathstart < pathend && !ret);
684 fprintf(stderr, "error: %s failed for %s.\n",
685 argv[0], argv[optind - 1]);
687 if (param.obduuid && param.num_alloc_obds)
693 static int lfs_getstripe(int argc, char **argv)
695 struct option long_opts[] = {
697 {"quiet", 0, 0, 'q'},
698 {"recursive", 0, 0, 'r'},
699 {"verbose", 0, 0, 'v'},
702 char short_opts[] = "hO:qrv";
704 struct find_param param = { 0 };
707 while ((c = getopt_long(argc, argv, short_opts,
708 long_opts, NULL)) != -1) {
713 "error: %s: only one obduuid allowed",
717 param.obduuid = (struct obd_uuid *)optarg;
733 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
734 argv[0], argv[optind - 1]);
742 param.maxdepth = param.recursive ? -1 : 1;
745 rc = llapi_getstripe(argv[optind], ¶m);
746 } while (++optind < argc && !rc);
749 fprintf(stderr, "error: %s failed for %s.\n",
750 argv[0], argv[optind - 1]);
754 static int lfs_osts(int argc, char **argv)
757 struct mntent *mnt = NULL;
758 struct find_param param;
764 fp = setmntent(MOUNTED, "r");
767 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
771 while (feof(fp) == 0 && ferror(fp) ==0) {
772 memset(¶m, 0, sizeof(param));
773 if (llapi_is_lustre_mnt(mnt)) {
774 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
777 "error: %s: failed on %s\n",
778 argv[0], mnt->mnt_dir);
788 #define COOK(value) \
791 while (value > 1024) { \
805 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
807 char rpath[PATH_MAX] = {'\0'};
809 int rc, len, out_len = 0;
811 if (!realpath(path, rpath)) {
813 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
814 path, strerror(-rc));
820 while (feof(fp) == 0 && ferror(fp) == 0) {
821 if (llapi_is_lustre_mnt(mnt)) {
822 len = strlen(mnt->mnt_dir);
824 !strncmp(rpath, mnt->mnt_dir, len)) {
826 memset(mntdir, 0, dir_len);
827 strncpy(mntdir, mnt->mnt_dir, dir_len);
836 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
840 static int showdf(char *mntdir, struct obd_statfs *stat,
841 char *uuid, int ishow, int cooked,
842 char *type, int index, int rc)
844 long long avail, used, total;
846 char *suffix = "KMGTPEZY";
847 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
848 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
856 avail = stat->os_ffree;
857 used = stat->os_files - stat->os_ffree;
858 total = stat->os_files;
860 int shift = cooked ? 0 : 10;
862 avail = (stat->os_bavail * stat->os_bsize) >> shift;
863 used = ((stat->os_blocks - stat->os_bfree) *
864 stat->os_bsize) >> shift;
865 total = (stat->os_blocks * stat->os_bsize) >> shift;
869 ratio = (double)used / (double)total;
875 cook_val = (double)total;
878 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
880 sprintf(tbuf, CDF, total);
882 cook_val = (double)used;
885 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
887 sprintf(ubuf, CDF, used);
889 cook_val = (double)avail;
892 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
894 sprintf(abuf, CDF, avail);
896 sprintf(tbuf, CDF, total);
897 sprintf(ubuf, CDF, used);
898 sprintf(abuf, CDF, avail);
901 sprintf(rbuf, RDF, (int)(ratio * 100));
902 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
903 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
905 printf("[%s:%d]\n", type, index);
911 printf(UUF": inactive device\n", uuid);
914 printf(UUF": %s\n", uuid, strerror(-rc));
921 static int mntdf(char *mntdir, int ishow, int cooked)
923 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
924 struct obd_uuid uuid_buf;
929 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
930 "UUID", "Inodes", "IUsed", "IFree",
931 "IUse%", "Mounted on");
933 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
934 "UUID", cooked ? "bytes" : "1K-blocks",
935 "Used", "Available", "Use%", "Mounted on");
937 for (index = 0; ; index++) {
938 memset(&stat_buf, 0, sizeof(struct obd_statfs));
939 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
940 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
941 &stat_buf, &uuid_buf);
948 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
949 rc == -ENODATA || rc == 0) {
950 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
954 "error: llapi_obd_statfs(%s): %s (%d)\n",
955 uuid_buf.uuid, strerror(-rc), rc);
959 sum.os_ffree += stat_buf.os_ffree;
960 sum.os_files += stat_buf.os_files;
964 for (index = 0; ; index++) {
965 memset(&stat_buf, 0, sizeof(struct obd_statfs));
966 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
967 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
968 &stat_buf, &uuid_buf);
975 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
976 rc == -ENODATA || rc == 0) {
977 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
981 "error: llapi_obd_statfs failed: %s (%d)\n",
986 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
987 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
988 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
993 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
998 static int lfs_df(int argc, char **argv)
1002 struct mntent *mnt = NULL;
1003 char *mntdir = NULL;
1004 int ishow = 0, cooked = 0;
1008 while ((c = getopt(argc, argv, "ih")) != -1) {
1021 path = argv[optind];
1023 fp = setmntent(MOUNTED, "r");
1026 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1027 argv[0], MOUNTED, strerror(errno));
1031 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1032 fprintf(stderr, "error: cannot allocate %d bytes\n",
1036 memset(mntdir, 0, PATH_MAX);
1039 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1046 rc = mntdf(mntdir, ishow, cooked);
1050 mnt = getmntent(fp);
1051 while (feof(fp) == 0 && ferror(fp) == 0) {
1052 if (llapi_is_lustre_mnt(mnt)) {
1053 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1058 mnt = getmntent(fp);
1067 static int lfs_check(int argc, char **argv)
1071 struct mntent *mnt = NULL;
1080 obd_types[0] = obd_type1;
1081 obd_types[1] = obd_type2;
1083 if (strcmp(argv[1], "osts") == 0) {
1084 strcpy(obd_types[0], "osc");
1085 } else if (strcmp(argv[1], "mds") == 0) {
1086 strcpy(obd_types[0], "mdc");
1087 } else if (strcmp(argv[1], "servers") == 0) {
1089 strcpy(obd_types[0], "osc");
1090 strcpy(obd_types[1], "mdc");
1092 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1097 fp = setmntent(MOUNTED, "r");
1099 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1102 mnt = getmntent(fp);
1103 while (feof(fp) == 0 && ferror(fp) ==0) {
1104 if (llapi_is_lustre_mnt(mnt))
1106 mnt = getmntent(fp);
1112 fprintf(stderr, "No suitable Lustre mount found\n");
1116 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1119 fprintf(stderr, "error: %s: %s status failed\n",
1126 static int lfs_catinfo(int argc, char **argv)
1129 struct mntent *mnt = NULL;
1132 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1135 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1138 fp = setmntent(MOUNTED, "r");
1140 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1143 mnt = getmntent(fp);
1144 while (feof(fp) == 0 && ferror(fp) == 0) {
1145 if (llapi_is_lustre_mnt(mnt))
1147 mnt = getmntent(fp);
1154 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1156 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1158 fprintf(stderr, "no lustre_lite mounted.\n");
1165 int lfs_join(int argc, char **argv)
1167 char *name_head, *name_tail;
1173 name_head = argv[1];
1174 fd = open(name_head, O_WRONLY);
1176 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1180 size = lseek(fd, 0, SEEK_END);
1181 if (size % JOIN_FILE_ALIGN) {
1182 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1183 name_head, (long long)size, JOIN_FILE_ALIGN);
1187 name_tail = argv[2];
1188 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1192 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1198 #ifdef HAVE_QUOTA_SUPPORT
1199 static int lfs_quotachown(int argc, char **argv)
1206 while ((c = getopt(argc, argv, "i")) != -1) {
1212 fprintf(stderr, "error: %s: option '-%c' "
1213 "unrecognized\n", argv[0], c);
1219 rc = llapi_quotachown(argv[optind], flag);
1221 fprintf(stderr,"error: change file owner/group failed.\n");
1226 static int lfs_quotacheck(int argc, char **argv)
1228 int c, check_type = 0;
1230 struct if_quotacheck qchk;
1231 struct if_quotactl qctl;
1232 char *obd_type = qchk.obd_type;
1233 char *obd_uuid = qchk.obd_uuid.uuid;
1236 memset(&qchk, 0, sizeof(qchk));
1239 while ((c = getopt(argc, argv, "ug")) != -1) {
1248 fprintf(stderr, "error: %s: option '-%c' "
1249 "unrecognized\n", argv[0], c);
1256 else /* do quotacheck for both user & group quota by default */
1264 memset(&qctl, 0, sizeof(qctl));
1265 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1266 qctl.qc_id = QFMT_LDISKFS;
1267 qctl.qc_type = check_type;
1268 rc = llapi_quotactl(mnt, &qctl);
1270 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1274 rc = llapi_quotacheck(mnt, check_type);
1276 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1280 rc = llapi_poll_quotacheck(mnt, &qchk);
1283 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1284 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1288 memset(&qctl, 0, sizeof(qctl));
1289 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1290 qctl.qc_id = QFMT_LDISKFS;
1291 qctl.qc_type = check_type;
1292 rc = llapi_quotactl(mnt, &qctl);
1295 fprintf(stderr, "%s %s ",
1296 qctl.obd_type, qctl.obd_uuid.uuid);
1297 fprintf(stderr, "%s turn on quota failed: %s\n",
1298 argv[0], strerror(errno));
1305 static int lfs_quotaon(int argc, char **argv)
1309 struct if_quotactl qctl;
1310 char *obd_type = qctl.obd_type;
1311 char *obd_uuid = qctl.obd_uuid.uuid;
1314 memset(&qctl, 0, sizeof(qctl));
1315 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1316 qctl.qc_id = QFMT_LDISKFS;
1319 while ((c = getopt(argc, argv, "ugf")) != -1) {
1322 qctl.qc_type |= 0x01;
1325 qctl.qc_type |= 0x02;
1328 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1331 fprintf(stderr, "error: %s: option '-%c' "
1332 "unrecognized\n", argv[0], c);
1339 else /* by default, enable quota for both user & group */
1340 qctl.qc_type = 0x02;
1347 rc = llapi_quotactl(mnt, &qctl);
1350 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1351 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1358 static int lfs_quotaoff(int argc, char **argv)
1362 struct if_quotactl qctl;
1363 char *obd_type = qctl.obd_type;
1364 char *obd_uuid = qctl.obd_uuid.uuid;
1367 memset(&qctl, 0, sizeof(qctl));
1368 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1371 while ((c = getopt(argc, argv, "ug")) != -1) {
1374 qctl.qc_type |= 0x01;
1377 qctl.qc_type |= 0x02;
1380 fprintf(stderr, "error: %s: option '-%c' "
1381 "unrecognized\n", argv[0], c);
1388 else /* by default, disable quota for both user & group */
1389 qctl.qc_type = 0x02;
1396 rc = llapi_quotactl(mnt, &qctl);
1399 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1400 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1407 #define ARG2INT(nr, str, msg) \
1410 nr = strtol(str, &endp, 0); \
1412 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1417 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1419 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1420 * returns the value or ULONG_MAX on integer overflow or incorrect format
1422 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1423 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1424 * 3. empty integer value is interpreted as 0
1427 static unsigned long str2sec(const char* timestr) {
1428 const char spec[] = "smhdw";
1429 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1430 unsigned long val = 0;
1433 if (strpbrk(timestr, spec) == NULL) {
1434 /* no specifiers inside the time string,
1435 should treat it as an integer value */
1436 val = strtoul(timestr, &tail, 10);
1437 return *tail ? ULONG_MAX : val;
1440 /* format string is XXwXXdXXhXXmXXs */
1446 v = strtoul(timestr, &tail, 10);
1447 if (v == ULONG_MAX || *tail == '\0')
1448 /* value too large (ULONG_MAX or more)
1449 or missing specifier */
1452 ptr = strchr(spec, *tail);
1454 /* unknown specifier */
1459 /* check if product will overflow the type */
1460 if (!(v < ULONG_MAX / mult[ind]))
1463 ADD_OVERFLOW(val, mult[ind] * v);
1464 if (val == ULONG_MAX)
1476 int lfs_setquota(int argc, char **argv)
1480 struct if_quotactl qctl;
1481 char *obd_type = qctl.obd_type;
1482 char *obd_uuid = qctl.obd_uuid.uuid;
1485 memset(&qctl, 0, sizeof(qctl));
1486 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1489 while ((c = getopt(argc, argv, "ugt")) != -1) {
1492 qctl.qc_type |= 0x01;
1495 qctl.qc_type |= 0x02;
1498 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1501 fprintf(stderr, "error: %s: option '-%c' "
1502 "unrecognized\n", argv[0], c);
1510 if (qctl.qc_type == UGQUOTA) {
1511 fprintf(stderr, "error: user and group quotas can't be set "
1516 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1517 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1519 if (optind + 6 != argc)
1522 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1524 fprintf(stderr, "error: find id for name %s failed: %s\n",
1525 argv[optind - 1], strerror(errno));
1529 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1530 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1531 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1532 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1534 dqb->dqb_valid = QIF_LIMITS;
1536 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1538 if (optind + 3 != argc)
1541 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1542 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1545 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1546 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1553 rc = llapi_quotactl(mnt, &qctl);
1556 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1557 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1564 static inline char *type2name(int check_type)
1566 if (check_type == USRQUOTA)
1568 else if (check_type == GRPQUOTA)
1575 /* Converts seconds value into format string
1576 * result is returned in buf
1578 * 1. result is in descenting order: 1w2d3h4m5s
1579 * 2. zero fields are not filled (except for p. 3): 5d1s
1580 * 3. zero seconds value is presented as "0s"
1582 static void sec2str(time_t seconds, char *buf)
1584 const char spec[] = "smhdw";
1585 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1590 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1591 c = seconds / mult[i];
1593 if (c > 0 || (i == 0 && buf == tail))
1594 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1601 static void diff2str(time_t seconds, char *buf, time_t now)
1607 if (seconds <= now) {
1608 strcpy(buf, "none");
1611 sec2str(seconds - now, buf);
1614 static void print_quota_title(char *name, struct if_quotactl *qctl)
1616 printf("Disk quotas for %s %s (%cid %u):\n",
1617 type2name(qctl->qc_type), name,
1618 *type2name(qctl->qc_type), qctl->qc_id);
1619 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1621 "kbytes", "quota", "limit", "grace",
1622 "files", "quota", "limit", "grace");
1625 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1631 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1632 int bover = 0, iover = 0;
1633 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1635 if (dqb->dqb_bhardlimit &&
1636 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1638 } else if (dqb->dqb_bsoftlimit &&
1639 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1640 if (dqb->dqb_btime > now) {
1647 if (dqb->dqb_ihardlimit &&
1648 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1650 } else if (dqb->dqb_isoftlimit &&
1651 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1652 if (dqb->dqb_btime > now) {
1659 #if 0 /* XXX: always print quotas even when no usages */
1660 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1666 if (strlen(mnt) > 15)
1667 printf("%s\n%15s", mnt, "");
1669 printf("%15s", mnt);
1672 diff2str(dqb->dqb_btime, timebuf, now);
1674 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1675 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1676 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1677 printf(" %7s%c %6s %7s %7s",
1678 numbuf[0], bover ? '*' : ' ', numbuf[1],
1679 numbuf[2], bover > 1 ? timebuf : "");
1682 diff2str(dqb->dqb_itime, timebuf, now);
1684 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1685 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1686 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1688 printf(" %7s%c %6s %7s %7s",
1689 numbuf[0], iover ? '*' : ' ', numbuf[1],
1690 numbuf[2], iover > 1 ? timebuf : "");
1693 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1694 qctl->qc_cmd == Q_GETOINFO) {
1698 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1699 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1700 printf("Block grace time: %s; Inode grace time: %s\n",
1701 bgtimebuf, igtimebuf);
1705 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1709 /* XXX: this is a flag to mark that only mds quota is wanted */
1710 qctl->qc_dqblk.dqb_valid = 1;
1711 rc = llapi_quotactl(mnt, qctl);
1713 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1716 qctl->qc_dqblk.dqb_valid = 0;
1718 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1721 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1724 struct obd_uuid *uuids = NULL, *uuidp;
1725 int obdcount = 1024;
1730 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1734 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1735 sizeof(struct obd_uuid));
1740 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1742 struct obd_uuid *uuids_temp;
1744 if (rc == -EOVERFLOW) {
1745 uuids_temp = realloc(uuids, obdcount *
1746 sizeof(struct obd_uuid));
1747 if (uuids_temp != NULL)
1748 goto retry_get_uuids;
1753 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1757 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1758 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1760 /* XXX clear this flag to get quota from osts */
1761 qctl->qc_dqblk.dqb_valid = 0;
1762 rc = llapi_quotactl(mnt, qctl);
1764 fprintf(stderr, "%s quotactl failed: %s\n",
1765 uuidp->uuid, strerror(errno));
1769 print_quota(uuidp->uuid, qctl, 1);
1777 static int lfs_quota(int argc, char **argv)
1780 char *name = NULL, *mnt;
1781 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1783 char *obd_type = qctl.obd_type;
1784 char *obd_uuid = qctl.obd_uuid.uuid;
1788 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1791 qctl.qc_type = 0x01;
1794 qctl.qc_type = 0x02;
1797 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1800 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1803 fprintf(stderr, "error: %s: option '-%c' "
1804 "unrecognized\n", argv[0], c);
1813 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1814 if (optind + 2 != argc) {
1815 fprintf(stderr, "error: missing quota argument(s)\n");
1819 name = argv[optind++];
1820 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1822 fprintf(stderr,"error: can't find id for name %s: %s\n",
1823 name, strerror(errno));
1826 print_quota_title(name, &qctl);
1827 } else if (optind + 1 != argc) {
1828 fprintf(stderr, "error: missing quota info argument(s)\n");
1834 rc = llapi_quotactl(mnt, &qctl);
1837 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1838 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1843 rc = id2name(&name, getuid(), qctl.qc_type);
1850 print_quota(mnt, &qctl, 0);
1852 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1853 print_mds_quota(mnt, &qctl);
1854 print_lov_quota(mnt, &qctl);
1859 #endif /* HAVE_QUOTA_SUPPORT */
1861 static int flushctx_ioctl(char *mp)
1865 fd = open(mp, O_RDONLY);
1867 fprintf(stderr, "flushctx: error open %s: %s\n",
1868 mp, strerror(errno));
1872 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1874 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1875 mp, strerror(errno));
1881 static int lfs_flushctx(int argc, char **argv)
1883 int kdestroy = 0, c;
1885 char procline[PATH_MAX], *line;
1889 while ((c = getopt(argc, argv, "k")) != -1) {
1895 fprintf(stderr, "error: %s: option '-%c' "
1896 "unrecognized\n", argv[0], c);
1902 system("kdestroy > /dev/null");
1904 if (optind >= argc) {
1905 /* flush for all mounted lustre fs. */
1906 proc = fopen("/proc/mounts", "r");
1908 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1913 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1918 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1919 fprintf(stderr, "%s: unexpected format in "
1925 if (strcmp(fs, "lustre") != 0)
1927 /* we use '@' to determine it's a client. are there
1928 * any other better way?
1930 if (strchr(dev, '@') == NULL)
1933 if (flushctx_ioctl(mp))
1937 /* flush fs as specified */
1938 while (optind < argc) {
1939 if (flushctx_ioctl(argv[optind++]))
1947 static int lfs_lsetfacl(int argc, char **argv)
1950 return(llapi_lsetfacl(argc, argv));
1953 static int lfs_lgetfacl(int argc, char **argv)
1956 return(llapi_lgetfacl(argc, argv));
1959 static int lfs_rsetfacl(int argc, char **argv)
1962 return(llapi_rsetfacl(argc, argv));
1965 static int lfs_rgetfacl(int argc, char **argv)
1968 return(llapi_rgetfacl(argc, argv));
1971 static int lfs_cp(int argc, char **argv)
1973 return(llapi_cp(argc, argv));
1976 static int lfs_ls(int argc, char **argv)
1978 return(llapi_ls(argc, argv));
1981 int main(int argc, char **argv)
1987 ptl_initialize(argc, argv);
1988 if (obd_initialize(argc, argv) < 0)
1990 if (dbg_initialize(argc, argv) < 0)
1993 Parser_init("lfs > ", cmdlist);
1996 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1998 rc = Parser_commands();
2001 obd_finalize(argc, argv);