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>
62 #ifdef HAVE_SYS_QUOTA_H
63 # include <sys/quota.h>
69 #include <lnet/api-support.h>
70 #include <lnet/lnetctl.h>
72 #include <liblustre.h>
73 #include <lustre/lustre_idl.h>
74 #include <lustre/liblustreapi.h>
75 #include <lustre/lustre_user.h>
77 #include <libcfs/libcfsutil.h>
80 unsigned int libcfs_subsystem_debug = 0;
83 static int lfs_setstripe(int argc, char **argv);
84 static int lfs_find(int argc, char **argv);
85 static int lfs_getstripe(int argc, char **argv);
86 static int lfs_osts(int argc, char **argv);
87 static int lfs_df(int argc, char **argv);
88 static int lfs_check(int argc, char **argv);
89 static int lfs_catinfo(int argc, char **argv);
90 #ifdef HAVE_SYS_QUOTA_H
91 static int lfs_quotachown(int argc, char **argv);
92 static int lfs_quotacheck(int argc, char **argv);
93 static int lfs_quotaon(int argc, char **argv);
94 static int lfs_quotaoff(int argc, char **argv);
95 static int lfs_setquota(int argc, char **argv);
96 static int lfs_quota(int argc, char **argv);
97 static int lfs_quotainv(int argc, char **argv);
99 static int lfs_flushctx(int argc, char **argv);
100 static int lfs_join(int argc, char **argv);
101 static int lfs_lsetfacl(int argc, char **argv);
102 static int lfs_lgetfacl(int argc, char **argv);
103 static int lfs_rsetfacl(int argc, char **argv);
104 static int lfs_rgetfacl(int argc, char **argv);
105 static int lfs_cp(int argc, char **argv);
106 static int lfs_ls(int argc, char **argv);
107 static int lfs_poollist(int argc, char **argv);
108 static int lfs_path2fid(int argc, char **argv);
110 /* all avaialable commands */
111 command_t cmdlist[] = {
112 {"setstripe", lfs_setstripe, 0,
113 "Create a new file with a specific striping pattern or\n"
114 "set the default striping pattern on an existing directory or\n"
115 "delete the default striping pattern from an existing directory\n"
116 "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
117 " [--count|-c stripe_count] [--pool|-p pool_name]\n"
120 " setstripe -d <dir> (to delete default striping)\n"
121 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
122 "\t Can be specified with k, m or g (in KB, MB and GB\n"
124 "\tstart_ost: OST index of first stripe (-1 filesystem default)\n"
125 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
126 "\tpool_name: Name of OST pool"},
127 {"getstripe", lfs_getstripe, 0,
128 "To list the striping info for a given file or files in a\n"
129 "directory or recursively for all files in a directory tree.\n"
130 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
131 " [--recursive | -r] <dir|file> ..."},
132 {"poollist", lfs_poollist, 0,
133 "List pools or pool OSTs\n"
134 "usage: poollist <fsname>[.<poolname>] | <pathname>\n"},
135 {"find", lfs_find, 0,
136 "To find files that match given parameters recursively in a directory tree.\n"
137 "usage: find <dir|file> ... \n"
138 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
139 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
140 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
141 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
142 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
143 " [[!] --pool <name>]\n"
144 "\t !: used before an option indicates 'NOT' the requested attribute\n"
145 "\t -: used before an value indicates 'AT MOST' the requested value\n"
146 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
147 {"check", lfs_check, 0,
148 "Display the status of MDS or OSTs (as specified in the command)\n"
149 "or all the servers (MDS and OSTs).\n"
150 "usage: check <osts|mds|servers>"},
151 {"catinfo", lfs_catinfo, 0,
152 "Show information of specified type logs.\n"
153 "usage: catinfo {keyword} [node name]\n"
154 "\tkeywords are one of followings: config, deletions.\n"
155 "\tnode name must be provided when use keyword config."},
156 {"join", lfs_join, 0,
157 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
158 "usage: join <filename_A> <filename_B>\n"},
159 {"osts", lfs_osts, 0, "osts"},
161 "report filesystem disk space usage or inodes usage"
163 "Usage: df [-i] [-h] [path]"},
164 #ifdef HAVE_SYS_QUOTA_H
165 {"quotachown",lfs_quotachown, 0,
166 "Change files' owner or group on the specified filesystem.\n"
167 "usage: quotachown [-i] <filesystem>\n"
168 "\t-i: ignore error if file is not exist\n"},
169 {"quotacheck", lfs_quotacheck, 0,
170 "Scan the specified filesystem for disk usage, and create,\n"
171 "or update quota files.\n"
172 "usage: quotacheck [ -ug ] <filesystem>"},
173 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
174 "usage: quotaon [ -ugf ] <filesystem>"},
175 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
176 "usage: quotaoff [ -ug ] <filesystem>"},
177 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
178 "usage: setquota [ -u | -g ] <name> -b <block-softlimit> -B <block-hardlimit> -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
179 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>\n"
180 " setquota [ -u | --user | -g | --group ] <name>\n"
181 " [--block-softlimit <block-softlimit>]\n"
182 " [--block-hardlimit <block-hardlimit>]\n"
183 " [--inode-softlimit <inode-softlimit>]\n"
184 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
185 " setquota [-t] [ -u | --user | -g | --group ]\n"
186 " [--block-grace <block-grace>]\n"
187 " [--inode-grace <inode-grace>] <filesystem>\n"
188 " -b can be used instead of --block-softlimit/--block-grace\n"
189 " -B can be used instead of --block-hardlimit\n"
190 " -i can be used instead of --inode-softlimit/--inode-grace\n"
191 " -I can be used instead of --inode-hardlimit"},
192 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
193 "usage: quota [-v] [-o obd_uuid|-i mdt_idx|-I ost_idx] [{-u|-g <name>}|-t] <filesystem>"},
194 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
195 "usage: quotainv [-u|-g] <filesystem>"},
197 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
198 "usage: flushctx [-k] [mountpoint...]"},
199 {"lsetfacl", lfs_lsetfacl, 0,
200 "Remote user setfacl for user/group on the same remote client.\n"
201 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
202 {"lgetfacl", lfs_lgetfacl, 0,
203 "Remote user getfacl for user/group on the same remote client.\n"
204 "usage: lgetfacl [-dRLPvh] file ..."},
205 {"rsetfacl", lfs_rsetfacl, 0,
206 "Remote user setfacl for user/group on other clients.\n"
207 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
208 {"rgetfacl", lfs_rgetfacl, 0,
209 "Remote user getfacl for user/group on other clients.\n"
210 "usage: rgetfacl [-dRLPvh] file ..."},
212 "Remote user copy files and directories.\n"
213 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
215 "Remote user list directory contents.\n"
216 "usage: ls [OPTION]... [FILE]..."},
217 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
218 "usage: path2fid <path>"},
219 {"help", Parser_help, 0, "help"},
220 {"exit", Parser_quit, 0, "quit"},
221 {"quit", Parser_quit, 0, "quit"},
225 static int isnumber(const char *str)
229 if (str[0] != '-' && !isdigit(str[0]))
232 for (ptr = str + 1; *ptr != '\0'; ptr++) {
241 static int lfs_setstripe(int argc, char **argv)
245 unsigned long long st_size;
246 int st_offset, st_count;
250 char *stripe_size_arg = NULL;
251 char *stripe_off_arg = NULL;
252 char *stripe_count_arg = NULL;
253 char *pool_name_arg = NULL;
254 unsigned long long size_units;
256 struct option long_opts[] = {
257 {"size", required_argument, 0, 's'},
258 {"count", required_argument, 0, 'c'},
259 {"index", required_argument, 0, 'i'},
260 {"offset", required_argument, 0, 'o'},
261 {"pool", required_argument, 0, 'p'},
262 {"delete", no_argument, 0, 'd'},
270 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
271 if (argc == 5 && argv[1][0] != '-' &&
272 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
273 fprintf(stderr, "warning: deprecated usage of setstripe "
274 "positional parameters. Use -c, -i, -s instead.\n");
275 /* for compatibility with the existing positional parameter
278 stripe_size_arg = argv[2];
279 stripe_off_arg = argv[3];
280 stripe_count_arg = argv[4];
284 #warning "remove obsolete positional parameter code"
288 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
289 long_opts, NULL)) >= 0) {
295 stripe_count_arg = optarg;
298 /* delete the default striping pattern */
303 stripe_off_arg = optarg;
306 stripe_size_arg = optarg;
309 pool_name_arg = optarg;
314 fprintf(stderr, "error: %s: option '%s' "
316 argv[0], argv[optind - 1]);
321 fname = argv[optind];
324 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
325 stripe_count_arg != NULL || pool_name_arg != NULL)) {
326 fprintf(stderr, "error: %s: cannot specify -d with "
327 "-s, -c -o or -p options\n",
333 if (optind == argc) {
334 fprintf(stderr, "error: %s: missing filename|dirname\n",
339 /* get the stripe size */
340 if (stripe_size_arg != NULL) {
341 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
343 fprintf(stderr, "error: %s: bad size '%s'\n",
344 argv[0], stripe_size_arg);
348 /* get the stripe offset */
349 if (stripe_off_arg != NULL) {
350 st_offset = strtoul(stripe_off_arg, &end, 0);
352 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
353 argv[0], stripe_off_arg);
357 /* get the stripe count */
358 if (stripe_count_arg != NULL) {
359 st_count = strtoul(stripe_count_arg, &end, 0);
361 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
362 argv[0], stripe_count_arg);
368 result = llapi_file_create_pool(fname, st_size, st_offset,
369 st_count, 0, pool_name_arg);
371 fprintf(stderr,"error: %s: create stripe file '%s' "
372 "failed\n", argv[0], fname);
375 fname = argv[++optind];
376 } while (fname != NULL);
381 static int lfs_poollist(int argc, char **argv)
386 return llapi_poollist(argv[1]);
389 static int set_time(time_t *time, time_t *set, char *str)
396 else if (str[0] == '-')
402 t = strtol(str, NULL, 0);
403 if (*time < t * 24 * 60 * 60) {
406 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
410 *set = *time - t * 24 * 60 * 60;
417 static int name2id(unsigned int *id, char *name, int type)
420 struct passwd *entry;
422 if (!(entry = getpwnam(name))) {
432 if (!(entry = getgrnam(name))) {
444 static int id2name(char **name, unsigned int id, int type)
447 struct passwd *entry;
449 if (!(entry = getpwuid(id))) {
455 *name = entry->pw_name;
459 if (!(entry = getgrgid(id))) {
465 *name = entry->gr_name;
471 #define FIND_POOL_OPT 3
472 static int lfs_find(int argc, char **argv)
477 struct find_param param = { .maxdepth = -1 };
479 struct option long_opts[] = {
480 /* New find options. */
481 {"atime", required_argument, 0, 'A'},
482 {"ctime", required_argument, 0, 'C'},
483 {"mtime", required_argument, 0, 'M'},
484 {"maxdepth", required_argument, 0, 'D'},
485 {"gid", required_argument, 0, 'g'},
486 {"group", required_argument, 0, 'G'},
487 {"uid", required_argument, 0, 'u'},
488 {"user", required_argument, 0, 'U'},
489 {"name", required_argument, 0, 'n'},
490 /* no short option for pool, p/P already used */
491 {"pool", required_argument, 0, FIND_POOL_OPT},
492 /* --obd is considered as a new option. */
493 {"obd", required_argument, 0, 'O'},
494 {"ost", required_argument, 0, 'O'},
495 {"print", no_argument, 0, 'P'},
496 {"print0", no_argument, 0, 'p'},
497 /* Old find options. */
498 {"quiet", no_argument, 0, 'q'},
499 {"recursive", no_argument, 0, 'r'},
500 {"size", required_argument, 0, 's'},
501 {"type", required_argument, 0, 't'},
502 {"verbose", no_argument, 0, 'v'},
516 /* when getopt_long_only() hits '!' it returns 1 and puts "!" in optarg */
517 while ((c = getopt_long_only(argc, argv, "-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
518 long_opts, NULL)) >= 0) {
523 /* '!' is part of option */
524 /* when getopt_long_only() finds a string which is not
525 * an option nor a known option argument it returns 1
526 * in that case if we already have found pathstart and pathend
527 * (i.e. we have the list of pathnames),
528 * the only supported value is "!"
530 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
531 if (!isoption && pathend != -1) {
532 fprintf(stderr, "err: %s: filename|dirname must either "
533 "precede options or follow options\n",
537 if (!isoption && pathstart == -1)
538 pathstart = optind - 1;
539 if (isoption && pathstart != -1 && pathend == -1) {
540 pathend = optind - 2;
541 if ((c == 1 && strcmp(optarg, "!") == 0) ||
542 c == 'P' || c == 'p' || c == 'O' ||
543 c == 'q' || c == 'r' || c == 'v')
544 pathend = optind - 1;
551 /* unknown; opt is "!" or path component,
552 * checking done above.
554 if (strcmp(optarg, "!") == 0)
558 xtime = ¶m.atime;
559 xsign = ¶m.asign;
562 xtime = ¶m.ctime;
563 xsign = ¶m.csign;
567 xtime = ¶m.mtime;
568 xsign = ¶m.msign;
572 if (optarg[0] == '-')
574 else if (optarg[0] == '+')
583 ret = set_time(&t, xtime, optarg);
591 param.maxdepth = strtol(optarg, 0, 0);
595 param.gid = strtol(optarg, &endptr, 10);
596 if (optarg == endptr) {
597 fprintf(stderr, "Bad gid: %s\n", optarg);
600 param.exclude_gid = !!neg_opt;
605 param.gid = strtol(optarg, &endptr, 10);
606 if (optarg == endptr) {
607 ret = name2id(¶m.gid, optarg, GROUP);
609 fprintf(stderr, "Group/GID: %s cannot "
610 "be found.\n", optarg);
614 param.exclude_gid = !!neg_opt;
619 param.uid = strtol(optarg, &endptr, 10);
620 if (optarg == endptr) {
621 fprintf(stderr, "Bad uid: %s\n", optarg);
624 param.exclude_uid = !!neg_opt;
629 param.uid = strtol(optarg, &endptr, 10);
630 if (optarg == endptr) {
631 ret = name2id(¶m.uid, optarg, USER);
633 fprintf(stderr, "User/UID: %s cannot "
634 "be found.\n", optarg);
638 param.exclude_uid = !!neg_opt;
643 if (strlen(optarg) > LOV_MAXPOOLNAME) {
645 "Pool name %s is too long"
646 " (max is %d)\n", optarg,
650 /* we do check for empty pool because empty pool
651 * is used to find V1 lov attributes */
652 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
653 param.poolname[LOV_MAXPOOLNAME] = '\0';
654 param.exclude_pool = !!neg_opt;
655 param.check_pool = 1;
659 param.pattern = (char *)optarg;
660 param.exclude_pattern = !!neg_opt;
663 char *buf, *token, *next, *p;
666 len = strlen((char *)optarg);
670 strcpy(buf, (char *)optarg);
672 if (param.num_alloc_obds == 0) {
673 param.obduuid = malloc(FIND_MAX_OSTS *
674 sizeof(struct obd_uuid));
675 if (param.obduuid == NULL)
677 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
680 for (token = buf; token && *token; token = next) {
681 p = strchr(token, ',');
687 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
711 param.exclude_type = !!neg_opt;
713 case 'b': param.type = S_IFBLK; break;
714 case 'c': param.type = S_IFCHR; break;
715 case 'd': param.type = S_IFDIR; break;
716 case 'f': param.type = S_IFREG; break;
717 case 'l': param.type = S_IFLNK; break;
718 case 'p': param.type = S_IFIFO; break;
719 case 's': param.type = S_IFSOCK; break;
720 #ifdef S_IFDOOR /* Solaris only */
721 case 'D': param.type = S_IFDOOR; break;
723 default: fprintf(stderr, "error: %s: bad type '%s'\n",
730 if (optarg[0] == '-')
732 else if (optarg[0] == '+')
741 if (optarg[0] == '+')
742 param.size_sign = -1;
743 else if (optarg[0] == '-')
744 param.size_sign = +1;
748 ret = parse_size(optarg, ¶m.size,
749 ¶m.size_units, 0);
751 fprintf(stderr,"error: bad size '%s'\n",
764 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
765 argv[0], argv[optind - 1]);
770 if (pathstart == -1) {
771 fprintf(stderr, "error: %s: no filename|pathname\n",
774 } else if (pathend == -1) {
782 static int deprecated_warning;
783 if (!deprecated_warning) {
784 fprintf(stderr, "lfs find: -q, -r, -v options "
785 "deprecated. Use 'lfs getstripe' instead.\n");
786 deprecated_warning = 1;
788 if (!param.recursive && param.maxdepth == -1)
794 ret = llapi_find(argv[pathstart], ¶m);
796 ret = llapi_getstripe(argv[pathstart], ¶m);
797 } while (++pathstart < pathend && !ret);
800 fprintf(stderr, "error: %s failed for %s.\n",
801 argv[0], argv[optind - 1]);
803 if (param.obduuid && param.num_alloc_obds)
809 static int lfs_getstripe(int argc, char **argv)
811 struct option long_opts[] = {
813 {"quiet", 0, 0, 'q'},
814 {"recursive", 0, 0, 'r'},
815 {"verbose", 0, 0, 'v'},
818 char short_opts[] = "hO:qrv";
820 struct find_param param = { 0 };
823 while ((c = getopt_long(argc, argv, short_opts,
824 long_opts, NULL)) != -1) {
829 "error: %s: only one obduuid allowed",
833 param.obduuid = (struct obd_uuid *)optarg;
849 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
850 argv[0], argv[optind - 1]);
858 param.maxdepth = param.recursive ? -1 : 1;
861 rc = llapi_getstripe(argv[optind], ¶m);
862 } while (++optind < argc && !rc);
865 fprintf(stderr, "error: %s failed for %s.\n",
866 argv[0], argv[optind - 1]);
870 static int lfs_osts(int argc, char **argv)
873 struct mntent *mnt = NULL;
874 struct find_param param;
880 fp = setmntent(MOUNTED, "r");
883 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
887 while (feof(fp) == 0 && ferror(fp) ==0) {
888 memset(¶m, 0, sizeof(param));
889 if (llapi_is_lustre_mnt(mnt)) {
890 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
893 "error: %s: failed on %s\n",
894 argv[0], mnt->mnt_dir);
904 static int lfs_path2fid(int argc, char **argv)
907 unsigned long long seq;
908 unsigned long oid, ver;
915 rc = llapi_path2fid(path, &seq, &oid, &ver);
917 fprintf(stderr, "error: can't get fid for %s\n", path);
921 printf("%llu:%lu", seq, oid);
930 #define COOK(value) \
933 while (value > 1024) { \
947 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
949 char rpath[PATH_MAX] = {'\0'};
951 int rc, len, out_len = 0;
953 if (!realpath(path, rpath)) {
955 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
956 path, strerror(-rc));
962 while (feof(fp) == 0 && ferror(fp) == 0) {
963 if (llapi_is_lustre_mnt(mnt)) {
964 len = strlen(mnt->mnt_dir);
966 !strncmp(rpath, mnt->mnt_dir, len)) {
968 memset(mntdir, 0, dir_len);
969 strncpy(mntdir, mnt->mnt_dir, dir_len);
978 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
982 static int showdf(char *mntdir, struct obd_statfs *stat,
983 char *uuid, int ishow, int cooked,
984 char *type, int index, int rc)
986 long long avail, used, total;
988 char *suffix = "KMGTPEZY";
989 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
990 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
998 avail = stat->os_ffree;
999 used = stat->os_files - stat->os_ffree;
1000 total = stat->os_files;
1002 int shift = cooked ? 0 : 10;
1004 avail = (stat->os_bavail * stat->os_bsize) >> shift;
1005 used = ((stat->os_blocks - stat->os_bfree) *
1006 stat->os_bsize) >> shift;
1007 total = (stat->os_blocks * stat->os_bsize) >> shift;
1011 ratio = (double)used / (double)total;
1017 cook_val = (double)total;
1020 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
1022 sprintf(tbuf, CDF, total);
1024 cook_val = (double)used;
1027 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
1029 sprintf(ubuf, CDF, used);
1031 cook_val = (double)avail;
1034 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
1036 sprintf(abuf, CDF, avail);
1038 sprintf(tbuf, CDF, total);
1039 sprintf(ubuf, CDF, used);
1040 sprintf(abuf, CDF, avail);
1043 sprintf(rbuf, RDF, (int)(ratio * 100));
1044 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1045 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1047 printf("[%s:%d]\n", type, index);
1053 printf(UUF": inactive device\n", uuid);
1056 printf(UUF": %s\n", uuid, strerror(-rc));
1063 static int mntdf(char *mntdir, int ishow, int cooked)
1065 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1066 struct obd_uuid uuid_buf;
1071 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1072 "UUID", "Inodes", "IUsed", "IFree",
1073 "IUse%", "Mounted on");
1075 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1076 "UUID", cooked ? "bytes" : "1K-blocks",
1077 "Used", "Available", "Use%", "Mounted on");
1079 for (index = 0; ; index++) {
1080 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1081 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1082 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1083 &stat_buf, &uuid_buf);
1090 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1091 rc == -ENODATA || rc == 0) {
1092 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1093 ishow, cooked, "MDT", index, rc);
1096 "error: llapi_obd_statfs(%s): %s (%d)\n",
1097 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1101 sum.os_ffree += stat_buf.os_ffree;
1102 sum.os_files += stat_buf.os_files;
1106 for (index = 0; ; index++) {
1107 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1108 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1109 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1110 &stat_buf, &uuid_buf);
1117 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1118 rc == -ENODATA || rc == 0) {
1119 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1120 ishow, cooked, "OST", index, rc);
1123 "error: llapi_obd_statfs failed: %s (%d)\n",
1128 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1129 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1130 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1135 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1140 static int lfs_df(int argc, char **argv)
1144 struct mntent *mnt = NULL;
1145 char *mntdir = NULL;
1146 int ishow = 0, cooked = 0;
1150 while ((c = getopt(argc, argv, "ih")) != -1) {
1163 path = argv[optind];
1165 fp = setmntent(MOUNTED, "r");
1168 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1169 argv[0], MOUNTED, strerror(errno));
1173 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1174 fprintf(stderr, "error: cannot allocate %d bytes\n",
1178 memset(mntdir, 0, PATH_MAX);
1181 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1188 rc = mntdf(mntdir, ishow, cooked);
1192 mnt = getmntent(fp);
1193 while (feof(fp) == 0 && ferror(fp) == 0) {
1194 if (llapi_is_lustre_mnt(mnt)) {
1195 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1200 mnt = getmntent(fp);
1209 static int lfs_check(int argc, char **argv)
1213 struct mntent *mnt = NULL;
1222 obd_types[0] = obd_type1;
1223 obd_types[1] = obd_type2;
1225 if (strcmp(argv[1], "osts") == 0) {
1226 strcpy(obd_types[0], "osc");
1227 } else if (strcmp(argv[1], "mds") == 0) {
1228 strcpy(obd_types[0], "mdc");
1229 } else if (strcmp(argv[1], "servers") == 0) {
1231 strcpy(obd_types[0], "osc");
1232 strcpy(obd_types[1], "mdc");
1234 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1239 fp = setmntent(MOUNTED, "r");
1241 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1244 mnt = getmntent(fp);
1245 while (feof(fp) == 0 && ferror(fp) ==0) {
1246 if (llapi_is_lustre_mnt(mnt))
1248 mnt = getmntent(fp);
1254 fprintf(stderr, "No suitable Lustre mount found\n");
1258 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1261 fprintf(stderr, "error: %s: %s status failed\n",
1268 static int lfs_catinfo(int argc, char **argv)
1271 struct mntent *mnt = NULL;
1274 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1277 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1280 fp = setmntent(MOUNTED, "r");
1282 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1285 mnt = getmntent(fp);
1286 while (feof(fp) == 0 && ferror(fp) == 0) {
1287 if (llapi_is_lustre_mnt(mnt))
1289 mnt = getmntent(fp);
1296 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1298 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1300 fprintf(stderr, "no lustre_lite mounted.\n");
1307 int lfs_join(int argc, char **argv)
1309 char *name_head, *name_tail;
1315 name_head = argv[1];
1316 fd = open(name_head, O_WRONLY);
1318 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1322 size = lseek(fd, 0, SEEK_END);
1323 if (size % JOIN_FILE_ALIGN) {
1324 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1325 name_head, (long long)size, JOIN_FILE_ALIGN);
1329 name_tail = argv[2];
1330 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1334 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1340 #ifdef HAVE_SYS_QUOTA_H
1341 static int lfs_quotachown(int argc, char **argv)
1348 while ((c = getopt(argc, argv, "i")) != -1) {
1354 fprintf(stderr, "error: %s: option '-%c' "
1355 "unrecognized\n", argv[0], c);
1361 rc = llapi_quotachown(argv[optind], flag);
1363 fprintf(stderr,"error: change file owner/group failed.\n");
1367 static int lfs_quotacheck(int argc, char **argv)
1369 int c, check_type = 0;
1371 struct if_quotacheck qchk;
1372 struct if_quotactl qctl;
1373 char *obd_type = (char *)qchk.obd_type;
1376 memset(&qchk, 0, sizeof(qchk));
1379 while ((c = getopt(argc, argv, "ug")) != -1) {
1388 fprintf(stderr, "error: %s: option '-%c' "
1389 "unrecognized\n", argv[0], c);
1396 else /* do quotacheck for both user & group quota by default */
1404 memset(&qctl, 0, sizeof(qctl));
1405 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1406 qctl.qc_type = check_type;
1407 rc = llapi_quotactl(mnt, &qctl);
1409 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1413 rc = llapi_quotacheck(mnt, check_type);
1415 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1419 rc = llapi_poll_quotacheck(mnt, &qchk);
1422 fprintf(stderr, "%s %s ", obd_type,
1423 obd_uuid2str(&qchk.obd_uuid));
1424 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1428 memset(&qctl, 0, sizeof(qctl));
1429 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1430 qctl.qc_type = check_type;
1431 rc = llapi_quotactl(mnt, &qctl);
1434 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1435 obd_uuid2str(&qctl.obd_uuid));
1436 fprintf(stderr, "%s turn on quota failed: %s\n",
1437 argv[0], strerror(errno));
1444 static int lfs_quotaon(int argc, char **argv)
1448 struct if_quotactl qctl;
1449 char *obd_type = (char *)qctl.obd_type;
1452 memset(&qctl, 0, sizeof(qctl));
1453 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1456 while ((c = getopt(argc, argv, "ugf")) != -1) {
1459 qctl.qc_type |= 0x01;
1462 qctl.qc_type |= 0x02;
1465 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1468 fprintf(stderr, "error: %s: option '-%c' "
1469 "unrecognized\n", argv[0], c);
1476 else /* by default, enable quota for both user & group */
1477 qctl.qc_type = 0x02;
1484 rc = llapi_quotactl(mnt, &qctl);
1487 fprintf(stderr, "%s %s ", obd_type,
1488 obd_uuid2str(&qctl.obd_uuid));
1489 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1496 static int lfs_quotaoff(int argc, char **argv)
1500 struct if_quotactl qctl;
1501 char *obd_type = (char *)qctl.obd_type;
1504 memset(&qctl, 0, sizeof(qctl));
1505 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1508 while ((c = getopt(argc, argv, "ug")) != -1) {
1511 qctl.qc_type |= 0x01;
1514 qctl.qc_type |= 0x02;
1517 fprintf(stderr, "error: %s: option '-%c' "
1518 "unrecognized\n", argv[0], c);
1525 else /* by default, disable quota for both user & group */
1526 qctl.qc_type = 0x02;
1533 rc = llapi_quotactl(mnt, &qctl);
1534 if (rc == -1 && errno == ESRCH) {
1535 fprintf(stderr, "\n%s quotas are not enabled.\n",
1536 qctl.qc_type == 0x00 ? "user" : "group");
1541 fprintf(stderr, "%s %s ", obd_type,
1542 obd_uuid2str(&qctl.obd_uuid));
1543 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1550 static int lfs_quotainv(int argc, char **argv)
1554 struct if_quotactl qctl;
1557 memset(&qctl, 0, sizeof(qctl));
1558 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1561 while ((c = getopt(argc, argv, "ugf")) != -1) {
1564 qctl.qc_type |= 0x01;
1567 qctl.qc_type |= 0x02;
1570 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1573 fprintf(stderr, "error: %s: option '-%c' "
1574 "unrecognized\n", argv[0], c);
1581 else /* by default, invalidate quota for both user & group */
1582 qctl.qc_type = 0x02;
1589 rc = llapi_quotactl(mnt, &qctl);
1591 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1598 #define ARG2INT(nr, str, msg) \
1601 nr = strtol(str, &endp, 0); \
1603 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1608 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1610 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1611 * returns the value or ULONG_MAX on integer overflow or incorrect format
1613 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1614 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1615 * 3. empty integer value is interpreted as 0
1618 static unsigned long str2sec(const char* timestr) {
1619 const char spec[] = "smhdw";
1620 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1621 unsigned long val = 0;
1624 if (strpbrk(timestr, spec) == NULL) {
1625 /* no specifiers inside the time string,
1626 should treat it as an integer value */
1627 val = strtoul(timestr, &tail, 10);
1628 return *tail ? ULONG_MAX : val;
1631 /* format string is XXwXXdXXhXXmXXs */
1637 v = strtoul(timestr, &tail, 10);
1638 if (v == ULONG_MAX || *tail == '\0')
1639 /* value too large (ULONG_MAX or more)
1640 or missing specifier */
1643 ptr = strchr(spec, *tail);
1645 /* unknown specifier */
1650 /* check if product will overflow the type */
1651 if (!(v < ULONG_MAX / mult[ind]))
1654 ADD_OVERFLOW(val, mult[ind] * v);
1655 if (val == ULONG_MAX)
1667 #define ARG2ULL(nr, str, defscale) \
1669 unsigned long long limit, units = 0; \
1672 rc = parse_size(str, &limit, &units, 1); \
1674 fprintf(stderr, "error: bad limit value %s\n", str); \
1677 nr = ((units == 0) ? (defscale) : 1) * limit; \
1680 static inline int has_times_option(int argc, char **argv)
1684 for (i = 1; i < argc; i++)
1685 if (!strcmp(argv[i], "-t"))
1691 int lfs_setquota_times(int argc, char **argv)
1694 struct if_quotactl qctl;
1695 char *mnt, *obd_type = (char *)qctl.obd_type;
1696 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1697 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1698 struct option long_opts[] = {
1699 {"user", no_argument, 0, 'u'},
1700 {"group", no_argument, 0, 'g'},
1701 {"block-grace", required_argument, 0, 'b'},
1702 {"inode-grace", required_argument, 0, 'i'},
1703 {"times", no_argument, 0, 't'},
1707 memset(&qctl, 0, sizeof(qctl));
1708 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1709 qctl.qc_type = UGQUOTA;
1712 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1716 if (qctl.qc_type != UGQUOTA) {
1717 fprintf(stderr, "error: -u and -g can't be used "
1718 "more than once\n");
1721 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1724 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1725 fprintf(stderr, "error: bad block-grace: %s\n",
1729 dqb->dqb_valid |= QIF_BTIME;
1732 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1733 fprintf(stderr, "error: bad inode-grace: %s\n",
1737 dqb->dqb_valid |= QIF_ITIME;
1739 case 't': /* Yes, of course! */
1741 default: /* getopt prints error message for us when opterr != 0 */
1746 if (qctl.qc_type == UGQUOTA) {
1747 fprintf(stderr, "error: neither -u nor -g specified\n");
1751 if (optind != argc - 1) {
1752 fprintf(stderr, "error: unexpected parameters encountered\n");
1757 rc = llapi_quotactl(mnt, &qctl);
1760 fprintf(stderr, "%s %s ", obd_type,
1761 obd_uuid2str(&qctl.obd_uuid));
1762 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1769 #define BSLIMIT (1 << 0)
1770 #define BHLIMIT (1 << 1)
1771 #define ISLIMIT (1 << 2)
1772 #define IHLIMIT (1 << 3)
1774 int lfs_setquota(int argc, char **argv)
1777 struct if_quotactl qctl;
1778 char *mnt, *obd_type = (char *)qctl.obd_type;
1779 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1780 struct option long_opts[] = {
1781 {"user", required_argument, 0, 'u'},
1782 {"group", required_argument, 0, 'g'},
1783 {"block-softlimit", required_argument, 0, 'b'},
1784 {"block-hardlimit", required_argument, 0, 'B'},
1785 {"inode-softlimit", required_argument, 0, 'i'},
1786 {"inode-hardlimit", required_argument, 0, 'I'},
1789 unsigned limit_mask = 0;
1791 if (has_times_option(argc, argv))
1792 return lfs_setquota_times(argc, argv);
1794 memset(&qctl, 0, sizeof(qctl));
1795 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1796 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1797 * so it can be used as a marker that qc_type
1798 * isn't reinitialized from command line */
1801 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1805 if (qctl.qc_type != UGQUOTA) {
1806 fprintf(stderr, "error: -u and -g can't be used"
1807 " more than once\n");
1810 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1811 rc = name2id(&qctl.qc_id, optarg,
1812 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1814 fprintf(stderr, "error: unknown id %s\n",
1820 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1821 dqb->dqb_bsoftlimit >>= 10;
1822 limit_mask |= BSLIMIT;
1825 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1826 dqb->dqb_bhardlimit >>= 10;
1827 limit_mask |= BHLIMIT;
1830 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1831 limit_mask |= ISLIMIT;
1834 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1835 limit_mask |= IHLIMIT;
1837 default: /* getopt prints error message for us when opterr != 0 */
1842 if (qctl.qc_type == UGQUOTA) {
1843 fprintf(stderr, "error: neither -u nor -g are specified\n");
1847 if (optind != argc - 1) {
1848 fprintf(stderr, "error: unexpected parameters encountered\n");
1854 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1855 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1856 /* sigh, we can't just set blimits/ilimits */
1857 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1858 .qc_type = qctl.qc_type,
1859 .qc_id = qctl.qc_id};
1861 rc = llapi_quotactl(mnt, &tmp_qctl);
1863 fprintf(stderr, "error: getquota failed\n");
1867 if (!(limit_mask & BHLIMIT))
1868 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1869 if (!(limit_mask & BSLIMIT))
1870 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1871 if (!(limit_mask & IHLIMIT))
1872 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1873 if (!(limit_mask & ISLIMIT))
1874 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1877 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1878 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1880 rc = llapi_quotactl(mnt, &qctl);
1883 fprintf(stderr, "%s %s ", obd_type,
1884 obd_uuid2str(&qctl.obd_uuid));
1885 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1892 static inline char *type2name(int check_type)
1894 if (check_type == USRQUOTA)
1896 else if (check_type == GRPQUOTA)
1903 /* Converts seconds value into format string
1904 * result is returned in buf
1906 * 1. result is in descenting order: 1w2d3h4m5s
1907 * 2. zero fields are not filled (except for p. 3): 5d1s
1908 * 3. zero seconds value is presented as "0s"
1910 static void sec2str(time_t seconds, char *buf)
1912 const char spec[] = "smhdw";
1913 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1918 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1919 c = seconds / mult[i];
1921 if (c > 0 || (i == 0 && buf == tail))
1922 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1929 static void diff2str(time_t seconds, char *buf, time_t now)
1935 if (seconds <= now) {
1936 strcpy(buf, "none");
1939 sec2str(seconds - now, buf);
1942 static void print_quota_title(char *name, struct if_quotactl *qctl)
1944 printf("Disk quotas for %s %s (%cid %u):\n",
1945 type2name(qctl->qc_type), name,
1946 *type2name(qctl->qc_type), qctl->qc_id);
1947 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1949 "kbytes", "quota", "limit", "grace",
1950 "files", "quota", "limit", "grace");
1953 static void print_quota(char *mnt, struct if_quotactl *qctl)
1959 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1960 int bover = 0, iover = 0;
1961 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1963 if (dqb->dqb_bhardlimit &&
1964 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1966 } else if (dqb->dqb_bsoftlimit &&
1967 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1968 if (dqb->dqb_btime > now) {
1975 if (dqb->dqb_ihardlimit &&
1976 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1978 } else if (dqb->dqb_isoftlimit &&
1979 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1980 if (dqb->dqb_btime > now) {
1987 #if 0 /* XXX: always print quotas even when no usages */
1988 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1994 if (strlen(mnt) > 15)
1995 printf("%s\n%15s", mnt, "");
1997 printf("%15s", mnt);
2000 diff2str(dqb->dqb_btime, timebuf, now);
2001 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2002 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
2003 if (qctl->qc_valid == QC_GENERAL)
2004 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2005 ? LPU64 : "["LPU64"]",
2006 dqb->dqb_bsoftlimit);
2008 sprintf(numbuf[1], "%s", "");
2009 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2010 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2011 printf(" %7s%c %6s %7s %7s",
2012 numbuf[0], bover ? '*' : ' ', numbuf[1],
2013 numbuf[2], bover > 1 ? timebuf : "");
2016 diff2str(dqb->dqb_itime, timebuf, now);
2018 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2019 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2020 if (qctl->qc_valid == QC_GENERAL)
2021 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2022 ? LPU64 : "["LPU64"]",
2023 dqb->dqb_isoftlimit);
2025 sprintf(numbuf[1], "%s", "");
2026 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2027 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2028 if (qctl->qc_valid != QC_OSTIDX)
2029 printf(" %7s%c %6s %7s %7s",
2030 numbuf[0], iover ? '*' : ' ', numbuf[1],
2031 numbuf[2], iover > 1 ? timebuf : "");
2034 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2035 qctl->qc_cmd == Q_GETOINFO) {
2039 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2040 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2041 printf("Block grace time: %s; Inode grace time: %s\n",
2042 bgtimebuf, igtimebuf);
2046 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2048 int rc = 0, rc1 = 0, count = 0;
2049 __u32 valid = qctl->qc_valid;
2051 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2053 fprintf(stderr, "can not get %s count: %s\n",
2054 is_mdt ? "mdt": "ost", strerror(errno));
2058 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2059 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2060 rc = llapi_quotactl(mnt, qctl);
2062 /* It is remote client case. */
2063 if (errno == EOPNOTSUPP) {
2070 fprintf(stderr, "quotactl %s%d failed.\n",
2071 is_mdt ? "mdt": "ost", qctl->qc_idx);
2075 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl);
2079 qctl->qc_valid = valid;
2083 static int lfs_quota(int argc, char **argv)
2086 char *mnt, *name = NULL;
2087 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2088 .qc_type = UGQUOTA };
2089 char *obd_type = (char *)qctl.obd_type;
2090 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2091 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2092 __u32 valid = QC_GENERAL, idx = 0;
2095 while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2098 if (qctl.qc_type != UGQUOTA) {
2099 fprintf(stderr, "error: use either -u or -g\n");
2102 qctl.qc_type = USRQUOTA;
2105 if (qctl.qc_type != UGQUOTA) {
2106 fprintf(stderr, "error: use either -u or -g\n");
2109 qctl.qc_type = GRPQUOTA;
2112 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2115 valid = qctl.qc_valid = QC_UUID;
2116 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2119 valid = qctl.qc_valid = QC_MDTIDX;
2120 idx = qctl.qc_idx = atoi(optarg);
2123 valid = qctl.qc_valid = QC_OSTIDX;
2124 idx = qctl.qc_idx = atoi(optarg);
2130 fprintf(stderr, "error: %s: option '-%c' "
2131 "unrecognized\n", argv[0], c);
2136 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2137 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2138 optind == argc - 1) {
2140 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2141 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2142 qctl.qc_valid = valid;
2145 qctl.qc_type = USRQUOTA;
2146 qctl.qc_id = geteuid();
2148 qctl.qc_type = GRPQUOTA;
2149 qctl.qc_id = getegid();
2151 rc = id2name(&name, qctl.qc_id,
2152 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2155 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2156 if (optind + 2 != argc) {
2157 fprintf(stderr, "error: missing quota argument(s)\n");
2161 name = argv[optind++];
2162 rc = name2id(&qctl.qc_id, name,
2163 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2165 fprintf(stderr,"error: can't find id for name %s: %s\n",
2166 name, strerror(errno));
2169 } else if (optind + 1 != argc) {
2170 fprintf(stderr, "error: missing quota info argument(s)\n");
2174 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2175 print_quota_title(name, &qctl);
2179 rc1 = llapi_quotactl(mnt, &qctl);
2180 if (rc1 == -1 && errno == ESRCH) {
2181 fprintf(stderr, "\n%s quotas are not enabled.\n",
2182 qctl.qc_type == USRQUOTA ? "user" : "group");
2185 if (rc1 && *obd_type)
2186 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2188 if (qctl.qc_valid != QC_GENERAL)
2189 mnt = obd_uuid2str(&qctl.obd_uuid);
2191 print_quota(mnt, &qctl);
2193 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2194 rc2 = print_obd_quota(mnt, &qctl, 1);
2195 rc3 = print_obd_quota(mnt, &qctl, 0);
2198 if (rc1 || rc2 || rc3)
2199 printf("Some errors happened when getting quota info. "
2200 "Some devices may be not working or deactivated. "
2201 "The data in \"[]\" is inaccurate.\n");
2209 #endif /* HAVE_SYS_QUOTA_H! */
2211 static int flushctx_ioctl(char *mp)
2215 fd = open(mp, O_RDONLY);
2217 fprintf(stderr, "flushctx: error open %s: %s\n",
2218 mp, strerror(errno));
2222 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2224 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2225 mp, strerror(errno));
2231 static int lfs_flushctx(int argc, char **argv)
2233 int kdestroy = 0, c;
2235 char procline[PATH_MAX], *line;
2239 while ((c = getopt(argc, argv, "k")) != -1) {
2245 fprintf(stderr, "error: %s: option '-%c' "
2246 "unrecognized\n", argv[0], c);
2252 system("kdestroy > /dev/null");
2254 if (optind >= argc) {
2255 /* flush for all mounted lustre fs. */
2256 proc = fopen("/proc/mounts", "r");
2258 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2263 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2268 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2269 fprintf(stderr, "%s: unexpected format in "
2275 if (strcmp(fs, "lustre") != 0)
2277 /* we use '@' to determine it's a client. are there
2278 * any other better way?
2280 if (strchr(dev, '@') == NULL)
2283 if (flushctx_ioctl(mp))
2287 /* flush fs as specified */
2288 while (optind < argc) {
2289 if (flushctx_ioctl(argv[optind++]))
2297 static int lfs_lsetfacl(int argc, char **argv)
2300 return(llapi_lsetfacl(argc, argv));
2303 static int lfs_lgetfacl(int argc, char **argv)
2306 return(llapi_lgetfacl(argc, argv));
2309 static int lfs_rsetfacl(int argc, char **argv)
2312 return(llapi_rsetfacl(argc, argv));
2315 static int lfs_rgetfacl(int argc, char **argv)
2318 return(llapi_rgetfacl(argc, argv));
2321 static int lfs_cp(int argc, char **argv)
2323 return(llapi_cp(argc, argv));
2326 static int lfs_ls(int argc, char **argv)
2328 return(llapi_ls(argc, argv));
2331 int main(int argc, char **argv)
2337 ptl_initialize(argc, argv);
2338 if (obd_initialize(argc, argv) < 0)
2340 if (dbg_initialize(argc, argv) < 0)
2343 Parser_init("lfs > ", cmdlist);
2346 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2348 rc = Parser_commands();
2351 obd_finalize(argc, argv);