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_changelog(int argc, char **argv);
109 static int lfs_changelog_clear(int argc, char **argv);
110 static int lfs_fid2path(int argc, char **argv);
111 static int lfs_path2fid(int argc, char **argv);
113 /* all avaialable commands */
114 command_t cmdlist[] = {
115 {"setstripe", lfs_setstripe, 0,
116 "Create a new file with a specific striping pattern or\n"
117 "set the default striping pattern on an existing directory or\n"
118 "delete the default striping pattern from an existing directory\n"
119 "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
120 " [--count|-c stripe_count] [--pool|-p <pool>]\n"
123 " setstripe -d <dir> (to delete default striping)\n"
124 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
125 "\t Can be specified with k, m or g (in KB, MB and GB\n"
127 "\tstart_ost: OST index of first stripe (-1 filesystem default)\n"
128 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
129 "\tpool: Name of OST pool"},
130 {"getstripe", lfs_getstripe, 0,
131 "To list the striping info for a given file or files in a\n"
132 "directory or recursively for all files in a directory tree.\n"
133 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
134 " [--count | -c ] [--size | -s ] [--index | -i ]\n"
135 " [--offset | -o ] [--pool | -p ]\n"
136 " [--recursive | -r] <dir|file> ..."},
137 {"pool_list", lfs_poollist, 0,
138 "List pools or pool OSTs\n"
139 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
140 {"find", lfs_find, 0,
141 "To find files that match given parameters recursively in a directory tree.\n"
142 "usage: find <dir|file> ... \n"
143 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
144 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
145 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
146 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
147 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
148 " [[!] --pool <pool>]\n"
149 "\t !: used before an option indicates 'NOT' the requested attribute\n"
150 "\t -: used before an value indicates 'AT MOST' the requested value\n"
151 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
152 {"check", lfs_check, 0,
153 "Display the status of MDS or OSTs (as specified in the command)\n"
154 "or all the servers (MDS and OSTs).\n"
155 "usage: check <osts|mds|servers>"},
156 {"catinfo", lfs_catinfo, 0,
157 "Show information of specified type logs.\n"
158 "usage: catinfo {keyword} [node name]\n"
159 "\tkeywords are one of followings: config, deletions.\n"
160 "\tnode name must be provided when use keyword config."},
161 {"join", lfs_join, 0,
162 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
163 "usage: join <filename_A> <filename_B>\n"},
164 {"osts", lfs_osts, 0, "osts"},
166 "report filesystem disk space usage or inodes usage"
167 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
168 "Usage: df [-i] [-h] [--pool|-p <fsname>[.<pool>] [path]"},
169 #ifdef HAVE_SYS_QUOTA_H
170 {"quotachown",lfs_quotachown, 0,
171 "Change files' owner or group on the specified filesystem.\n"
172 "usage: quotachown [-i] <filesystem>\n"
173 "\t-i: ignore error if file is not exist\n"},
174 {"quotacheck", lfs_quotacheck, 0,
175 "Scan the specified filesystem for disk usage, and create,\n"
176 "or update quota files.\n"
177 "usage: quotacheck [ -ug ] <filesystem>"},
178 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
179 "usage: quotaon [ -ugf ] <filesystem>"},
180 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
181 "usage: quotaoff [ -ug ] <filesystem>"},
182 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
183 "usage: setquota [ -u | -g ] <name> -b <block-softlimit> -B <block-hardlimit> -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
184 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>\n"
185 " setquota [ -u | --user | -g | --group ] <name>\n"
186 " [--block-softlimit <block-softlimit>]\n"
187 " [--block-hardlimit <block-hardlimit>]\n"
188 " [--inode-softlimit <inode-softlimit>]\n"
189 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
190 " setquota [-t] [ -u | --user | -g | --group ]\n"
191 " [--block-grace <block-grace>]\n"
192 " [--inode-grace <inode-grace>] <filesystem>\n"
193 " -b can be used instead of --block-softlimit/--block-grace\n"
194 " -B can be used instead of --block-hardlimit\n"
195 " -i can be used instead of --inode-softlimit/--inode-grace\n"
196 " -I can be used instead of --inode-hardlimit"},
197 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
198 "usage: quota [-v] [-o obd_uuid|-i mdt_idx|-I ost_idx] [{-u|-g <name>}|-t] <filesystem>"},
199 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
200 "usage: quotainv [-u|-g] <filesystem>"},
202 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
203 "usage: flushctx [-k] [mountpoint...]"},
204 {"lsetfacl", lfs_lsetfacl, 0,
205 "Remote user setfacl for user/group on the same remote client.\n"
206 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
207 {"lgetfacl", lfs_lgetfacl, 0,
208 "Remote user getfacl for user/group on the same remote client.\n"
209 "usage: lgetfacl [-dRLPvh] file ..."},
210 {"rsetfacl", lfs_rsetfacl, 0,
211 "Remote user setfacl for user/group on other clients.\n"
212 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
213 {"rgetfacl", lfs_rgetfacl, 0,
214 "Remote user getfacl for user/group on other clients.\n"
215 "usage: rgetfacl [-dRLPvh] file ..."},
217 "Remote user copy files and directories.\n"
218 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
220 "Remote user list directory contents.\n"
221 "usage: ls [OPTION]... [FILE]..."},
222 {"changelog", lfs_changelog, 0,
223 "Show the metadata changes on an MDT."
224 "\nusage: changelog [--follow] <mdtname> [startrec [endrec]]"
225 "\n(note: --follow is only valid when run on MDT node)"},
226 {"changelog_clear", lfs_changelog_clear, 0,
227 "Indicate that old changelog records up to <endrec> are no longer of "
228 "interest to consumer <id>, allowing the system to free up space.\n"
229 "An <endrec> of 0 means all records.\n"
230 "usage: changelog_clear <mdtname> <id> <endrec>"},
231 {"fid2path", lfs_fid2path, 0,
232 "Resolve the full path to a given FID. For a specific hardlink "
233 "specify link number <linkno>.\n"
234 /* "For a historical name, specify changelog record <recno>.\n" */
235 "usage: fid2path <fsname|rootpath> <fid> [--link <linkno>]"
236 /*[--rec <recno>]*/},
237 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
238 "usage: path2fid <path>"},
239 {"help", Parser_help, 0, "help"},
240 {"exit", Parser_quit, 0, "quit"},
241 {"quit", Parser_quit, 0, "quit"},
245 static int isnumber(const char *str)
249 if (str[0] != '-' && !isdigit(str[0]))
252 for (ptr = str + 1; *ptr != '\0'; ptr++) {
261 static int lfs_setstripe(int argc, char **argv)
265 unsigned long long st_size;
266 int st_offset, st_count;
270 char *stripe_size_arg = NULL;
271 char *stripe_off_arg = NULL;
272 char *stripe_count_arg = NULL;
273 char *pool_name_arg = NULL;
274 unsigned long long size_units;
276 struct option long_opts[] = {
277 {"size", required_argument, 0, 's'},
278 {"count", required_argument, 0, 'c'},
279 {"index", required_argument, 0, 'i'},
280 {"offset", required_argument, 0, 'o'},
281 {"pool", required_argument, 0, 'p'},
282 {"delete", no_argument, 0, 'd'},
290 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
291 if (argc == 5 && argv[1][0] != '-' &&
292 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
293 fprintf(stderr, "warning: deprecated usage of setstripe "
294 "positional parameters. Use -c, -i, -s instead.\n");
295 /* for compatibility with the existing positional parameter
298 stripe_size_arg = argv[2];
299 stripe_off_arg = argv[3];
300 stripe_count_arg = argv[4];
304 #warning "remove obsolete positional parameter code"
308 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
309 long_opts, NULL)) >= 0) {
315 stripe_count_arg = optarg;
318 /* delete the default striping pattern */
323 stripe_off_arg = optarg;
326 stripe_size_arg = optarg;
329 pool_name_arg = optarg;
334 fprintf(stderr, "error: %s: option '%s' "
336 argv[0], argv[optind - 1]);
341 fname = argv[optind];
344 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
345 stripe_count_arg != NULL || pool_name_arg != NULL)) {
346 fprintf(stderr, "error: %s: cannot specify -d with "
347 "-s, -c -o or -p options\n",
353 if (optind == argc) {
354 fprintf(stderr, "error: %s: missing filename|dirname\n",
359 /* get the stripe size */
360 if (stripe_size_arg != NULL) {
361 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
363 fprintf(stderr, "error: %s: bad size '%s'\n",
364 argv[0], stripe_size_arg);
368 /* get the stripe offset */
369 if (stripe_off_arg != NULL) {
370 st_offset = strtoul(stripe_off_arg, &end, 0);
372 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
373 argv[0], stripe_off_arg);
377 /* get the stripe count */
378 if (stripe_count_arg != NULL) {
379 st_count = strtoul(stripe_count_arg, &end, 0);
381 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
382 argv[0], stripe_count_arg);
388 result = llapi_file_create_pool(fname, st_size, st_offset,
389 st_count, 0, pool_name_arg);
391 fprintf(stderr,"error: %s: create stripe file '%s' "
392 "failed\n", argv[0], fname);
395 fname = argv[++optind];
396 } while (fname != NULL);
401 static int lfs_poollist(int argc, char **argv)
406 return llapi_poollist(argv[1]);
409 static int set_time(time_t *time, time_t *set, char *str)
416 else if (str[0] == '-')
422 t = strtol(str, NULL, 0);
423 if (*time < t * 24 * 60 * 60) {
426 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
430 *set = *time - t * 24 * 60 * 60;
437 static int name2id(unsigned int *id, char *name, int type)
440 struct passwd *entry;
442 if (!(entry = getpwnam(name))) {
452 if (!(entry = getgrnam(name))) {
464 static int id2name(char **name, unsigned int id, int type)
467 struct passwd *entry;
469 if (!(entry = getpwuid(id))) {
475 *name = entry->pw_name;
479 if (!(entry = getgrgid(id))) {
485 *name = entry->gr_name;
491 #define FIND_POOL_OPT 3
492 static int lfs_find(int argc, char **argv)
497 struct find_param param = { .maxdepth = -1 };
499 struct option long_opts[] = {
500 /* New find options. */
501 {"atime", required_argument, 0, 'A'},
502 {"ctime", required_argument, 0, 'C'},
503 {"mtime", required_argument, 0, 'M'},
504 {"maxdepth", required_argument, 0, 'D'},
505 {"gid", required_argument, 0, 'g'},
506 {"group", required_argument, 0, 'G'},
507 {"uid", required_argument, 0, 'u'},
508 {"user", required_argument, 0, 'U'},
509 {"name", required_argument, 0, 'n'},
510 /* no short option for pool, p/P already used */
511 {"pool", required_argument, 0, FIND_POOL_OPT},
512 /* --obd is considered as a new option. */
513 {"obd", required_argument, 0, 'O'},
514 {"ost", required_argument, 0, 'O'},
515 {"print", no_argument, 0, 'P'},
516 {"print0", no_argument, 0, 'p'},
517 /* Old find options. */
518 {"quiet", no_argument, 0, 'q'},
519 {"recursive", no_argument, 0, 'r'},
520 {"size", required_argument, 0, 's'},
521 {"type", required_argument, 0, 't'},
522 {"verbose", no_argument, 0, 'v'},
536 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
537 while ((c = getopt_long_only(argc,argv,"-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
538 long_opts, NULL)) >= 0) {
543 /* '!' is part of option */
544 /* when getopt_long_only() finds a string which is not
545 * an option nor a known option argument it returns 1
546 * in that case if we already have found pathstart and pathend
547 * (i.e. we have the list of pathnames),
548 * the only supported value is "!"
550 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
551 if (!isoption && pathend != -1) {
552 fprintf(stderr, "err: %s: filename|dirname must either "
553 "precede options or follow options\n",
557 if (!isoption && pathstart == -1)
558 pathstart = optind - 1;
559 if (isoption && pathstart != -1 && pathend == -1) {
560 pathend = optind - 2;
561 if ((c == 1 && strcmp(optarg, "!") == 0) ||
562 c == 'P' || c == 'p' || c == 'O' ||
563 c == 'q' || c == 'r' || c == 'v')
564 pathend = optind - 1;
571 /* unknown; opt is "!" or path component,
572 * checking done above.
574 if (strcmp(optarg, "!") == 0)
578 xtime = ¶m.atime;
579 xsign = ¶m.asign;
582 xtime = ¶m.ctime;
583 xsign = ¶m.csign;
587 xtime = ¶m.mtime;
588 xsign = ¶m.msign;
592 if (optarg[0] == '-')
594 else if (optarg[0] == '+')
603 ret = set_time(&t, xtime, optarg);
611 param.maxdepth = strtol(optarg, 0, 0);
615 param.gid = strtol(optarg, &endptr, 10);
616 if (optarg == endptr) {
617 fprintf(stderr, "Bad gid: %s\n", optarg);
620 param.exclude_gid = !!neg_opt;
625 param.gid = strtol(optarg, &endptr, 10);
626 if (optarg == endptr) {
627 ret = name2id(¶m.gid, optarg, GROUP);
629 fprintf(stderr, "Group/GID: %s cannot "
630 "be found.\n", optarg);
634 param.exclude_gid = !!neg_opt;
639 param.uid = strtol(optarg, &endptr, 10);
640 if (optarg == endptr) {
641 fprintf(stderr, "Bad uid: %s\n", optarg);
644 param.exclude_uid = !!neg_opt;
649 param.uid = strtol(optarg, &endptr, 10);
650 if (optarg == endptr) {
651 ret = name2id(¶m.uid, optarg, USER);
653 fprintf(stderr, "User/UID: %s cannot "
654 "be found.\n", optarg);
658 param.exclude_uid = !!neg_opt;
663 if (strlen(optarg) > LOV_MAXPOOLNAME) {
665 "Pool name %s is too long"
666 " (max is %d)\n", optarg,
670 /* we do check for empty pool because empty pool
671 * is used to find V1 lov attributes */
672 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
673 param.poolname[LOV_MAXPOOLNAME] = '\0';
674 param.exclude_pool = !!neg_opt;
675 param.check_pool = 1;
679 param.pattern = (char *)optarg;
680 param.exclude_pattern = !!neg_opt;
683 char *buf, *token, *next, *p;
686 len = strlen((char *)optarg);
690 strcpy(buf, (char *)optarg);
692 if (param.num_alloc_obds == 0) {
693 param.obduuid = malloc(FIND_MAX_OSTS *
694 sizeof(struct obd_uuid));
695 if (param.obduuid == NULL)
697 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
700 for (token = buf; token && *token; token = next) {
701 p = strchr(token, ',');
707 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
731 param.exclude_type = !!neg_opt;
733 case 'b': param.type = S_IFBLK; break;
734 case 'c': param.type = S_IFCHR; break;
735 case 'd': param.type = S_IFDIR; break;
736 case 'f': param.type = S_IFREG; break;
737 case 'l': param.type = S_IFLNK; break;
738 case 'p': param.type = S_IFIFO; break;
739 case 's': param.type = S_IFSOCK; break;
740 #ifdef S_IFDOOR /* Solaris only */
741 case 'D': param.type = S_IFDOOR; break;
743 default: fprintf(stderr, "error: %s: bad type '%s'\n",
750 if (optarg[0] == '-')
752 else if (optarg[0] == '+')
761 if (optarg[0] == '+')
762 param.size_sign = -1;
763 else if (optarg[0] == '-')
764 param.size_sign = +1;
768 ret = parse_size(optarg, ¶m.size,
769 ¶m.size_units, 0);
771 fprintf(stderr,"error: bad size '%s'\n",
784 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
785 argv[0], argv[optind - 1]);
790 if (pathstart == -1) {
791 fprintf(stderr, "error: %s: no filename|pathname\n",
794 } else if (pathend == -1) {
802 static int deprecated_warning;
803 if (!deprecated_warning) {
804 fprintf(stderr, "lfs find: -q, -r, -v options "
805 "deprecated. Use 'lfs getstripe' instead.\n");
806 deprecated_warning = 1;
808 if (!param.recursive && param.maxdepth == -1)
814 ret = llapi_find(argv[pathstart], ¶m);
816 ret = llapi_getstripe(argv[pathstart], ¶m);
817 } while (++pathstart < pathend && !ret);
820 fprintf(stderr, "error: %s failed for %s.\n",
821 argv[0], argv[optind - 1]);
823 if (param.obduuid && param.num_alloc_obds)
829 static int lfs_getstripe(int argc, char **argv)
831 struct option long_opts[] = {
833 {"quiet", 0, 0, 'q'},
834 {"recursive", 0, 0, 'r'},
835 {"count", 0, 0, 'c'},
837 {"index", 0, 0, 'i'},
838 {"offset", 0, 0, 'o'},
840 {"verbose", 0, 0, 'v'},
843 char short_opts[] = "hO:qrvcsiop";
845 struct find_param param = { 0 };
848 while ((c = getopt_long(argc, argv, short_opts,
849 long_opts, NULL)) != -1) {
854 "error: %s: only one obduuid allowed",
858 param.obduuid = (struct obd_uuid *)optarg;
867 param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
871 param.verbose |= VERBOSE_COUNT;
874 param.verbose |= VERBOSE_SIZE;
878 param.verbose |= VERBOSE_OFFSET;
881 param.verbose |= VERBOSE_POOL;
886 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
887 argv[0], argv[optind - 1]);
895 param.maxdepth = param.recursive ? -1 : 1;
898 rc = llapi_getstripe(argv[optind], ¶m);
899 } while (++optind < argc && !rc);
902 fprintf(stderr, "error: %s failed for %s.\n",
903 argv[0], argv[optind - 1]);
907 static int lfs_osts(int argc, char **argv)
909 char mntdir[PATH_MAX] = {'\0'};
910 struct find_param param;
916 while (llapi_search_mounts(NULL, index++, mntdir, NULL) == 0) {
917 memset(¶m, 0, sizeof(param));
918 rc = llapi_getstripe(mntdir, ¶m);
920 fprintf(stderr, "error: %s: failed on %s\n",
923 memset(mntdir, 0, PATH_MAX);
929 #define COOK(value) \
932 while (value > 1024) { \
946 static int showdf(char *mntdir, struct obd_statfs *stat,
947 char *uuid, int ishow, int cooked,
948 char *type, int index, int rc)
950 long long avail, used, total;
952 char *suffix = "KMGTPEZY";
953 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
954 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
962 avail = stat->os_ffree;
963 used = stat->os_files - stat->os_ffree;
964 total = stat->os_files;
966 int shift = cooked ? 0 : 10;
968 avail = (stat->os_bavail * stat->os_bsize) >> shift;
969 used = ((stat->os_blocks - stat->os_bfree) *
970 stat->os_bsize) >> shift;
971 total = (stat->os_blocks * stat->os_bsize) >> shift;
975 ratio = (double)used / (double)total;
981 cook_val = (double)total;
984 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
986 sprintf(tbuf, CDF, total);
988 cook_val = (double)used;
991 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
993 sprintf(ubuf, CDF, used);
995 cook_val = (double)avail;
998 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
1000 sprintf(abuf, CDF, avail);
1002 sprintf(tbuf, CDF, total);
1003 sprintf(ubuf, CDF, used);
1004 sprintf(abuf, CDF, avail);
1007 sprintf(rbuf, RDF, (int)(ratio * 100));
1008 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1009 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1011 printf("[%s:%d]\n", type, index);
1017 printf(UUF": inactive device\n", uuid);
1020 printf(UUF": %s\n", uuid, strerror(-rc));
1027 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow, int cooked)
1029 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1030 struct obd_uuid uuid_buf;
1031 char *poolname = NULL;
1036 poolname = strchr(pool, '.');
1037 if (poolname != NULL) {
1038 if (strncmp(fsname, pool, strlen(fsname))) {
1039 fprintf(stderr, "filesystem name incorrect\n");
1048 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1049 "UUID", "Inodes", "IUsed", "IFree",
1050 "IUse%", "Mounted on");
1052 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1053 "UUID", cooked ? "bytes" : "1K-blocks",
1054 "Used", "Available", "Use%", "Mounted on");
1056 for (index = 0; ; index++) {
1057 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1058 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1059 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1060 &stat_buf, &uuid_buf);
1067 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1068 rc == -ENODATA || rc == 0) {
1069 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1070 ishow, cooked, "MDT", index, rc);
1073 "error: llapi_obd_statfs(%s): %s (%d)\n",
1074 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1078 sum.os_ffree += stat_buf.os_ffree;
1079 sum.os_files += stat_buf.os_files;
1083 for (index = 0; ; index++) {
1084 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1085 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1086 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1087 &stat_buf, &uuid_buf);
1094 if (llapi_search_ost(fsname, poolname,
1095 obd_uuid2str(&uuid_buf)) != 1)
1098 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1099 rc == -ENODATA || rc == 0) {
1100 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1101 ishow, cooked, "OST", index, rc);
1104 "error: llapi_obd_statfs failed: %s (%d)\n",
1109 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1110 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1111 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1116 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1121 static int lfs_df(int argc, char **argv)
1124 char *mntdir = NULL;
1125 int ishow = 0, cooked = 0;
1127 char fsname[PATH_MAX], *pool_name = NULL;
1128 struct option long_opts[] = {
1129 {"pool", required_argument, 0, 'p'},
1134 while ((c = getopt_long(argc, argv, "ihp:", long_opts, NULL)) != -1) {
1150 path = argv[optind];
1152 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1153 fprintf(stderr, "error: cannot allocate %d bytes\n",
1157 memset(mntdir, 0, PATH_MAX);
1160 char rpath[PATH_MAX] = {'\0'};
1162 if (!realpath(path, rpath)) {
1164 fprintf(stderr, "error: invalid path '%s': %s\n",
1165 path, strerror(-rc));
1167 rc = llapi_search_mounts(rpath, 0, mntdir, fsname);
1168 if (rc == 0 && mntdir[0] != '\0') {
1169 rc = mntdf(mntdir, fsname, pool_name,
1177 while (llapi_search_mounts(NULL, index++, mntdir, fsname)==0) {
1178 rc = mntdf(mntdir, fsname, pool_name,
1190 static int lfs_check(int argc, char **argv)
1193 char mntdir[PATH_MAX] = {'\0'};
1202 obd_types[0] = obd_type1;
1203 obd_types[1] = obd_type2;
1205 if (strcmp(argv[1], "osts") == 0) {
1206 strcpy(obd_types[0], "osc");
1207 } else if (strcmp(argv[1], "mds") == 0) {
1208 strcpy(obd_types[0], "mdc");
1209 } else if (strcmp(argv[1], "servers") == 0) {
1211 strcpy(obd_types[0], "osc");
1212 strcpy(obd_types[1], "mdc");
1214 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1219 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1220 if (rc < 0 || mntdir[0] == '\0') {
1221 fprintf(stderr, "No suitable Lustre mount found\n");
1225 rc = llapi_target_iterate(num_types, obd_types,
1226 mntdir, llapi_ping_target);
1229 fprintf(stderr, "error: %s: %s status failed\n",
1236 static int lfs_catinfo(int argc, char **argv)
1238 char mntdir[PATH_MAX] = {'\0'};
1241 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1244 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1247 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1248 if (rc == 0 && mntdir[0] != '\0') {
1250 rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1252 rc = llapi_catinfo(mntdir, argv[1], NULL);
1254 fprintf(stderr, "no lustre_lite mounted.\n");
1261 int lfs_join(int argc, char **argv)
1263 char *name_head, *name_tail;
1269 name_head = argv[1];
1270 fd = open(name_head, O_WRONLY);
1272 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1276 size = lseek(fd, 0, SEEK_END);
1277 if (size % JOIN_FILE_ALIGN) {
1278 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1279 name_head, (long long)size, JOIN_FILE_ALIGN);
1283 name_tail = argv[2];
1284 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1288 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1294 #ifdef HAVE_SYS_QUOTA_H
1295 static int lfs_quotachown(int argc, char **argv)
1302 while ((c = getopt(argc, argv, "i")) != -1) {
1308 fprintf(stderr, "error: %s: option '-%c' "
1309 "unrecognized\n", argv[0], c);
1315 rc = llapi_quotachown(argv[optind], flag);
1317 fprintf(stderr,"error: change file owner/group failed.\n");
1321 static int lfs_quotacheck(int argc, char **argv)
1323 int c, check_type = 0;
1325 struct if_quotacheck qchk;
1326 struct if_quotactl qctl;
1327 char *obd_type = (char *)qchk.obd_type;
1330 memset(&qchk, 0, sizeof(qchk));
1333 while ((c = getopt(argc, argv, "ug")) != -1) {
1342 fprintf(stderr, "error: %s: option '-%c' "
1343 "unrecognized\n", argv[0], c);
1350 else /* do quotacheck for both user & group quota by default */
1358 rc = llapi_quotacheck(mnt, check_type);
1360 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1364 rc = llapi_poll_quotacheck(mnt, &qchk);
1367 fprintf(stderr, "%s %s ", obd_type,
1368 obd_uuid2str(&qchk.obd_uuid));
1369 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1373 memset(&qctl, 0, sizeof(qctl));
1374 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1375 qctl.qc_type = check_type;
1376 rc = llapi_quotactl(mnt, &qctl);
1377 if (rc && errno != EALREADY) {
1379 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1380 obd_uuid2str(&qctl.obd_uuid));
1381 fprintf(stderr, "%s turn on quota failed: %s\n",
1382 argv[0], strerror(errno));
1389 static int lfs_quotaon(int argc, char **argv)
1393 struct if_quotactl qctl;
1394 char *obd_type = (char *)qctl.obd_type;
1397 memset(&qctl, 0, sizeof(qctl));
1398 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1401 while ((c = getopt(argc, argv, "ugf")) != -1) {
1404 qctl.qc_type |= 0x01;
1407 qctl.qc_type |= 0x02;
1410 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1413 fprintf(stderr, "error: %s: option '-%c' "
1414 "unrecognized\n", argv[0], c);
1421 else /* by default, enable quota for both user & group */
1422 qctl.qc_type = 0x02;
1429 rc = llapi_quotactl(mnt, &qctl);
1431 if (errno == EALREADY) {
1432 fprintf(stderr, "\n%s quotas are enabled already.\n",
1433 qctl.qc_type == 0x02 ? "user/group" :
1434 (qctl.qc_type == 0x00 ? "user" : "group"));
1436 } else if (errno == ENOENT) {
1437 fprintf(stderr, "error: cannot find quota database, "
1438 "make sure you have run quotacheck\n");
1441 fprintf(stderr, "%s %s ", obd_type,
1442 obd_uuid2str(&qctl.obd_uuid));
1443 fprintf(stderr, "%s failed: %s\n", argv[0],
1451 static int lfs_quotaoff(int argc, char **argv)
1455 struct if_quotactl qctl;
1456 char *obd_type = (char *)qctl.obd_type;
1459 memset(&qctl, 0, sizeof(qctl));
1460 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1463 while ((c = getopt(argc, argv, "ug")) != -1) {
1466 qctl.qc_type |= 0x01;
1469 qctl.qc_type |= 0x02;
1472 fprintf(stderr, "error: %s: option '-%c' "
1473 "unrecognized\n", argv[0], c);
1480 else /* by default, disable quota for both user & group */
1481 qctl.qc_type = 0x02;
1488 rc = llapi_quotactl(mnt, &qctl);
1490 if (errno == EALREADY) {
1491 fprintf(stderr, "\n%s quotas are disabled already.\n",
1492 qctl.qc_type == 0x02 ? "user/group" :
1493 (qctl.qc_type == 0x00 ? "user" : "group"));
1497 fprintf(stderr, "%s %s ", obd_type,
1498 obd_uuid2str(&qctl.obd_uuid));
1499 fprintf(stderr, "quotaoff failed: %s\n",
1507 static int lfs_quotainv(int argc, char **argv)
1511 struct if_quotactl qctl;
1514 memset(&qctl, 0, sizeof(qctl));
1515 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1518 while ((c = getopt(argc, argv, "ugf")) != -1) {
1521 qctl.qc_type |= 0x01;
1524 qctl.qc_type |= 0x02;
1527 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1530 fprintf(stderr, "error: %s: option '-%c' "
1531 "unrecognized\n", argv[0], c);
1538 else /* by default, invalidate quota for both user & group */
1539 qctl.qc_type = 0x02;
1546 rc = llapi_quotactl(mnt, &qctl);
1548 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1555 #define ARG2INT(nr, str, msg) \
1558 nr = strtol(str, &endp, 0); \
1560 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1565 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1567 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1568 * returns the value or ULONG_MAX on integer overflow or incorrect format
1570 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1571 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1572 * 3. empty integer value is interpreted as 0
1575 static unsigned long str2sec(const char* timestr) {
1576 const char spec[] = "smhdw";
1577 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1578 unsigned long val = 0;
1581 if (strpbrk(timestr, spec) == NULL) {
1582 /* no specifiers inside the time string,
1583 should treat it as an integer value */
1584 val = strtoul(timestr, &tail, 10);
1585 return *tail ? ULONG_MAX : val;
1588 /* format string is XXwXXdXXhXXmXXs */
1594 v = strtoul(timestr, &tail, 10);
1595 if (v == ULONG_MAX || *tail == '\0')
1596 /* value too large (ULONG_MAX or more)
1597 or missing specifier */
1600 ptr = strchr(spec, *tail);
1602 /* unknown specifier */
1607 /* check if product will overflow the type */
1608 if (!(v < ULONG_MAX / mult[ind]))
1611 ADD_OVERFLOW(val, mult[ind] * v);
1612 if (val == ULONG_MAX)
1624 #define ARG2ULL(nr, str, defscale) \
1626 unsigned long long limit, units = 0; \
1629 rc = parse_size(str, &limit, &units, 1); \
1631 fprintf(stderr, "error: bad limit value %s\n", str); \
1634 nr = ((units == 0) ? (defscale) : 1) * limit; \
1637 static inline int has_times_option(int argc, char **argv)
1641 for (i = 1; i < argc; i++)
1642 if (!strcmp(argv[i], "-t"))
1648 int lfs_setquota_times(int argc, char **argv)
1651 struct if_quotactl qctl;
1652 char *mnt, *obd_type = (char *)qctl.obd_type;
1653 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1654 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1655 struct option long_opts[] = {
1656 {"user", no_argument, 0, 'u'},
1657 {"group", no_argument, 0, 'g'},
1658 {"block-grace", required_argument, 0, 'b'},
1659 {"inode-grace", required_argument, 0, 'i'},
1660 {"times", no_argument, 0, 't'},
1664 memset(&qctl, 0, sizeof(qctl));
1665 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1666 qctl.qc_type = UGQUOTA;
1669 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1673 if (qctl.qc_type != UGQUOTA) {
1674 fprintf(stderr, "error: -u and -g can't be used "
1675 "more than once\n");
1678 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1681 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1682 fprintf(stderr, "error: bad block-grace: %s\n",
1686 dqb->dqb_valid |= QIF_BTIME;
1689 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1690 fprintf(stderr, "error: bad inode-grace: %s\n",
1694 dqb->dqb_valid |= QIF_ITIME;
1696 case 't': /* Yes, of course! */
1698 default: /* getopt prints error message for us when opterr != 0 */
1703 if (qctl.qc_type == UGQUOTA) {
1704 fprintf(stderr, "error: neither -u nor -g specified\n");
1708 if (optind != argc - 1) {
1709 fprintf(stderr, "error: unexpected parameters encountered\n");
1714 rc = llapi_quotactl(mnt, &qctl);
1717 fprintf(stderr, "%s %s ", obd_type,
1718 obd_uuid2str(&qctl.obd_uuid));
1719 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1726 #define BSLIMIT (1 << 0)
1727 #define BHLIMIT (1 << 1)
1728 #define ISLIMIT (1 << 2)
1729 #define IHLIMIT (1 << 3)
1731 int lfs_setquota(int argc, char **argv)
1734 struct if_quotactl qctl;
1735 char *mnt, *obd_type = (char *)qctl.obd_type;
1736 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1737 struct option long_opts[] = {
1738 {"user", required_argument, 0, 'u'},
1739 {"group", required_argument, 0, 'g'},
1740 {"block-softlimit", required_argument, 0, 'b'},
1741 {"block-hardlimit", required_argument, 0, 'B'},
1742 {"inode-softlimit", required_argument, 0, 'i'},
1743 {"inode-hardlimit", required_argument, 0, 'I'},
1746 unsigned limit_mask = 0;
1748 if (has_times_option(argc, argv))
1749 return lfs_setquota_times(argc, argv);
1751 memset(&qctl, 0, sizeof(qctl));
1752 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1753 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1754 * so it can be used as a marker that qc_type
1755 * isn't reinitialized from command line */
1758 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1762 if (qctl.qc_type != UGQUOTA) {
1763 fprintf(stderr, "error: -u and -g can't be used"
1764 " more than once\n");
1767 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1768 rc = name2id(&qctl.qc_id, optarg,
1769 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1771 fprintf(stderr, "error: unknown id %s\n",
1777 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1778 dqb->dqb_bsoftlimit >>= 10;
1779 limit_mask |= BSLIMIT;
1782 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1783 dqb->dqb_bhardlimit >>= 10;
1784 limit_mask |= BHLIMIT;
1787 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1788 limit_mask |= ISLIMIT;
1791 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1792 limit_mask |= IHLIMIT;
1794 default: /* getopt prints error message for us when opterr != 0 */
1799 if (qctl.qc_type == UGQUOTA) {
1800 fprintf(stderr, "error: neither -u nor -g was specified\n");
1804 if (limit_mask == 0) {
1805 fprintf(stderr, "error: at least one limit must be specified\n");
1809 if (optind != argc - 1) {
1810 fprintf(stderr, "error: unexpected parameters encountered\n");
1816 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1817 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1818 /* sigh, we can't just set blimits/ilimits */
1819 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1820 .qc_type = qctl.qc_type,
1821 .qc_id = qctl.qc_id};
1823 rc = llapi_quotactl(mnt, &tmp_qctl);
1825 fprintf(stderr, "error: setquota failed while retrieving"
1826 " current quota settings (%s)\n",
1831 if (!(limit_mask & BHLIMIT))
1832 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1833 if (!(limit_mask & BSLIMIT))
1834 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1835 if (!(limit_mask & IHLIMIT))
1836 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1837 if (!(limit_mask & ISLIMIT))
1838 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1840 /* Keep grace times if we have got no softlimit arguments */
1841 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
1842 dqb->dqb_valid |= QIF_BTIME;
1843 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
1846 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
1847 dqb->dqb_valid |= QIF_ITIME;
1848 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
1852 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1853 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1855 rc = llapi_quotactl(mnt, &qctl);
1858 fprintf(stderr, "%s %s ", obd_type,
1859 obd_uuid2str(&qctl.obd_uuid));
1860 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1867 static inline char *type2name(int check_type)
1869 if (check_type == USRQUOTA)
1871 else if (check_type == GRPQUOTA)
1878 /* Converts seconds value into format string
1879 * result is returned in buf
1881 * 1. result is in descenting order: 1w2d3h4m5s
1882 * 2. zero fields are not filled (except for p. 3): 5d1s
1883 * 3. zero seconds value is presented as "0s"
1885 static void sec2str(time_t seconds, char *buf)
1887 const char spec[] = "smhdw";
1888 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1893 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1894 c = seconds / mult[i];
1896 if (c > 0 || (i == 0 && buf == tail))
1897 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1904 static void diff2str(time_t seconds, char *buf, time_t now)
1910 if (seconds <= now) {
1911 strcpy(buf, "none");
1914 sec2str(seconds - now, buf);
1917 static void print_quota_title(char *name, struct if_quotactl *qctl)
1919 printf("Disk quotas for %s %s (%cid %u):\n",
1920 type2name(qctl->qc_type), name,
1921 *type2name(qctl->qc_type), qctl->qc_id);
1922 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1924 "kbytes", "quota", "limit", "grace",
1925 "files", "quota", "limit", "grace");
1928 static void print_quota(char *mnt, struct if_quotactl *qctl, int type, int rc)
1934 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1935 int bover = 0, iover = 0;
1936 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1938 if (dqb->dqb_bhardlimit &&
1939 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1941 } else if (dqb->dqb_bsoftlimit &&
1942 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1943 if (dqb->dqb_btime > now) {
1950 if (dqb->dqb_ihardlimit &&
1951 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1953 } else if (dqb->dqb_isoftlimit &&
1954 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1955 if (dqb->dqb_btime > now) {
1962 #if 0 /* XXX: always print quotas even when no usages */
1963 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1969 if (strlen(mnt) > 15)
1970 printf("%s\n%15s", mnt, "");
1972 printf("%15s", mnt);
1975 diff2str(dqb->dqb_btime, timebuf, now);
1976 if (rc == -1 && errno == EREMOTEIO)
1977 sprintf(numbuf[0], LPU64"*",
1978 toqb(dqb->dqb_curspace));
1981 (dqb->dqb_valid & QIF_SPACE) ?
1982 LPU64 : "["LPU64"]",
1983 toqb(dqb->dqb_curspace));
1984 if (type == QC_GENERAL)
1985 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
1986 ? LPU64 : "["LPU64"]",
1987 dqb->dqb_bsoftlimit);
1989 sprintf(numbuf[1], "%s", "");
1990 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
1991 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
1992 printf(" %7s%c %6s %7s %7s",
1993 numbuf[0], bover ? '*' : ' ', numbuf[1],
1994 numbuf[2], bover > 1 ? timebuf : "");
1997 diff2str(dqb->dqb_itime, timebuf, now);
1999 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2000 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2001 if (type == QC_GENERAL)
2002 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2003 ? LPU64 : "["LPU64"]",
2004 dqb->dqb_isoftlimit);
2006 sprintf(numbuf[1], "%s", "");
2007 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2008 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2009 if (type != QC_OSTIDX)
2010 printf(" %7s%c %6s %7s %7s",
2011 numbuf[0], iover ? '*' : ' ', numbuf[1],
2012 numbuf[2], iover > 1 ? timebuf : "");
2015 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2016 qctl->qc_cmd == Q_GETOINFO) {
2020 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2021 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2022 printf("Block grace time: %s; Inode grace time: %s\n",
2023 bgtimebuf, igtimebuf);
2027 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2029 int rc = 0, rc1 = 0, count = 0;
2030 __u32 valid = qctl->qc_valid;
2032 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2034 fprintf(stderr, "can not get %s count: %s\n",
2035 is_mdt ? "mdt": "ost", strerror(errno));
2039 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2040 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2041 rc = llapi_quotactl(mnt, qctl);
2043 /* It is remote client case. */
2044 if (errno == EOPNOTSUPP) {
2051 fprintf(stderr, "quotactl %s%d failed.\n",
2052 is_mdt ? "mdt": "ost", qctl->qc_idx);
2056 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid, 0);
2060 qctl->qc_valid = valid;
2064 static int lfs_quota(int argc, char **argv)
2067 char *mnt, *name = NULL;
2068 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2069 .qc_type = UGQUOTA };
2070 char *obd_type = (char *)qctl.obd_type;
2071 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2072 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2073 __u32 valid = QC_GENERAL, idx = 0;
2076 while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2079 if (qctl.qc_type != UGQUOTA) {
2080 fprintf(stderr, "error: use either -u or -g\n");
2083 qctl.qc_type = USRQUOTA;
2086 if (qctl.qc_type != UGQUOTA) {
2087 fprintf(stderr, "error: use either -u or -g\n");
2090 qctl.qc_type = GRPQUOTA;
2093 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2096 valid = qctl.qc_valid = QC_UUID;
2097 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2100 valid = qctl.qc_valid = QC_MDTIDX;
2101 idx = qctl.qc_idx = atoi(optarg);
2104 valid = qctl.qc_valid = QC_OSTIDX;
2105 idx = qctl.qc_idx = atoi(optarg);
2111 fprintf(stderr, "error: %s: option '-%c' "
2112 "unrecognized\n", argv[0], c);
2117 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2118 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2119 optind == argc - 1) {
2121 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2122 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2123 qctl.qc_valid = valid;
2126 qctl.qc_type = USRQUOTA;
2127 qctl.qc_id = geteuid();
2129 qctl.qc_type = GRPQUOTA;
2130 qctl.qc_id = getegid();
2132 rc = id2name(&name, qctl.qc_id,
2133 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2136 /* lfs quota -u username /path/to/lustre/mount */
2137 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2138 /* options should be followed by u/g-name and mntpoint */
2139 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2140 fprintf(stderr, "error: missing quota argument(s)\n");
2144 name = argv[optind++];
2145 rc = name2id(&qctl.qc_id, name,
2146 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2148 fprintf(stderr,"error: can't find id for name %s: %s\n",
2149 name, strerror(errno));
2152 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2153 fprintf(stderr, "error: missing quota info argument(s)\n");
2157 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2158 print_quota_title(name, &qctl);
2162 rc1 = llapi_quotactl(mnt, &qctl);
2163 if (rc1 == -1 && errno == EALREADY) {
2164 fprintf(stderr, "\n%s quotas are not enabled.\n",
2165 qctl.qc_type == USRQUOTA ? "user" : "group");
2168 if (rc1 && *obd_type)
2169 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2171 if (qctl.qc_valid != QC_GENERAL)
2174 print_quota(mnt, &qctl, QC_GENERAL, rc1);
2176 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2177 rc2 = print_obd_quota(mnt, &qctl, 1);
2178 rc3 = print_obd_quota(mnt, &qctl, 0);
2181 if (rc1 || rc2 || rc3)
2182 printf("Some errors happened when getting quota info. "
2183 "Some devices may be not working or deactivated. "
2184 "The data in \"[]\" is inaccurate.\n");
2192 #endif /* HAVE_SYS_QUOTA_H! */
2194 static int flushctx_ioctl(char *mp)
2198 fd = open(mp, O_RDONLY);
2200 fprintf(stderr, "flushctx: error open %s: %s\n",
2201 mp, strerror(errno));
2205 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2207 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2208 mp, strerror(errno));
2214 static int lfs_flushctx(int argc, char **argv)
2216 int kdestroy = 0, c;
2218 char procline[PATH_MAX], *line;
2222 while ((c = getopt(argc, argv, "k")) != -1) {
2228 fprintf(stderr, "error: %s: option '-%c' "
2229 "unrecognized\n", argv[0], c);
2235 system("kdestroy > /dev/null");
2237 if (optind >= argc) {
2238 /* flush for all mounted lustre fs. */
2239 proc = fopen("/proc/mounts", "r");
2241 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2246 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2251 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2252 fprintf(stderr, "%s: unexpected format in "
2258 if (strcmp(fs, "lustre") != 0)
2260 /* we use '@' to determine it's a client. are there
2261 * any other better way?
2263 if (strchr(dev, '@') == NULL)
2266 if (flushctx_ioctl(mp))
2270 /* flush fs as specified */
2271 while (optind < argc) {
2272 if (flushctx_ioctl(argv[optind++]))
2280 static int lfs_lsetfacl(int argc, char **argv)
2283 return(llapi_lsetfacl(argc, argv));
2286 static int lfs_lgetfacl(int argc, char **argv)
2289 return(llapi_lgetfacl(argc, argv));
2292 static int lfs_rsetfacl(int argc, char **argv)
2295 return(llapi_rsetfacl(argc, argv));
2298 static int lfs_rgetfacl(int argc, char **argv)
2301 return(llapi_rgetfacl(argc, argv));
2304 static int lfs_cp(int argc, char **argv)
2306 return(llapi_cp(argc, argv));
2309 static int lfs_ls(int argc, char **argv)
2311 return(llapi_ls(argc, argv));
2314 static int lfs_changelog(int argc, char **argv)
2316 void *changelog_priv;
2317 struct changelog_rec *rec;
2318 long long startrec = 0, endrec = 0;
2320 struct option long_opts[] = {
2321 {"follow", no_argument, 0, 'f'},
2324 char short_opts[] = "f";
2328 while ((c = getopt_long(argc, argv, short_opts,
2329 long_opts, NULL)) != -1) {
2337 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2338 argv[0], argv[optind - 1]);
2345 mdd = argv[optind++];
2347 startrec = strtoll(argv[optind++], NULL, 10);
2349 endrec = strtoll(argv[optind++], NULL, 10);
2351 rc = llapi_changelog_start(&changelog_priv,
2352 follow ? CHANGELOG_FLAG_FOLLOW : 0,
2355 fprintf(stderr, "Can't start changelog: %s\n",
2356 strerror(errno = -rc));
2360 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
2361 if (endrec && rec->cr_index > endrec)
2363 if (rec->cr_index < startrec)
2366 printf(LPU64" %02d%-5s "LPU64" 0x%x t="DFID,
2367 rec->cr_index, rec->cr_type,
2368 changelog_type2str(rec->cr_type), rec->cr_time,
2369 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
2370 if (rec->cr_namelen)
2371 /* namespace rec includes parent and filename */
2372 printf(" p="DFID" %.*s\n", PFID(&rec->cr_pfid),
2373 rec->cr_namelen, rec->cr_name);
2376 llapi_changelog_free(&rec);
2379 llapi_changelog_fini(&changelog_priv);
2381 return (rc == 1 ? 0 : rc);
2384 static int lfs_changelog_clear(int argc, char **argv)
2392 endrec = strtoll(argv[3], NULL, 10);
2394 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
2396 fprintf(stderr, "%s error: %s\n", argv[0],
2397 strerror(errno = -rc));
2401 static int lfs_fid2path(int argc, char **argv)
2403 struct option long_opts[] = {
2404 {"cur", no_argument, 0, 'c'},
2405 {"link", required_argument, 0, 'l'},
2406 {"rec", required_argument, 0, 'r'},
2409 char c, short_opts[] = "cl:r:";
2410 char *device, *fid, *path;
2411 long long recno = -1;
2418 while ((c = getopt_long(argc, argv, short_opts,
2419 long_opts, NULL)) != -1) {
2425 linkno = strtol(optarg, NULL, 10);
2428 recno = strtoll(optarg, NULL, 10);
2433 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2434 argv[0], argv[optind - 1]);
2439 device = argv[optind++];
2440 fid = argv[optind++];
2444 path = calloc(1, PATH_MAX);
2446 lnktmp = (linkno >= 0) ? linkno : 0;
2448 int oldtmp = lnktmp;
2449 long long rectmp = recno;
2450 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2453 fprintf(stderr, "%s error: %s\n", argv[0],
2454 strerror(errno = -rc));
2459 fprintf(stdout, "%lld %s\n", rectmp, path);
2461 fprintf(stdout, "%s\n", path);
2464 /* specified linkno */
2466 if (oldtmp == lnktmp)
2475 static int lfs_path2fid(int argc, char **argv)
2485 rc = llapi_path2fid(path, &fid);
2487 fprintf(stderr, "can't get fid for %s: %s\n", path,
2488 strerror(errno = -rc));
2492 printf(DFID"\n", PFID(&fid));
2497 int main(int argc, char **argv)
2503 ptl_initialize(argc, argv);
2504 if (obd_initialize(argc, argv) < 0)
2506 if (dbg_initialize(argc, argv) < 0)
2509 Parser_init("lfs > ", cmdlist);
2512 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2514 rc = Parser_commands();
2517 obd_finalize(argc, argv);
2518 return rc < 0 ? -rc : rc;