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>
51 #include <sys/types.h>
60 #include <lnet/api-support.h>
61 #include <lnet/lnetctl.h>
63 #include <liblustre.h>
64 #include <lustre/lustre_idl.h>
65 #include <lustre/liblustreapi.h>
66 #include <lustre/lustre_user.h>
68 #include <libcfs/libcfsutil.h>
71 unsigned int libcfs_subsystem_debug = 0;
74 static int lfs_setstripe(int argc, char **argv);
75 static int lfs_find(int argc, char **argv);
76 static int lfs_getstripe(int argc, char **argv);
77 static int lfs_osts(int argc, char **argv);
78 static int lfs_df(int argc, char **argv);
79 static int lfs_check(int argc, char **argv);
80 static int lfs_catinfo(int argc, char **argv);
81 #ifdef HAVE_QUOTA_SUPPORT
82 static int lfs_quotachown(int argc, char **argv);
83 static int lfs_quotacheck(int argc, char **argv);
84 static int lfs_quotaon(int argc, char **argv);
85 static int lfs_quotaoff(int argc, char **argv);
86 static int lfs_setquota(int argc, char **argv);
87 static int lfs_quota(int argc, char **argv);
89 static int lfs_flushctx(int argc, char **argv);
90 static int lfs_join(int argc, char **argv);
91 static int lfs_lsetfacl(int argc, char **argv);
92 static int lfs_lgetfacl(int argc, char **argv);
93 static int lfs_rsetfacl(int argc, char **argv);
94 static int lfs_rgetfacl(int argc, char **argv);
95 static int lfs_cp(int argc, char **argv);
96 static int lfs_ls(int argc, char **argv);
98 /* all avaialable commands */
99 command_t cmdlist[] = {
100 {"setstripe", lfs_setstripe, 0,
101 "Create a new file with a specific striping pattern or\n"
102 "set the default striping pattern on an existing directory or\n"
103 "delete the default striping pattern from an existing directory\n"
104 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
106 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
107 " [--index|-i stripe_index]\n"
108 " [--count|-c stripe_count]\n"
110 " setstripe -d <dirname> (to delete default striping)\n"
111 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
112 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
113 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
114 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
115 {"getstripe", lfs_getstripe, 0,
116 "To list the striping info for a given filename or files in a\n"
117 "directory or recursively for all files in a directory tree.\n"
118 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
119 " [--recursive | -r] <dir|file> ..."},
120 {"find", lfs_find, 0,
121 "To find files that match given parameters recursively in a directory tree.\n"
122 "usage: find <dir/file> ... \n"
123 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
124 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
125 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
126 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
127 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
128 "\t !: used before an option indicates 'NOT' the requested attribute\n"
129 "\t -: used before an value indicates 'AT MOST' the requested value\n"
130 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
131 {"check", lfs_check, 0,
132 "Display the status of MDS or OSTs (as specified in the command)\n"
133 "or all the servers (MDS and OSTs).\n"
134 "usage: check <osts|mds|servers>"},
135 {"catinfo", lfs_catinfo, 0,
136 "Show information of specified type logs.\n"
137 "usage: catinfo {keyword} [node name]\n"
138 "\tkeywords are one of followings: config, deletions.\n"
139 "\tnode name must be provided when use keyword config."},
140 {"join", lfs_join, 0,
141 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
142 "usage: join <filename_A> <filename_B>\n"},
143 {"osts", lfs_osts, 0, "osts"},
145 "report filesystem disk space usage or inodes usage"
147 "Usage: df [-i] [-h] [path]"},
148 #ifdef HAVE_QUOTA_SUPPORT
149 {"quotachown",lfs_quotachown, 0,
150 "Change files' owner or group on the specified filesystem.\n"
151 "usage: quotachown [-i] <filesystem>\n"
152 "\t-i: ignore error if file is not exist\n"},
153 {"quotacheck", lfs_quotacheck, 0,
154 "Scan the specified filesystem for disk usage, and create,\n"
155 "or update quota files.\n"
156 "usage: quotacheck [ -ug ] <filesystem>"},
157 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
158 "usage: quotaon [ -ugf ] <filesystem>"},
159 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
160 "usage: quotaoff [ -ug ] <filesystem>"},
161 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
162 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
163 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
164 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
165 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
167 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
168 "usage: flushctx [-k] [mountpoint...]"},
169 {"lsetfacl", lfs_lsetfacl, 0,
170 "Remote user setfacl for user/group on the same remote client.\n"
171 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
172 {"lgetfacl", lfs_lgetfacl, 0,
173 "Remote user getfacl for user/group on the same remote client.\n"
174 "usage: lgetfacl [-dRLPvh] file ..."},
175 {"rsetfacl", lfs_rsetfacl, 0,
176 "Remote user setfacl for user/group on other clients.\n"
177 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
178 {"rgetfacl", lfs_rgetfacl, 0,
179 "Remote user getfacl for user/group on other clients.\n"
180 "usage: rgetfacl [-dRLPvh] file ..."},
182 "Remote user copy files and directories.\n"
183 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
185 "Remote user list directory contents.\n"
186 "usage: ls [OPTION]... [FILE]..."},
187 {"help", Parser_help, 0, "help"},
188 {"exit", Parser_quit, 0, "quit"},
189 {"quit", Parser_quit, 0, "quit"},
194 static int lfs_setstripe(int argc, char **argv)
198 unsigned long long st_size;
199 int st_offset, st_count;
203 char *stripe_size_arg = NULL;
204 char *stripe_off_arg = NULL;
205 char *stripe_count_arg = NULL;
206 unsigned long long size_units;
208 struct option long_opts[] = {
209 {"size", required_argument, 0, 's'},
210 {"count", required_argument, 0, 'c'},
211 {"index", required_argument, 0, 'i'},
212 {"delete", no_argument, 0, 'd'},
219 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
220 /* for compatibility with the existing positional parameter
224 } else if (argc == 5 &&
225 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
226 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
227 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
228 /* for compatibility with the existing positional parameter
231 stripe_size_arg = argv[2];
232 stripe_off_arg = argv[3];
233 stripe_count_arg = argv[4];
237 while ((c = getopt_long(argc, argv, "c:di:s:",
238 long_opts, NULL)) >= 0) {
244 stripe_count_arg = optarg;
247 /* delete the default striping pattern */
251 stripe_off_arg = optarg;
254 stripe_size_arg = optarg;
259 fprintf(stderr, "error: %s: option '%s' "
261 argv[0], argv[optind - 1]);
266 fname = argv[optind];
272 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
273 stripe_count_arg != NULL)) {
274 fprintf(stderr, "error: %s: cannot specify -d with "
275 "-s, -c or -i options\n",
280 if (optind != argc - 1) {
281 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
283 argv[0], argv[argc - 1]);
287 /* get the stripe size */
288 if (stripe_size_arg != NULL) {
289 result = parse_size(stripe_size_arg, &st_size, &size_units);
291 fprintf(stderr,"error: bad size '%s'\n",
296 /* get the stripe offset */
297 if (stripe_off_arg != NULL) {
298 st_offset = strtoul(stripe_off_arg, &end, 0);
300 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
301 argv[0], stripe_off_arg);
305 /* get the stripe count */
306 if (stripe_count_arg != NULL) {
307 st_count = strtoul(stripe_count_arg, &end, 0);
309 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
310 argv[0], stripe_count_arg);
315 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
317 fprintf(stderr, "error: %s: create stripe file failed\n",
323 static int set_time(time_t *time, time_t *set, char *str)
330 else if (str[0] == '-')
336 t = strtol(str, NULL, 0);
337 if (*time < t * 24 * 60 * 60) {
340 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
344 *set = *time - t * 24 * 60 * 60;
348 static int name2id(unsigned int *id, char *name, int type)
350 if (type == USRQUOTA) {
351 struct passwd *entry;
353 if (!(entry = getpwnam(name))) {
363 if (!(entry = getgrnam(name))) {
375 static int id2name(char **name, unsigned int id, int type)
377 if (type == USRQUOTA) {
378 struct passwd *entry;
380 if (!(entry = getpwuid(id))) {
386 *name = entry->pw_name;
390 if (!(entry = getgrgid(id))) {
396 *name = entry->gr_name;
402 static int lfs_find(int argc, char **argv)
407 struct find_param param = { .maxdepth = -1 };
409 struct option long_opts[] = {
410 /* New find options. */
411 {"atime", required_argument, 0, 'A'},
412 {"ctime", required_argument, 0, 'C'},
413 {"mtime", required_argument, 0, 'M'},
414 {"maxdepth", required_argument, 0, 'D'},
415 {"gid", required_argument, 0, 'g'},
416 {"group", required_argument, 0, 'G'},
417 {"uid", required_argument, 0, 'u'},
418 {"user", required_argument, 0, 'U'},
419 {"name", required_argument, 0, 'n'},
420 /* --obd is considered as a new option. */
421 {"obd", required_argument, 0, 'O'},
422 {"ost", required_argument, 0, 'O'},
423 {"print", no_argument, 0, 'P'},
424 {"print0", no_argument, 0, 'p'},
425 /* Old find options. */
426 {"quiet", no_argument, 0, 'q'},
427 {"recursive", no_argument, 0, 'r'},
428 {"size", required_argument, 0, 's'},
429 {"type", required_argument, 0, 't'},
430 {"verbose", no_argument, 0, 'v'},
444 while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
445 long_opts, NULL)) >= 0) {
450 /* '!' is part of option */
451 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
452 if (!isoption && pathend != -1) {
453 fprintf(stderr, "err: %s: filename|dirname must either "
454 "precede options or follow options\n",
458 if (!isoption && pathstart == -1)
459 pathstart = optind - 1;
460 if (isoption && pathstart != -1 && pathend == -1) {
461 pathend = optind - 2;
462 if ((c == 1 && strcmp(optarg, "!") == 0) ||
463 c == 'P' || c == 'p' || c == 'O' ||
464 c == 'q' || c == 'r' || c == 'v')
465 pathend = optind - 1;
472 if (strcmp(optarg, "!") == 0)
476 xtime = ¶m.atime;
477 xsign = ¶m.asign;
480 xtime = ¶m.ctime;
481 xsign = ¶m.csign;
485 xtime = ¶m.mtime;
486 xsign = ¶m.msign;
490 if (optarg[0] == '-')
492 else if (optarg[0] == '+')
501 ret = set_time(&t, xtime, optarg);
509 param.maxdepth = strtol(optarg, 0, 0);
513 param.gid = strtol(optarg, &endptr, 10);
514 if (optarg == endptr) {
515 fprintf(stderr, "Bad gid: %s\n", optarg);
518 param.exclude_gid = !!neg_opt;
523 param.gid = strtol(optarg, &endptr, 10);
524 if (optarg == endptr) {
525 ret = name2id(¶m.gid, optarg, GRPQUOTA);
527 fprintf(stderr, "Group/GID: %s cannot "
528 "be found.\n", optarg);
532 param.exclude_gid = !!neg_opt;
537 param.uid = strtol(optarg, &endptr, 10);
538 if (optarg == endptr) {
539 fprintf(stderr, "Bad uid: %s\n", optarg);
542 param.exclude_uid = !!neg_opt;
547 param.uid = strtol(optarg, &endptr, 10);
548 if (optarg == endptr) {
549 ret = name2id(¶m.uid, optarg, USRQUOTA);
551 fprintf(stderr, "User/UID: %s cannot "
552 "be found.\n", optarg);
556 param.exclude_uid = !!neg_opt;
561 param.pattern = (char *)optarg;
562 param.exclude_pattern = !!neg_opt;
565 char *buf, *token, *next, *p;
568 len = strlen((char *)optarg);
572 strcpy(buf, (char *)optarg);
574 if (param.num_alloc_obds == 0) {
575 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
576 sizeof(struct obd_uuid));
577 if (param.obduuid == NULL)
579 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
582 for (token = buf; token && *token; token = next) {
583 p = strchr(token, ',');
589 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
613 param.exclude_type = !!neg_opt;
615 case 'b': param.type = S_IFBLK; break;
616 case 'c': param.type = S_IFCHR; break;
617 case 'd': param.type = S_IFDIR; break;
618 case 'f': param.type = S_IFREG; break;
619 case 'l': param.type = S_IFLNK; break;
620 case 'p': param.type = S_IFIFO; break;
621 case 's': param.type = S_IFSOCK; break;
622 #ifdef S_IFDOOR /* Solaris only */
623 case 'D': param.type = S_IFDOOR; break;
625 default: fprintf(stderr, "error: %s: bad type '%s'\n",
632 if (optarg[0] == '-')
634 else if (optarg[0] == '+')
643 if (optarg[0] == '+')
644 param.size_sign = -1;
645 else if (optarg[0] == '-')
646 param.size_sign = +1;
650 ret = parse_size(optarg, ¶m.size,¶m.size_units);
652 fprintf(stderr,"error: bad size '%s'\n",
665 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
666 argv[0], argv[optind - 1]);
671 if (pathstart == -1) {
672 fprintf(stderr, "error: %s: no filename|pathname\n",
675 } else if (pathend == -1) {
683 static int deprecated_warning;
684 if (!deprecated_warning) {
685 fprintf(stderr, "lfs find: -q, -r, -v options "
686 "deprecated. Use 'lfs getstripe' instead.\n");
687 deprecated_warning = 1;
689 if (!param.recursive && param.maxdepth == -1)
695 ret = llapi_find(argv[pathstart], ¶m);
697 ret = llapi_getstripe(argv[pathstart], ¶m);
698 } while (++pathstart < pathend && !ret);
701 fprintf(stderr, "error: %s failed for %s.\n",
702 argv[0], argv[optind - 1]);
704 if (param.obduuid && param.num_alloc_obds)
710 static int lfs_getstripe(int argc, char **argv)
712 struct option long_opts[] = {
714 {"quiet", 0, 0, 'q'},
715 {"recursive", 0, 0, 'r'},
716 {"verbose", 0, 0, 'v'},
719 char short_opts[] = "hO:qrv";
721 struct find_param param = { 0 };
724 while ((c = getopt_long(argc, argv, short_opts,
725 long_opts, NULL)) != -1) {
730 "error: %s: only one obduuid allowed",
734 param.obduuid = (struct obd_uuid *)optarg;
750 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
751 argv[0], argv[optind - 1]);
759 param.maxdepth = param.recursive ? -1 : 1;
762 rc = llapi_getstripe(argv[optind], ¶m);
763 } while (++optind < argc && !rc);
766 fprintf(stderr, "error: %s failed for %s.\n",
767 argv[0], argv[optind - 1]);
771 static int lfs_osts(int argc, char **argv)
774 struct mntent *mnt = NULL;
775 struct find_param param;
781 fp = setmntent(MOUNTED, "r");
784 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
788 while (feof(fp) == 0 && ferror(fp) ==0) {
789 memset(¶m, 0, sizeof(param));
790 if (llapi_is_lustre_mnt(mnt)) {
791 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
794 "error: %s: failed on %s\n",
795 argv[0], mnt->mnt_dir);
805 #define COOK(value) \
808 while (value > 1024) { \
822 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
824 char rpath[PATH_MAX] = {'\0'};
826 int rc, len, out_len = 0;
828 if (!realpath(path, rpath)) {
830 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
831 path, strerror(-rc));
837 while (feof(fp) == 0 && ferror(fp) == 0) {
838 if (llapi_is_lustre_mnt(mnt)) {
839 len = strlen(mnt->mnt_dir);
841 !strncmp(rpath, mnt->mnt_dir, len)) {
843 memset(mntdir, 0, dir_len);
844 strncpy(mntdir, mnt->mnt_dir, dir_len);
853 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
857 static int showdf(char *mntdir, struct obd_statfs *stat,
858 char *uuid, int ishow, int cooked,
859 char *type, int index, int rc)
861 long long avail, used, total;
863 char *suffix = "KMGTPEZY";
864 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
865 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
873 avail = stat->os_ffree;
874 used = stat->os_files - stat->os_ffree;
875 total = stat->os_files;
877 int shift = cooked ? 0 : 10;
879 avail = (stat->os_bavail * stat->os_bsize) >> shift;
880 used = ((stat->os_blocks - stat->os_bfree) *
881 stat->os_bsize) >> shift;
882 total = (stat->os_blocks * stat->os_bsize) >> shift;
886 ratio = (double)used / (double)total;
892 cook_val = (double)total;
895 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
897 sprintf(tbuf, CDF, total);
899 cook_val = (double)used;
902 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
904 sprintf(ubuf, CDF, used);
906 cook_val = (double)avail;
909 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
911 sprintf(abuf, CDF, avail);
913 sprintf(tbuf, CDF, total);
914 sprintf(ubuf, CDF, used);
915 sprintf(abuf, CDF, avail);
918 sprintf(rbuf, RDF, (int)(ratio * 100));
919 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
920 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
922 printf("[%s:%d]\n", type, index);
928 printf(UUF": inactive device\n", uuid);
931 printf(UUF": %s\n", uuid, strerror(-rc));
938 static int mntdf(char *mntdir, int ishow, int cooked)
940 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
941 struct obd_uuid uuid_buf;
946 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
947 "UUID", "Inodes", "IUsed", "IFree",
948 "IUse%", "Mounted on");
950 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
951 "UUID", cooked ? "bytes" : "1K-blocks",
952 "Used", "Available", "Use%", "Mounted on");
954 for (index = 0; ; index++) {
955 memset(&stat_buf, 0, sizeof(struct obd_statfs));
956 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
957 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
958 &stat_buf, &uuid_buf);
965 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
966 rc == -ENODATA || rc == 0) {
967 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
971 "error: llapi_obd_statfs(%s): %s (%d)\n",
972 uuid_buf.uuid, strerror(-rc), rc);
976 sum.os_ffree += stat_buf.os_ffree;
977 sum.os_files += stat_buf.os_files;
981 for (index = 0; ; index++) {
982 memset(&stat_buf, 0, sizeof(struct obd_statfs));
983 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
984 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
985 &stat_buf, &uuid_buf);
992 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
993 rc == -ENODATA || rc == 0) {
994 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
998 "error: llapi_obd_statfs failed: %s (%d)\n",
1003 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1004 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1005 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1010 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1015 static int lfs_df(int argc, char **argv)
1019 struct mntent *mnt = NULL;
1020 char *mntdir = NULL;
1021 int ishow = 0, cooked = 0;
1025 while ((c = getopt(argc, argv, "ih")) != -1) {
1038 path = argv[optind];
1040 fp = setmntent(MOUNTED, "r");
1043 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1044 argv[0], MOUNTED, strerror(errno));
1048 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1049 fprintf(stderr, "error: cannot allocate %d bytes\n",
1053 memset(mntdir, 0, PATH_MAX);
1056 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1063 rc = mntdf(mntdir, ishow, cooked);
1067 mnt = getmntent(fp);
1068 while (feof(fp) == 0 && ferror(fp) == 0) {
1069 if (llapi_is_lustre_mnt(mnt)) {
1070 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1075 mnt = getmntent(fp);
1084 static int lfs_check(int argc, char **argv)
1088 struct mntent *mnt = NULL;
1097 obd_types[0] = obd_type1;
1098 obd_types[1] = obd_type2;
1100 if (strcmp(argv[1], "osts") == 0) {
1101 strcpy(obd_types[0], "osc");
1102 } else if (strcmp(argv[1], "mds") == 0) {
1103 strcpy(obd_types[0], "mdc");
1104 } else if (strcmp(argv[1], "servers") == 0) {
1106 strcpy(obd_types[0], "osc");
1107 strcpy(obd_types[1], "mdc");
1109 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1114 fp = setmntent(MOUNTED, "r");
1116 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1119 mnt = getmntent(fp);
1120 while (feof(fp) == 0 && ferror(fp) ==0) {
1121 if (llapi_is_lustre_mnt(mnt))
1123 mnt = getmntent(fp);
1129 fprintf(stderr, "No suitable Lustre mount found\n");
1133 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1136 fprintf(stderr, "error: %s: %s status failed\n",
1143 static int lfs_catinfo(int argc, char **argv)
1146 struct mntent *mnt = NULL;
1149 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1152 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1155 fp = setmntent(MOUNTED, "r");
1157 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1160 mnt = getmntent(fp);
1161 while (feof(fp) == 0 && ferror(fp) == 0) {
1162 if (llapi_is_lustre_mnt(mnt))
1164 mnt = getmntent(fp);
1171 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1173 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1175 fprintf(stderr, "no lustre_lite mounted.\n");
1182 int lfs_join(int argc, char **argv)
1184 char *name_head, *name_tail;
1190 name_head = argv[1];
1191 fd = open(name_head, O_WRONLY);
1193 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1197 size = lseek(fd, 0, SEEK_END);
1198 if (size % JOIN_FILE_ALIGN) {
1199 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1200 name_head, (long long)size, JOIN_FILE_ALIGN);
1204 name_tail = argv[2];
1205 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1209 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1215 #ifdef HAVE_QUOTA_SUPPORT
1216 static int lfs_quotachown(int argc, char **argv)
1223 while ((c = getopt(argc, argv, "i")) != -1) {
1229 fprintf(stderr, "error: %s: option '-%c' "
1230 "unrecognized\n", argv[0], c);
1236 rc = llapi_quotachown(argv[optind], flag);
1238 fprintf(stderr,"error: change file owner/group failed.\n");
1243 static int lfs_quotacheck(int argc, char **argv)
1245 int c, check_type = 0;
1247 struct if_quotacheck qchk;
1248 struct if_quotactl qctl;
1249 char *obd_type = qchk.obd_type;
1250 char *obd_uuid = qchk.obd_uuid.uuid;
1253 memset(&qchk, 0, sizeof(qchk));
1256 while ((c = getopt(argc, argv, "ug")) != -1) {
1265 fprintf(stderr, "error: %s: option '-%c' "
1266 "unrecognized\n", argv[0], c);
1273 else /* do quotacheck for both user & group quota by default */
1281 memset(&qctl, 0, sizeof(qctl));
1282 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1283 qctl.qc_id = QFMT_LDISKFS;
1284 qctl.qc_type = check_type;
1285 rc = llapi_quotactl(mnt, &qctl);
1287 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1291 rc = llapi_quotacheck(mnt, check_type);
1293 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1297 rc = llapi_poll_quotacheck(mnt, &qchk);
1300 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1301 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1305 memset(&qctl, 0, sizeof(qctl));
1306 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1307 qctl.qc_id = QFMT_LDISKFS;
1308 qctl.qc_type = check_type;
1309 rc = llapi_quotactl(mnt, &qctl);
1312 fprintf(stderr, "%s %s ",
1313 qctl.obd_type, qctl.obd_uuid.uuid);
1314 fprintf(stderr, "%s turn on quota failed: %s\n",
1315 argv[0], strerror(errno));
1322 static int lfs_quotaon(int argc, char **argv)
1326 struct if_quotactl qctl;
1327 char *obd_type = qctl.obd_type;
1328 char *obd_uuid = qctl.obd_uuid.uuid;
1331 memset(&qctl, 0, sizeof(qctl));
1332 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1333 qctl.qc_id = QFMT_LDISKFS;
1336 while ((c = getopt(argc, argv, "ugf")) != -1) {
1339 qctl.qc_type |= 0x01;
1342 qctl.qc_type |= 0x02;
1345 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1348 fprintf(stderr, "error: %s: option '-%c' "
1349 "unrecognized\n", argv[0], c);
1356 else /* by default, enable quota for both user & group */
1357 qctl.qc_type = 0x02;
1364 rc = llapi_quotactl(mnt, &qctl);
1367 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1368 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1375 static int lfs_quotaoff(int argc, char **argv)
1379 struct if_quotactl qctl;
1380 char *obd_type = qctl.obd_type;
1381 char *obd_uuid = qctl.obd_uuid.uuid;
1384 memset(&qctl, 0, sizeof(qctl));
1385 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1388 while ((c = getopt(argc, argv, "ug")) != -1) {
1391 qctl.qc_type |= 0x01;
1394 qctl.qc_type |= 0x02;
1397 fprintf(stderr, "error: %s: option '-%c' "
1398 "unrecognized\n", argv[0], c);
1405 else /* by default, disable 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, "quotaoff failed: %s\n", strerror(errno));
1424 #define ARG2INT(nr, str, msg) \
1427 nr = strtol(str, &endp, 0); \
1429 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1434 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1436 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1437 * returns the value or ULONG_MAX on integer overflow or incorrect format
1439 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1440 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1441 * 3. empty integer value is interpreted as 0
1444 static unsigned long str2sec(const char* timestr) {
1445 const char spec[] = "smhdw";
1446 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1447 unsigned long val = 0;
1450 if (strpbrk(timestr, spec) == NULL) {
1451 /* no specifiers inside the time string,
1452 should treat it as an integer value */
1453 val = strtoul(timestr, &tail, 10);
1454 return *tail ? ULONG_MAX : val;
1457 /* format string is XXwXXdXXhXXmXXs */
1463 v = strtoul(timestr, &tail, 10);
1464 if (v == ULONG_MAX || *tail == '\0')
1465 /* value too large (ULONG_MAX or more)
1466 or missing specifier */
1469 ptr = strchr(spec, *tail);
1471 /* unknown specifier */
1476 /* check if product will overflow the type */
1477 if (!(v < ULONG_MAX / mult[ind]))
1480 ADD_OVERFLOW(val, mult[ind] * v);
1481 if (val == ULONG_MAX)
1493 int lfs_setquota(int argc, char **argv)
1497 struct if_quotactl qctl;
1498 char *obd_type = qctl.obd_type;
1499 char *obd_uuid = qctl.obd_uuid.uuid;
1502 memset(&qctl, 0, sizeof(qctl));
1503 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1506 while ((c = getopt(argc, argv, "ugt")) != -1) {
1509 qctl.qc_type |= 0x01;
1512 qctl.qc_type |= 0x02;
1515 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1518 fprintf(stderr, "error: %s: option '-%c' "
1519 "unrecognized\n", argv[0], c);
1527 if (qctl.qc_type == UGQUOTA) {
1528 fprintf(stderr, "error: user and group quotas can't be set "
1533 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1534 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1536 if (optind + 6 != argc)
1539 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1541 fprintf(stderr, "error: find id for name %s failed: %s\n",
1542 argv[optind - 1], strerror(errno));
1546 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1547 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1548 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1549 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1551 dqb->dqb_valid = QIF_LIMITS;
1553 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1555 if (optind + 3 != argc)
1558 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1559 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1562 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1563 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1570 rc = llapi_quotactl(mnt, &qctl);
1573 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1574 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1581 static inline char *type2name(int check_type)
1583 if (check_type == USRQUOTA)
1585 else if (check_type == GRPQUOTA)
1592 /* Converts seconds value into format string
1593 * result is returned in buf
1595 * 1. result is in descenting order: 1w2d3h4m5s
1596 * 2. zero fields are not filled (except for p. 3): 5d1s
1597 * 3. zero seconds value is presented as "0s"
1599 static void sec2str(time_t seconds, char *buf)
1601 const char spec[] = "smhdw";
1602 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1607 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1608 c = seconds / mult[i];
1610 if (c > 0 || (i == 0 && buf == tail))
1611 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1618 static void diff2str(time_t seconds, char *buf, time_t now)
1624 if (seconds <= now) {
1625 strcpy(buf, "none");
1628 sec2str(seconds - now, buf);
1631 static void print_quota_title(char *name, struct if_quotactl *qctl)
1633 printf("Disk quotas for %s %s (%cid %u):\n",
1634 type2name(qctl->qc_type), name,
1635 *type2name(qctl->qc_type), qctl->qc_id);
1636 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1638 "kbytes", "quota", "limit", "grace",
1639 "files", "quota", "limit", "grace");
1642 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1648 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1649 int bover = 0, iover = 0;
1650 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1652 if (dqb->dqb_bhardlimit &&
1653 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1655 } else if (dqb->dqb_bsoftlimit &&
1656 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1657 if (dqb->dqb_btime > now) {
1664 if (dqb->dqb_ihardlimit &&
1665 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1667 } else if (dqb->dqb_isoftlimit &&
1668 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1669 if (dqb->dqb_btime > now) {
1676 #if 0 /* XXX: always print quotas even when no usages */
1677 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1683 if (strlen(mnt) > 15)
1684 printf("%s\n%15s", mnt, "");
1686 printf("%15s", mnt);
1689 diff2str(dqb->dqb_btime, timebuf, now);
1691 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1692 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1693 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1694 printf(" %7s%c %6s %7s %7s",
1695 numbuf[0], bover ? '*' : ' ', numbuf[1],
1696 numbuf[2], bover > 1 ? timebuf : "");
1699 diff2str(dqb->dqb_itime, timebuf, now);
1701 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1702 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1703 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1705 printf(" %7s%c %6s %7s %7s",
1706 numbuf[0], iover ? '*' : ' ', numbuf[1],
1707 numbuf[2], iover > 1 ? timebuf : "");
1710 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1711 qctl->qc_cmd == Q_GETOINFO) {
1715 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1716 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1717 printf("Block grace time: %s; Inode grace time: %s\n",
1718 bgtimebuf, igtimebuf);
1722 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1726 /* XXX: this is a flag to mark that only mds quota is wanted */
1727 qctl->qc_dqblk.dqb_valid = 1;
1728 rc = llapi_quotactl(mnt, qctl);
1730 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1733 qctl->qc_dqblk.dqb_valid = 0;
1735 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1738 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1741 struct obd_uuid *uuids = NULL, *uuidp;
1742 int obdcount = 1024;
1747 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1751 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1752 sizeof(struct obd_uuid));
1757 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1759 struct obd_uuid *uuids_temp;
1761 if (rc == -EOVERFLOW) {
1762 uuids_temp = realloc(uuids, obdcount *
1763 sizeof(struct obd_uuid));
1764 if (uuids_temp != NULL)
1765 goto retry_get_uuids;
1770 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1774 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1775 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1777 /* XXX clear this flag to get quota from osts */
1778 qctl->qc_dqblk.dqb_valid = 0;
1779 rc = llapi_quotactl(mnt, qctl);
1781 fprintf(stderr, "%s quotactl failed: %s\n",
1782 uuidp->uuid, strerror(errno));
1786 print_quota(uuidp->uuid, qctl, 1);
1794 static int lfs_quota(int argc, char **argv)
1797 char *name = NULL, *mnt;
1798 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1800 char *obd_type = qctl.obd_type;
1801 char *obd_uuid = qctl.obd_uuid.uuid;
1805 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1808 qctl.qc_type = 0x01;
1811 qctl.qc_type = 0x02;
1814 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1817 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1820 fprintf(stderr, "error: %s: option '-%c' "
1821 "unrecognized\n", argv[0], c);
1830 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1831 if (optind + 2 != argc) {
1832 fprintf(stderr, "error: missing quota argument(s)\n");
1836 name = argv[optind++];
1837 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1839 fprintf(stderr,"error: can't find id for name %s: %s\n",
1840 name, strerror(errno));
1843 print_quota_title(name, &qctl);
1844 } else if (optind + 1 != argc) {
1845 fprintf(stderr, "error: missing quota info argument(s)\n");
1851 rc = llapi_quotactl(mnt, &qctl);
1854 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1855 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1860 rc = id2name(&name, getuid(), qctl.qc_type);
1867 print_quota(mnt, &qctl, 0);
1869 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1870 print_mds_quota(mnt, &qctl);
1871 print_lov_quota(mnt, &qctl);
1876 #endif /* HAVE_QUOTA_SUPPORT */
1878 static int flushctx_ioctl(char *mp)
1882 fd = open(mp, O_RDONLY);
1884 fprintf(stderr, "flushctx: error open %s: %s\n",
1885 mp, strerror(errno));
1889 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1891 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1892 mp, strerror(errno));
1898 static int lfs_flushctx(int argc, char **argv)
1900 int kdestroy = 0, c;
1902 char procline[PATH_MAX], *line;
1906 while ((c = getopt(argc, argv, "k")) != -1) {
1912 fprintf(stderr, "error: %s: option '-%c' "
1913 "unrecognized\n", argv[0], c);
1919 system("kdestroy > /dev/null");
1921 if (optind >= argc) {
1922 /* flush for all mounted lustre fs. */
1923 proc = fopen("/proc/mounts", "r");
1925 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1930 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1935 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1936 fprintf(stderr, "%s: unexpected format in "
1942 if (strcmp(fs, "lustre") != 0)
1944 /* we use '@' to determine it's a client. are there
1945 * any other better way?
1947 if (strchr(dev, '@') == NULL)
1950 if (flushctx_ioctl(mp))
1954 /* flush fs as specified */
1955 while (optind < argc) {
1956 if (flushctx_ioctl(argv[optind++]))
1964 static int lfs_lsetfacl(int argc, char **argv)
1967 return(llapi_lsetfacl(argc, argv));
1970 static int lfs_lgetfacl(int argc, char **argv)
1973 return(llapi_lgetfacl(argc, argv));
1976 static int lfs_rsetfacl(int argc, char **argv)
1979 return(llapi_rsetfacl(argc, argv));
1982 static int lfs_rgetfacl(int argc, char **argv)
1985 return(llapi_rgetfacl(argc, argv));
1988 static int lfs_cp(int argc, char **argv)
1990 return(llapi_cp(argc, argv));
1993 static int lfs_ls(int argc, char **argv)
1995 return(llapi_ls(argc, argv));
1998 int main(int argc, char **argv)
2004 ptl_initialize(argc, argv);
2005 if (obd_initialize(argc, argv) < 0)
2007 if (dbg_initialize(argc, argv) < 0)
2010 Parser_init("lfs > ", cmdlist);
2013 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2015 rc = Parser_commands();
2018 obd_finalize(argc, argv);