1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
56 #include <sys/types.h>
65 #include <lnet/api-support.h>
66 #include <lnet/lnetctl.h>
68 #include <liblustre.h>
69 #include <lustre/lustre_idl.h>
70 #include <lustre/liblustreapi.h>
71 #include <lustre/lustre_user.h>
73 #include <libcfs/libcfsutil.h>
76 unsigned int libcfs_subsystem_debug = 0;
79 static int lfs_setstripe(int argc, char **argv);
80 static int lfs_find(int argc, char **argv);
81 static int lfs_getstripe(int argc, char **argv);
82 static int lfs_osts(int argc, char **argv);
83 static int lfs_df(int argc, char **argv);
84 static int lfs_check(int argc, char **argv);
85 static int lfs_catinfo(int argc, char **argv);
86 #ifdef HAVE_QUOTA_SUPPORT
87 static int lfs_quotachown(int argc, char **argv);
88 static int lfs_quotacheck(int argc, char **argv);
89 static int lfs_quotaon(int argc, char **argv);
90 static int lfs_quotaoff(int argc, char **argv);
91 static int lfs_setquota(int argc, char **argv);
92 static int lfs_quota(int argc, char **argv);
94 static int lfs_flushctx(int argc, char **argv);
95 static int lfs_join(int argc, char **argv);
96 static int lfs_lsetfacl(int argc, char **argv);
97 static int lfs_lgetfacl(int argc, char **argv);
98 static int lfs_rsetfacl(int argc, char **argv);
99 static int lfs_rgetfacl(int argc, char **argv);
100 static int lfs_cp(int argc, char **argv);
101 static int lfs_ls(int argc, char **argv);
102 static int lfs_poollist(int argc, char **argv);
104 /* all avaialable commands */
105 command_t cmdlist[] = {
106 {"setstripe", lfs_setstripe, 0,
107 "Create a new file with a specific striping pattern or\n"
108 "set the default striping pattern on an existing directory or\n"
109 "delete the default striping pattern from an existing directory\n"
110 "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
111 " [--count|-c stripe_count] [--pool|-p pool_name]\n"
114 " setstripe -d <dir> (to delete default striping)\n"
115 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
116 "\t Can be specified with k, m or g (in KB, MB and GB\n"
118 "\tstart_ost: OST index of first stripe (-1 filesystem default)\n"
119 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
120 "\tpool_name: Name of OST pool"},
121 {"getstripe", lfs_getstripe, 0,
122 "To list the striping info for a given file or files in a\n"
123 "directory or recursively for all files in a directory tree.\n"
124 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
125 " [--recursive | -r] <dir|file> ..."},
126 {"poollist", lfs_poollist, 0,
127 "List pools or pool OSTs\n"
128 "usage: poollist <fsname>[.<poolname>] | <pathname>\n"},
129 {"find", lfs_find, 0,
130 "To find files that match given parameters recursively in a directory tree.\n"
131 "usage: find <dir|file> ... \n"
132 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
133 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
134 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
135 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
136 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
137 " [[!] --pool <name>]\n"
138 "\t !: used before an option indicates 'NOT' the requested attribute\n"
139 "\t -: used before an value indicates 'AT MOST' the requested value\n"
140 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
141 {"check", lfs_check, 0,
142 "Display the status of MDS or OSTs (as specified in the command)\n"
143 "or all the servers (MDS and OSTs).\n"
144 "usage: check <osts|mds|servers>"},
145 {"catinfo", lfs_catinfo, 0,
146 "Show information of specified type logs.\n"
147 "usage: catinfo {keyword} [node name]\n"
148 "\tkeywords are one of followings: config, deletions.\n"
149 "\tnode name must be provided when use keyword config."},
150 {"join", lfs_join, 0,
151 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
152 "usage: join <filename_A> <filename_B>\n"},
153 {"osts", lfs_osts, 0, "osts"},
155 "report filesystem disk space usage or inodes usage"
157 "Usage: df [-i] [-h] [path]"},
158 #ifdef HAVE_QUOTA_SUPPORT
159 {"quotachown",lfs_quotachown, 0,
160 "Change files' owner or group on the specified filesystem.\n"
161 "usage: quotachown [-i] <filesystem>\n"
162 "\t-i: ignore error if file is not exist\n"},
163 {"quotacheck", lfs_quotacheck, 0,
164 "Scan the specified filesystem for disk usage, and create,\n"
165 "or update quota files.\n"
166 "usage: quotacheck [ -ug ] <filesystem>"},
167 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
168 "usage: quotaon [ -ugf ] <filesystem>"},
169 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
170 "usage: quotaoff [ -ug ] <filesystem>"},
171 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
172 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
173 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
174 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
175 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
177 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
178 "usage: flushctx [-k] [mountpoint...]"},
179 {"lsetfacl", lfs_lsetfacl, 0,
180 "Remote user setfacl for user/group on the same remote client.\n"
181 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
182 {"lgetfacl", lfs_lgetfacl, 0,
183 "Remote user getfacl for user/group on the same remote client.\n"
184 "usage: lgetfacl [-dRLPvh] file ..."},
185 {"rsetfacl", lfs_rsetfacl, 0,
186 "Remote user setfacl for user/group on other clients.\n"
187 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
188 {"rgetfacl", lfs_rgetfacl, 0,
189 "Remote user getfacl for user/group on other clients.\n"
190 "usage: rgetfacl [-dRLPvh] file ..."},
192 "Remote user copy files and directories.\n"
193 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
195 "Remote user list directory contents.\n"
196 "usage: ls [OPTION]... [FILE]..."},
197 {"help", Parser_help, 0, "help"},
198 {"exit", Parser_quit, 0, "quit"},
199 {"quit", Parser_quit, 0, "quit"},
204 static int lfs_setstripe(int argc, char **argv)
208 unsigned long long st_size;
209 int st_offset, st_count;
213 char *stripe_size_arg = NULL;
214 char *stripe_off_arg = NULL;
215 char *stripe_count_arg = NULL;
216 char *pool_name_arg = NULL;
217 unsigned long long size_units;
219 struct option long_opts[] = {
220 {"size", required_argument, 0, 's'},
221 {"count", required_argument, 0, 'c'},
222 {"index", required_argument, 0, 'i'},
223 {"offset", required_argument, 0, 'o'},
224 {"pool", required_argument, 0, 'p'},
225 {"delete", no_argument, 0, 'd'},
232 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
233 /* for compatibility with the existing positional parameter
237 } else if (argc == 5 &&
238 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
239 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
240 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
241 /* for compatibility with the existing positional parameter
244 stripe_size_arg = argv[2];
245 stripe_off_arg = argv[3];
246 stripe_count_arg = argv[4];
250 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
251 long_opts, NULL)) >= 0) {
257 stripe_count_arg = optarg;
260 /* delete the default striping pattern */
265 stripe_off_arg = optarg;
268 stripe_size_arg = optarg;
271 pool_name_arg = optarg;
276 fprintf(stderr, "error: %s: option '%s' "
278 argv[0], argv[optind - 1]);
283 fname = argv[optind];
289 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
290 stripe_count_arg != NULL || pool_name_arg != NULL)) {
291 fprintf(stderr, "error: %s: cannot specify -d with "
292 "-s, -c -o or -p options\n",
297 if (optind != argc - 1) {
298 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
300 argv[0], argv[argc - 1]);
304 /* get the stripe size */
305 if (stripe_size_arg != NULL) {
306 result = parse_size(stripe_size_arg, &st_size, &size_units);
308 fprintf(stderr,"error: bad size '%s'\n",
313 /* get the stripe offset */
314 if (stripe_off_arg != NULL) {
315 st_offset = strtoul(stripe_off_arg, &end, 0);
317 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
318 argv[0], stripe_off_arg);
322 /* get the stripe count */
323 if (stripe_count_arg != NULL) {
324 st_count = strtoul(stripe_count_arg, &end, 0);
326 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
327 argv[0], stripe_count_arg);
332 if (pool_name_arg == NULL)
333 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
335 result = llapi_file_create_pool(fname, st_size, st_offset,
336 st_count, 0, pool_name_arg);
339 fprintf(stderr, "error: %s: create stripe file failed\n",
345 static int lfs_poollist(int argc, char **argv)
350 return llapi_poollist(argv[1]);
353 static int set_time(time_t *time, time_t *set, char *str)
360 else if (str[0] == '-')
366 t = strtol(str, NULL, 0);
367 if (*time < t * 24 * 60 * 60) {
370 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
374 *set = *time - t * 24 * 60 * 60;
378 static int name2id(unsigned int *id, char *name, int type)
380 if (type == USRQUOTA) {
381 struct passwd *entry;
383 if (!(entry = getpwnam(name))) {
393 if (!(entry = getgrnam(name))) {
405 static int id2name(char **name, unsigned int id, int type)
407 if (type == USRQUOTA) {
408 struct passwd *entry;
410 if (!(entry = getpwuid(id))) {
416 *name = entry->pw_name;
420 if (!(entry = getgrgid(id))) {
426 *name = entry->gr_name;
432 #define FIND_POOL_OPT 3
433 static int lfs_find(int argc, char **argv)
438 struct find_param param = { .maxdepth = -1 };
440 struct option long_opts[] = {
441 /* New find options. */
442 {"atime", required_argument, 0, 'A'},
443 {"ctime", required_argument, 0, 'C'},
444 {"mtime", required_argument, 0, 'M'},
445 {"maxdepth", required_argument, 0, 'D'},
446 {"gid", required_argument, 0, 'g'},
447 {"group", required_argument, 0, 'G'},
448 {"uid", required_argument, 0, 'u'},
449 {"user", required_argument, 0, 'U'},
450 {"name", required_argument, 0, 'n'},
451 /* no short option for pool, p/P already used */
452 {"pool", required_argument, 0, FIND_POOL_OPT},
453 /* --obd is considered as a new option. */
454 {"obd", required_argument, 0, 'O'},
455 {"ost", required_argument, 0, 'O'},
456 {"print", no_argument, 0, 'P'},
457 {"print0", no_argument, 0, 'p'},
458 /* Old find options. */
459 {"quiet", no_argument, 0, 'q'},
460 {"recursive", no_argument, 0, 'r'},
461 {"size", required_argument, 0, 's'},
462 {"type", required_argument, 0, 't'},
463 {"verbose", no_argument, 0, 'v'},
477 while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
478 long_opts, NULL)) >= 0) {
483 /* '!' is part of option */
484 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
485 if (!isoption && pathend != -1) {
486 fprintf(stderr, "err: %s: filename|dirname must either "
487 "precede options or follow options\n",
491 if (!isoption && pathstart == -1)
492 pathstart = optind - 1;
493 if (isoption && pathstart != -1 && pathend == -1) {
494 pathend = optind - 2;
495 if ((c == 1 && strcmp(optarg, "!") == 0) ||
496 c == 'P' || c == 'p' || c == 'O' ||
497 c == 'q' || c == 'r' || c == 'v')
498 pathend = optind - 1;
505 if (strcmp(optarg, "!") == 0)
509 xtime = ¶m.atime;
510 xsign = ¶m.asign;
513 xtime = ¶m.ctime;
514 xsign = ¶m.csign;
518 xtime = ¶m.mtime;
519 xsign = ¶m.msign;
523 if (optarg[0] == '-')
525 else if (optarg[0] == '+')
534 ret = set_time(&t, xtime, optarg);
542 param.maxdepth = strtol(optarg, 0, 0);
546 param.gid = strtol(optarg, &endptr, 10);
547 if (optarg == endptr) {
548 fprintf(stderr, "Bad gid: %s\n", optarg);
551 param.exclude_gid = !!neg_opt;
556 param.gid = strtol(optarg, &endptr, 10);
557 if (optarg == endptr) {
558 ret = name2id(¶m.gid, optarg, GRPQUOTA);
560 fprintf(stderr, "Group/GID: %s cannot "
561 "be found.\n", optarg);
565 param.exclude_gid = !!neg_opt;
570 param.uid = strtol(optarg, &endptr, 10);
571 if (optarg == endptr) {
572 fprintf(stderr, "Bad uid: %s\n", optarg);
575 param.exclude_uid = !!neg_opt;
580 param.uid = strtol(optarg, &endptr, 10);
581 if (optarg == endptr) {
582 ret = name2id(¶m.uid, optarg, USRQUOTA);
584 fprintf(stderr, "User/UID: %s cannot "
585 "be found.\n", optarg);
589 param.exclude_uid = !!neg_opt;
594 if (strlen(optarg) > MAXPOOLNAME) {
596 "Pool name %s is too long"
597 " (max is %d)\n", optarg,
601 /* we do check for empty pool because empty pool
602 * is used to find V1 lov attributes */
603 strncpy(param.poolname, optarg, MAXPOOLNAME);
604 param.poolname[MAXPOOLNAME] = '\0';
605 param.exclude_pool = !!neg_opt;
606 param.check_pool = 1;
610 param.pattern = (char *)optarg;
611 param.exclude_pattern = !!neg_opt;
614 char *buf, *token, *next, *p;
617 len = strlen((char *)optarg);
621 strcpy(buf, (char *)optarg);
623 if (param.num_alloc_obds == 0) {
624 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
625 sizeof(struct obd_uuid));
626 if (param.obduuid == NULL)
628 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
631 for (token = buf; token && *token; token = next) {
632 p = strchr(token, ',');
638 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
662 param.exclude_type = !!neg_opt;
664 case 'b': param.type = S_IFBLK; break;
665 case 'c': param.type = S_IFCHR; break;
666 case 'd': param.type = S_IFDIR; break;
667 case 'f': param.type = S_IFREG; break;
668 case 'l': param.type = S_IFLNK; break;
669 case 'p': param.type = S_IFIFO; break;
670 case 's': param.type = S_IFSOCK; break;
671 #ifdef S_IFDOOR /* Solaris only */
672 case 'D': param.type = S_IFDOOR; break;
674 default: fprintf(stderr, "error: %s: bad type '%s'\n",
681 if (optarg[0] == '-')
683 else if (optarg[0] == '+')
692 if (optarg[0] == '+')
693 param.size_sign = -1;
694 else if (optarg[0] == '-')
695 param.size_sign = +1;
699 ret = parse_size(optarg, ¶m.size,¶m.size_units);
701 fprintf(stderr,"error: bad size '%s'\n",
714 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
715 argv[0], argv[optind - 1]);
720 if (pathstart == -1) {
721 fprintf(stderr, "error: %s: no filename|pathname\n",
724 } else if (pathend == -1) {
732 static int deprecated_warning;
733 if (!deprecated_warning) {
734 fprintf(stderr, "lfs find: -q, -r, -v options "
735 "deprecated. Use 'lfs getstripe' instead.\n");
736 deprecated_warning = 1;
738 if (!param.recursive && param.maxdepth == -1)
744 ret = llapi_find(argv[pathstart], ¶m);
746 ret = llapi_getstripe(argv[pathstart], ¶m);
747 } while (++pathstart < pathend && !ret);
750 fprintf(stderr, "error: %s failed for %s.\n",
751 argv[0], argv[optind - 1]);
753 if (param.obduuid && param.num_alloc_obds)
759 static int lfs_getstripe(int argc, char **argv)
761 struct option long_opts[] = {
763 {"quiet", 0, 0, 'q'},
764 {"recursive", 0, 0, 'r'},
765 {"verbose", 0, 0, 'v'},
768 char short_opts[] = "hO:qrv";
770 struct find_param param = { 0 };
773 while ((c = getopt_long(argc, argv, short_opts,
774 long_opts, NULL)) != -1) {
779 "error: %s: only one obduuid allowed",
783 param.obduuid = (struct obd_uuid *)optarg;
799 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
800 argv[0], argv[optind - 1]);
808 param.maxdepth = param.recursive ? -1 : 1;
811 rc = llapi_getstripe(argv[optind], ¶m);
812 } while (++optind < argc && !rc);
815 fprintf(stderr, "error: %s failed for %s.\n",
816 argv[0], argv[optind - 1]);
820 static int lfs_osts(int argc, char **argv)
823 struct mntent *mnt = NULL;
824 struct find_param param;
830 fp = setmntent(MOUNTED, "r");
833 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
837 while (feof(fp) == 0 && ferror(fp) ==0) {
838 memset(¶m, 0, sizeof(param));
839 if (llapi_is_lustre_mnt(mnt)) {
840 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
843 "error: %s: failed on %s\n",
844 argv[0], mnt->mnt_dir);
854 #define COOK(value) \
857 while (value > 1024) { \
871 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
873 char rpath[PATH_MAX] = {'\0'};
875 int rc, len, out_len = 0;
877 if (!realpath(path, rpath)) {
879 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
880 path, strerror(-rc));
886 while (feof(fp) == 0 && ferror(fp) == 0) {
887 if (llapi_is_lustre_mnt(mnt)) {
888 len = strlen(mnt->mnt_dir);
890 !strncmp(rpath, mnt->mnt_dir, len)) {
892 memset(mntdir, 0, dir_len);
893 strncpy(mntdir, mnt->mnt_dir, dir_len);
902 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
906 static int showdf(char *mntdir, struct obd_statfs *stat,
907 char *uuid, int ishow, int cooked,
908 char *type, int index, int rc)
910 long long avail, used, total;
912 char *suffix = "KMGTPEZY";
913 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
914 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
922 avail = stat->os_ffree;
923 used = stat->os_files - stat->os_ffree;
924 total = stat->os_files;
926 int shift = cooked ? 0 : 10;
928 avail = (stat->os_bavail * stat->os_bsize) >> shift;
929 used = ((stat->os_blocks - stat->os_bfree) *
930 stat->os_bsize) >> shift;
931 total = (stat->os_blocks * stat->os_bsize) >> shift;
935 ratio = (double)used / (double)total;
941 cook_val = (double)total;
944 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
946 sprintf(tbuf, CDF, total);
948 cook_val = (double)used;
951 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
953 sprintf(ubuf, CDF, used);
955 cook_val = (double)avail;
958 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
960 sprintf(abuf, CDF, avail);
962 sprintf(tbuf, CDF, total);
963 sprintf(ubuf, CDF, used);
964 sprintf(abuf, CDF, avail);
967 sprintf(rbuf, RDF, (int)(ratio * 100));
968 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
969 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
971 printf("[%s:%d]\n", type, index);
977 printf(UUF": inactive device\n", uuid);
980 printf(UUF": %s\n", uuid, strerror(-rc));
987 static int mntdf(char *mntdir, int ishow, int cooked)
989 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
990 struct obd_uuid uuid_buf;
995 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
996 "UUID", "Inodes", "IUsed", "IFree",
997 "IUse%", "Mounted on");
999 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1000 "UUID", cooked ? "bytes" : "1K-blocks",
1001 "Used", "Available", "Use%", "Mounted on");
1003 for (index = 0; ; index++) {
1004 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1005 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1006 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1007 &stat_buf, &uuid_buf);
1014 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1015 rc == -ENODATA || rc == 0) {
1016 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
1020 "error: llapi_obd_statfs(%s): %s (%d)\n",
1021 uuid_buf.uuid, strerror(-rc), rc);
1025 sum.os_ffree += stat_buf.os_ffree;
1026 sum.os_files += stat_buf.os_files;
1030 for (index = 0; ; index++) {
1031 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1032 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1033 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1034 &stat_buf, &uuid_buf);
1041 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1042 rc == -ENODATA || rc == 0) {
1043 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
1047 "error: llapi_obd_statfs failed: %s (%d)\n",
1052 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1053 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1054 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1059 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1064 static int lfs_df(int argc, char **argv)
1068 struct mntent *mnt = NULL;
1069 char *mntdir = NULL;
1070 int ishow = 0, cooked = 0;
1074 while ((c = getopt(argc, argv, "ih")) != -1) {
1087 path = argv[optind];
1089 fp = setmntent(MOUNTED, "r");
1092 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1093 argv[0], MOUNTED, strerror(errno));
1097 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1098 fprintf(stderr, "error: cannot allocate %d bytes\n",
1102 memset(mntdir, 0, PATH_MAX);
1105 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1112 rc = mntdf(mntdir, ishow, cooked);
1116 mnt = getmntent(fp);
1117 while (feof(fp) == 0 && ferror(fp) == 0) {
1118 if (llapi_is_lustre_mnt(mnt)) {
1119 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1124 mnt = getmntent(fp);
1133 static int lfs_check(int argc, char **argv)
1137 struct mntent *mnt = NULL;
1146 obd_types[0] = obd_type1;
1147 obd_types[1] = obd_type2;
1149 if (strcmp(argv[1], "osts") == 0) {
1150 strcpy(obd_types[0], "osc");
1151 } else if (strcmp(argv[1], "mds") == 0) {
1152 strcpy(obd_types[0], "mdc");
1153 } else if (strcmp(argv[1], "servers") == 0) {
1155 strcpy(obd_types[0], "osc");
1156 strcpy(obd_types[1], "mdc");
1158 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1163 fp = setmntent(MOUNTED, "r");
1165 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1168 mnt = getmntent(fp);
1169 while (feof(fp) == 0 && ferror(fp) ==0) {
1170 if (llapi_is_lustre_mnt(mnt))
1172 mnt = getmntent(fp);
1178 fprintf(stderr, "No suitable Lustre mount found\n");
1182 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1185 fprintf(stderr, "error: %s: %s status failed\n",
1192 static int lfs_catinfo(int argc, char **argv)
1195 struct mntent *mnt = NULL;
1198 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1201 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1204 fp = setmntent(MOUNTED, "r");
1206 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1209 mnt = getmntent(fp);
1210 while (feof(fp) == 0 && ferror(fp) == 0) {
1211 if (llapi_is_lustre_mnt(mnt))
1213 mnt = getmntent(fp);
1220 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1222 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1224 fprintf(stderr, "no lustre_lite mounted.\n");
1231 int lfs_join(int argc, char **argv)
1233 char *name_head, *name_tail;
1239 name_head = argv[1];
1240 fd = open(name_head, O_WRONLY);
1242 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1246 size = lseek(fd, 0, SEEK_END);
1247 if (size % JOIN_FILE_ALIGN) {
1248 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1249 name_head, (long long)size, JOIN_FILE_ALIGN);
1253 name_tail = argv[2];
1254 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1258 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1264 #ifdef HAVE_QUOTA_SUPPORT
1265 static int lfs_quotachown(int argc, char **argv)
1272 while ((c = getopt(argc, argv, "i")) != -1) {
1278 fprintf(stderr, "error: %s: option '-%c' "
1279 "unrecognized\n", argv[0], c);
1285 rc = llapi_quotachown(argv[optind], flag);
1287 fprintf(stderr,"error: change file owner/group failed.\n");
1292 static int lfs_quotacheck(int argc, char **argv)
1294 int c, check_type = 0;
1296 struct if_quotacheck qchk;
1297 struct if_quotactl qctl;
1298 char *obd_type = qchk.obd_type;
1299 char *obd_uuid = qchk.obd_uuid.uuid;
1302 memset(&qchk, 0, sizeof(qchk));
1305 while ((c = getopt(argc, argv, "ug")) != -1) {
1314 fprintf(stderr, "error: %s: option '-%c' "
1315 "unrecognized\n", argv[0], c);
1322 else /* do quotacheck for both user & group quota by default */
1330 memset(&qctl, 0, sizeof(qctl));
1331 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1332 qctl.qc_id = QFMT_LDISKFS;
1333 qctl.qc_type = check_type;
1334 rc = llapi_quotactl(mnt, &qctl);
1336 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1340 rc = llapi_quotacheck(mnt, check_type);
1342 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1346 rc = llapi_poll_quotacheck(mnt, &qchk);
1349 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1350 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1354 memset(&qctl, 0, sizeof(qctl));
1355 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1356 qctl.qc_id = QFMT_LDISKFS;
1357 qctl.qc_type = check_type;
1358 rc = llapi_quotactl(mnt, &qctl);
1361 fprintf(stderr, "%s %s ",
1362 qctl.obd_type, qctl.obd_uuid.uuid);
1363 fprintf(stderr, "%s turn on quota failed: %s\n",
1364 argv[0], strerror(errno));
1371 static int lfs_quotaon(int argc, char **argv)
1375 struct if_quotactl qctl;
1376 char *obd_type = qctl.obd_type;
1377 char *obd_uuid = qctl.obd_uuid.uuid;
1380 memset(&qctl, 0, sizeof(qctl));
1381 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1382 qctl.qc_id = QFMT_LDISKFS;
1385 while ((c = getopt(argc, argv, "ugf")) != -1) {
1388 qctl.qc_type |= 0x01;
1391 qctl.qc_type |= 0x02;
1394 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1397 fprintf(stderr, "error: %s: option '-%c' "
1398 "unrecognized\n", argv[0], c);
1405 else /* by default, enable quota for both user & group */
1406 qctl.qc_type = 0x02;
1413 rc = llapi_quotactl(mnt, &qctl);
1416 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1417 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1424 static int lfs_quotaoff(int argc, char **argv)
1428 struct if_quotactl qctl;
1429 char *obd_type = qctl.obd_type;
1430 char *obd_uuid = qctl.obd_uuid.uuid;
1433 memset(&qctl, 0, sizeof(qctl));
1434 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1437 while ((c = getopt(argc, argv, "ug")) != -1) {
1440 qctl.qc_type |= 0x01;
1443 qctl.qc_type |= 0x02;
1446 fprintf(stderr, "error: %s: option '-%c' "
1447 "unrecognized\n", argv[0], c);
1454 else /* by default, disable quota for both user & group */
1455 qctl.qc_type = 0x02;
1462 rc = llapi_quotactl(mnt, &qctl);
1465 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1466 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1473 #define ARG2INT(nr, str, msg) \
1476 nr = strtol(str, &endp, 0); \
1478 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1483 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1485 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1486 * returns the value or ULONG_MAX on integer overflow or incorrect format
1488 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1489 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1490 * 3. empty integer value is interpreted as 0
1493 static unsigned long str2sec(const char* timestr) {
1494 const char spec[] = "smhdw";
1495 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1496 unsigned long val = 0;
1499 if (strpbrk(timestr, spec) == NULL) {
1500 /* no specifiers inside the time string,
1501 should treat it as an integer value */
1502 val = strtoul(timestr, &tail, 10);
1503 return *tail ? ULONG_MAX : val;
1506 /* format string is XXwXXdXXhXXmXXs */
1512 v = strtoul(timestr, &tail, 10);
1513 if (v == ULONG_MAX || *tail == '\0')
1514 /* value too large (ULONG_MAX or more)
1515 or missing specifier */
1518 ptr = strchr(spec, *tail);
1520 /* unknown specifier */
1525 /* check if product will overflow the type */
1526 if (!(v < ULONG_MAX / mult[ind]))
1529 ADD_OVERFLOW(val, mult[ind] * v);
1530 if (val == ULONG_MAX)
1542 int lfs_setquota(int argc, char **argv)
1546 struct if_quotactl qctl;
1547 char *obd_type = qctl.obd_type;
1548 char *obd_uuid = qctl.obd_uuid.uuid;
1551 memset(&qctl, 0, sizeof(qctl));
1552 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1555 while ((c = getopt(argc, argv, "ugt")) != -1) {
1558 qctl.qc_type |= 0x01;
1561 qctl.qc_type |= 0x02;
1564 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1567 fprintf(stderr, "error: %s: option '-%c' "
1568 "unrecognized\n", argv[0], c);
1576 if (qctl.qc_type == UGQUOTA) {
1577 fprintf(stderr, "error: user and group quotas can't be set "
1582 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1583 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1585 if (optind + 6 != argc)
1588 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1590 fprintf(stderr, "error: find id for name %s failed: %s\n",
1591 argv[optind - 1], strerror(errno));
1595 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1596 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1597 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1598 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1600 dqb->dqb_valid = QIF_LIMITS;
1602 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1604 if (optind + 3 != argc)
1607 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1608 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1611 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1612 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1619 rc = llapi_quotactl(mnt, &qctl);
1622 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1623 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1630 static inline char *type2name(int check_type)
1632 if (check_type == USRQUOTA)
1634 else if (check_type == GRPQUOTA)
1641 /* Converts seconds value into format string
1642 * result is returned in buf
1644 * 1. result is in descenting order: 1w2d3h4m5s
1645 * 2. zero fields are not filled (except for p. 3): 5d1s
1646 * 3. zero seconds value is presented as "0s"
1648 static void sec2str(time_t seconds, char *buf)
1650 const char spec[] = "smhdw";
1651 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1656 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1657 c = seconds / mult[i];
1659 if (c > 0 || (i == 0 && buf == tail))
1660 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1667 static void diff2str(time_t seconds, char *buf, time_t now)
1673 if (seconds <= now) {
1674 strcpy(buf, "none");
1677 sec2str(seconds - now, buf);
1680 static void print_quota_title(char *name, struct if_quotactl *qctl)
1682 printf("Disk quotas for %s %s (%cid %u):\n",
1683 type2name(qctl->qc_type), name,
1684 *type2name(qctl->qc_type), qctl->qc_id);
1685 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1687 "kbytes", "quota", "limit", "grace",
1688 "files", "quota", "limit", "grace");
1691 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1697 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1698 int bover = 0, iover = 0;
1699 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1701 if (dqb->dqb_bhardlimit &&
1702 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1704 } else if (dqb->dqb_bsoftlimit &&
1705 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1706 if (dqb->dqb_btime > now) {
1713 if (dqb->dqb_ihardlimit &&
1714 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1716 } else if (dqb->dqb_isoftlimit &&
1717 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1718 if (dqb->dqb_btime > now) {
1725 #if 0 /* XXX: always print quotas even when no usages */
1726 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1732 if (strlen(mnt) > 15)
1733 printf("%s\n%15s", mnt, "");
1735 printf("%15s", mnt);
1738 diff2str(dqb->dqb_btime, timebuf, now);
1740 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1741 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1742 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1743 printf(" %7s%c %6s %7s %7s",
1744 numbuf[0], bover ? '*' : ' ', numbuf[1],
1745 numbuf[2], bover > 1 ? timebuf : "");
1748 diff2str(dqb->dqb_itime, timebuf, now);
1750 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1751 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1752 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1754 printf(" %7s%c %6s %7s %7s",
1755 numbuf[0], iover ? '*' : ' ', numbuf[1],
1756 numbuf[2], iover > 1 ? timebuf : "");
1759 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1760 qctl->qc_cmd == Q_GETOINFO) {
1764 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1765 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1766 printf("Block grace time: %s; Inode grace time: %s\n",
1767 bgtimebuf, igtimebuf);
1771 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1775 /* XXX: this is a flag to mark that only mds quota is wanted */
1776 qctl->qc_dqblk.dqb_valid = 1;
1777 rc = llapi_quotactl(mnt, qctl);
1779 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1782 qctl->qc_dqblk.dqb_valid = 0;
1784 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1787 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1790 struct obd_uuid *uuids = NULL, *uuidp;
1791 int obdcount = 1024;
1796 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1800 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1801 sizeof(struct obd_uuid));
1806 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1808 struct obd_uuid *uuids_temp;
1810 if (rc == -EOVERFLOW) {
1811 uuids_temp = realloc(uuids, obdcount *
1812 sizeof(struct obd_uuid));
1813 if (uuids_temp != NULL)
1814 goto retry_get_uuids;
1819 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1823 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1824 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1826 /* XXX clear this flag to get quota from osts */
1827 qctl->qc_dqblk.dqb_valid = 0;
1828 rc = llapi_quotactl(mnt, qctl);
1830 fprintf(stderr, "%s quotactl failed: %s\n",
1831 uuidp->uuid, strerror(errno));
1835 print_quota(uuidp->uuid, qctl, 1);
1843 static int lfs_quota(int argc, char **argv)
1846 char *name = NULL, *mnt;
1847 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1849 char *obd_type = qctl.obd_type;
1850 char *obd_uuid = qctl.obd_uuid.uuid;
1854 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1857 qctl.qc_type = 0x01;
1860 qctl.qc_type = 0x02;
1863 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1866 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1869 fprintf(stderr, "error: %s: option '-%c' "
1870 "unrecognized\n", argv[0], c);
1879 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1880 if (optind + 2 != argc) {
1881 fprintf(stderr, "error: missing quota argument(s)\n");
1885 name = argv[optind++];
1886 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1888 fprintf(stderr,"error: can't find id for name %s: %s\n",
1889 name, strerror(errno));
1892 print_quota_title(name, &qctl);
1893 } else if (optind + 1 != argc) {
1894 fprintf(stderr, "error: missing quota info argument(s)\n");
1900 rc = llapi_quotactl(mnt, &qctl);
1903 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1904 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1909 rc = id2name(&name, getuid(), qctl.qc_type);
1916 print_quota(mnt, &qctl, 0);
1918 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1919 print_mds_quota(mnt, &qctl);
1920 print_lov_quota(mnt, &qctl);
1925 #endif /* HAVE_QUOTA_SUPPORT */
1927 static int flushctx_ioctl(char *mp)
1931 fd = open(mp, O_RDONLY);
1933 fprintf(stderr, "flushctx: error open %s: %s\n",
1934 mp, strerror(errno));
1938 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1940 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1941 mp, strerror(errno));
1947 static int lfs_flushctx(int argc, char **argv)
1949 int kdestroy = 0, c;
1951 char procline[PATH_MAX], *line;
1955 while ((c = getopt(argc, argv, "k")) != -1) {
1961 fprintf(stderr, "error: %s: option '-%c' "
1962 "unrecognized\n", argv[0], c);
1968 system("kdestroy > /dev/null");
1970 if (optind >= argc) {
1971 /* flush for all mounted lustre fs. */
1972 proc = fopen("/proc/mounts", "r");
1974 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1979 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1984 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1985 fprintf(stderr, "%s: unexpected format in "
1991 if (strcmp(fs, "lustre") != 0)
1993 /* we use '@' to determine it's a client. are there
1994 * any other better way?
1996 if (strchr(dev, '@') == NULL)
1999 if (flushctx_ioctl(mp))
2003 /* flush fs as specified */
2004 while (optind < argc) {
2005 if (flushctx_ioctl(argv[optind++]))
2013 static int lfs_lsetfacl(int argc, char **argv)
2016 return(llapi_lsetfacl(argc, argv));
2019 static int lfs_lgetfacl(int argc, char **argv)
2022 return(llapi_lgetfacl(argc, argv));
2025 static int lfs_rsetfacl(int argc, char **argv)
2028 return(llapi_rsetfacl(argc, argv));
2031 static int lfs_rgetfacl(int argc, char **argv)
2034 return(llapi_rgetfacl(argc, argv));
2037 static int lfs_cp(int argc, char **argv)
2039 return(llapi_cp(argc, argv));
2042 static int lfs_ls(int argc, char **argv)
2044 return(llapi_ls(argc, argv));
2047 int main(int argc, char **argv)
2053 ptl_initialize(argc, argv);
2054 if (obd_initialize(argc, argv) < 0)
2056 if (dbg_initialize(argc, argv) < 0)
2059 Parser_init("lfs > ", cmdlist);
2062 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2064 rc = Parser_commands();
2067 obd_finalize(argc, argv);