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>
70 #include <lnet/api-support.h>
71 #include <lnet/lnetctl.h>
73 #include <liblustre.h>
74 #include <lustre/lustre_idl.h>
75 #include <lustre/liblustreapi.h>
76 #include <lustre/lustre_user.h>
78 #include <libcfs/libcfsutil.h>
81 unsigned int libcfs_subsystem_debug = 0;
84 static int lfs_setstripe(int argc, char **argv);
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_osts(int argc, char **argv);
88 static int lfs_df(int argc, char **argv);
89 static int lfs_check(int argc, char **argv);
90 static int lfs_catinfo(int argc, char **argv);
91 #ifdef HAVE_SYS_QUOTA_H
92 static int lfs_quotachown(int argc, char **argv);
93 static int lfs_quotacheck(int argc, char **argv);
94 static int lfs_quotaon(int argc, char **argv);
95 static int lfs_quotaoff(int argc, char **argv);
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_quotainv(int argc, char **argv);
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_join(int argc, char **argv);
102 static int lfs_lsetfacl(int argc, char **argv);
103 static int lfs_lgetfacl(int argc, char **argv);
104 static int lfs_rsetfacl(int argc, char **argv);
105 static int lfs_rgetfacl(int argc, char **argv);
106 static int lfs_cp(int argc, char **argv);
107 static int lfs_ls(int argc, char **argv);
108 static int lfs_poollist(int argc, char **argv);
109 static int lfs_changelog(int argc, char **argv);
110 static int lfs_changelog_clear(int argc, char **argv);
111 static int lfs_fid2path(int argc, char **argv);
112 static int lfs_path2fid(int argc, char **argv);
114 /* all avaialable commands */
115 command_t cmdlist[] = {
116 {"setstripe", lfs_setstripe, 0,
117 "Create a new file with a specific striping pattern or\n"
118 "set the default striping pattern on an existing directory or\n"
119 "delete the default striping pattern from an existing directory\n"
120 "usage: setstripe [--size|-s stripe_size] [--offset|-o start_ost]\n"
121 " [--count|-c stripe_count] [--pool|-p pool_name]\n"
124 " setstripe -d <dir> (to delete default striping)\n"
125 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
126 "\t Can be specified with k, m or g (in KB, MB and GB\n"
128 "\tstart_ost: OST index of first stripe (-1 filesystem default)\n"
129 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
130 "\tpool_name: Name of OST pool"},
131 {"getstripe", lfs_getstripe, 0,
132 "To list the striping info for a given file or files in a\n"
133 "directory or recursively for all files in a directory tree.\n"
134 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
135 " [--count | -c ] [--size | -s ] [--index | -i ]\n"
136 " [--offset | -o ] [--pool | -p ]\n"
137 " [--recursive | -r] <dir|file> ..."},
138 {"pool_list", lfs_poollist, 0,
139 "List pools or pool OSTs\n"
140 "usage: pool_list <fsname>[.<poolname>] | <pathname>\n"},
141 {"find", lfs_find, 0,
142 "To find files that match given parameters recursively in a directory tree.\n"
143 "usage: find <dir|file> ... \n"
144 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
145 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
146 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
147 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
148 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
149 " [[!] --pool <name>]\n"
150 "\t !: used before an option indicates 'NOT' the requested attribute\n"
151 "\t -: used before an value indicates 'AT MOST' the requested value\n"
152 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
153 {"check", lfs_check, 0,
154 "Display the status of MDS or OSTs (as specified in the command)\n"
155 "or all the servers (MDS and OSTs).\n"
156 "usage: check <osts|mds|servers>"},
157 {"catinfo", lfs_catinfo, 0,
158 "Show information of specified type logs.\n"
159 "usage: catinfo {keyword} [node name]\n"
160 "\tkeywords are one of followings: config, deletions.\n"
161 "\tnode name must be provided when use keyword config."},
162 {"join", lfs_join, 0,
163 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
164 "usage: join <filename_A> <filename_B>\n"},
165 {"osts", lfs_osts, 0, "osts"},
167 "report filesystem disk space usage or inodes usage"
169 "Usage: df [-i] [-h] [path]"},
170 #ifdef HAVE_SYS_QUOTA_H
171 {"quotachown",lfs_quotachown, 0,
172 "Change files' owner or group on the specified filesystem.\n"
173 "usage: quotachown [-i] <filesystem>\n"
174 "\t-i: ignore error if file is not exist\n"},
175 {"quotacheck", lfs_quotacheck, 0,
176 "Scan the specified filesystem for disk usage, and create,\n"
177 "or update quota files.\n"
178 "usage: quotacheck [ -ug ] <filesystem>"},
179 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
180 "usage: quotaon [ -ugf ] <filesystem>"},
181 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
182 "usage: quotaoff [ -ug ] <filesystem>"},
183 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
184 "usage: setquota [ -u | -g ] <name> -b <block-softlimit> -B <block-hardlimit> -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
185 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>\n"
186 " setquota [ -u | --user | -g | --group ] <name>\n"
187 " [--block-softlimit <block-softlimit>]\n"
188 " [--block-hardlimit <block-hardlimit>]\n"
189 " [--inode-softlimit <inode-softlimit>]\n"
190 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
191 " setquota [-t] [ -u | --user | -g | --group ]\n"
192 " [--block-grace <block-grace>]\n"
193 " [--inode-grace <inode-grace>] <filesystem>\n"
194 " -b can be used instead of --block-softlimit/--block-grace\n"
195 " -B can be used instead of --block-hardlimit\n"
196 " -i can be used instead of --inode-softlimit/--inode-grace\n"
197 " -I can be used instead of --inode-hardlimit"},
198 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
199 "usage: quota [-v] [-o obd_uuid|-i mdt_idx|-I ost_idx] [{-u|-g <name>}|-t] <filesystem>"},
200 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
201 "usage: quotainv [-u|-g] <filesystem>"},
203 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
204 "usage: flushctx [-k] [mountpoint...]"},
205 {"lsetfacl", lfs_lsetfacl, 0,
206 "Remote user setfacl for user/group on the same remote client.\n"
207 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
208 {"lgetfacl", lfs_lgetfacl, 0,
209 "Remote user getfacl for user/group on the same remote client.\n"
210 "usage: lgetfacl [-dRLPvh] file ..."},
211 {"rsetfacl", lfs_rsetfacl, 0,
212 "Remote user setfacl for user/group on other clients.\n"
213 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
214 {"rgetfacl", lfs_rgetfacl, 0,
215 "Remote user getfacl for user/group on other clients.\n"
216 "usage: rgetfacl [-dRLPvh] file ..."},
218 "Remote user copy files and directories.\n"
219 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
221 "Remote user list directory contents.\n"
222 "usage: ls [OPTION]... [FILE]..."},
223 {"changelog", lfs_changelog, 0,
224 "Show the metadata changes on an MDT."
225 "\nusage: changelog [--follow] <mdtname> [startrec [endrec]]"
226 "\n(note: --follow is only valid when run on MDT node)"},
227 {"changelog_clear", lfs_changelog_clear, 0,
228 "Indicate that old changelog records up to <endrec> are no longer of "
229 "interest to consumer <id>, allowing the system to free up space.\n"
230 "An <endrec> of 0 means all records.\n"
231 "usage: changelog_clear <mdtname> <id> <endrec>"},
232 {"fid2path", lfs_fid2path, 0,
233 "Resolve the full path to a given FID. For a specific hardlink "
234 "specify link number <linkno>.\n"
235 /* "For a historical name, specify changelog record <recno>.\n" */
236 "usage: fid2path <fsname|rootpath> <fid> [--link <linkno>]"
237 /*[--rec <recno>]*/},
238 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
239 "usage: path2fid <path>"},
240 {"help", Parser_help, 0, "help"},
241 {"exit", Parser_quit, 0, "quit"},
242 {"quit", Parser_quit, 0, "quit"},
246 static int isnumber(const char *str)
250 if (str[0] != '-' && !isdigit(str[0]))
253 for (ptr = str + 1; *ptr != '\0'; ptr++) {
262 static int lfs_setstripe(int argc, char **argv)
266 unsigned long long st_size;
267 int st_offset, st_count;
271 char *stripe_size_arg = NULL;
272 char *stripe_off_arg = NULL;
273 char *stripe_count_arg = NULL;
274 char *pool_name_arg = NULL;
275 unsigned long long size_units;
277 struct option long_opts[] = {
278 {"size", required_argument, 0, 's'},
279 {"count", required_argument, 0, 'c'},
280 {"index", required_argument, 0, 'i'},
281 {"offset", required_argument, 0, 'o'},
282 {"pool", required_argument, 0, 'p'},
283 {"delete", no_argument, 0, 'd'},
291 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
292 if (argc == 5 && argv[1][0] != '-' &&
293 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
294 fprintf(stderr, "warning: deprecated usage of setstripe "
295 "positional parameters. Use -c, -i, -s instead.\n");
296 /* for compatibility with the existing positional parameter
299 stripe_size_arg = argv[2];
300 stripe_off_arg = argv[3];
301 stripe_count_arg = argv[4];
305 #warning "remove obsolete positional parameter code"
309 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
310 long_opts, NULL)) >= 0) {
316 stripe_count_arg = optarg;
319 /* delete the default striping pattern */
324 stripe_off_arg = optarg;
327 stripe_size_arg = optarg;
330 pool_name_arg = optarg;
335 fprintf(stderr, "error: %s: option '%s' "
337 argv[0], argv[optind - 1]);
342 fname = argv[optind];
345 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
346 stripe_count_arg != NULL || pool_name_arg != NULL)) {
347 fprintf(stderr, "error: %s: cannot specify -d with "
348 "-s, -c -o or -p options\n",
354 if (optind == argc) {
355 fprintf(stderr, "error: %s: missing filename|dirname\n",
360 /* get the stripe size */
361 if (stripe_size_arg != NULL) {
362 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
364 fprintf(stderr, "error: %s: bad size '%s'\n",
365 argv[0], stripe_size_arg);
369 /* get the stripe offset */
370 if (stripe_off_arg != NULL) {
371 st_offset = strtoul(stripe_off_arg, &end, 0);
373 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
374 argv[0], stripe_off_arg);
378 /* get the stripe count */
379 if (stripe_count_arg != NULL) {
380 st_count = strtoul(stripe_count_arg, &end, 0);
382 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
383 argv[0], stripe_count_arg);
389 result = llapi_file_create_pool(fname, st_size, st_offset,
390 st_count, 0, pool_name_arg);
392 fprintf(stderr,"error: %s: create stripe file '%s' "
393 "failed\n", argv[0], fname);
396 fname = argv[++optind];
397 } while (fname != NULL);
402 static int lfs_poollist(int argc, char **argv)
407 return llapi_poollist(argv[1]);
410 static int set_time(time_t *time, time_t *set, char *str)
417 else if (str[0] == '-')
423 t = strtol(str, NULL, 0);
424 if (*time < t * 24 * 60 * 60) {
427 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
431 *set = *time - t * 24 * 60 * 60;
438 static int name2id(unsigned int *id, char *name, int type)
441 struct passwd *entry;
443 if (!(entry = getpwnam(name))) {
453 if (!(entry = getgrnam(name))) {
465 static int id2name(char **name, unsigned int id, int type)
468 struct passwd *entry;
470 if (!(entry = getpwuid(id))) {
476 *name = entry->pw_name;
480 if (!(entry = getgrgid(id))) {
486 *name = entry->gr_name;
492 #define FIND_POOL_OPT 3
493 static int lfs_find(int argc, char **argv)
498 struct find_param param = { .maxdepth = -1 };
500 struct option long_opts[] = {
501 /* New find options. */
502 {"atime", required_argument, 0, 'A'},
503 {"ctime", required_argument, 0, 'C'},
504 {"mtime", required_argument, 0, 'M'},
505 {"maxdepth", required_argument, 0, 'D'},
506 {"gid", required_argument, 0, 'g'},
507 {"group", required_argument, 0, 'G'},
508 {"uid", required_argument, 0, 'u'},
509 {"user", required_argument, 0, 'U'},
510 {"name", required_argument, 0, 'n'},
511 /* no short option for pool, p/P already used */
512 {"pool", required_argument, 0, FIND_POOL_OPT},
513 /* --obd is considered as a new option. */
514 {"obd", required_argument, 0, 'O'},
515 {"ost", required_argument, 0, 'O'},
516 {"print", no_argument, 0, 'P'},
517 {"print0", no_argument, 0, 'p'},
518 /* Old find options. */
519 {"quiet", no_argument, 0, 'q'},
520 {"recursive", no_argument, 0, 'r'},
521 {"size", required_argument, 0, 's'},
522 {"type", required_argument, 0, 't'},
523 {"verbose", no_argument, 0, 'v'},
537 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
538 while ((c = getopt_long_only(argc,argv,"-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
539 long_opts, NULL)) >= 0) {
544 /* '!' is part of option */
545 /* when getopt_long_only() finds a string which is not
546 * an option nor a known option argument it returns 1
547 * in that case if we already have found pathstart and pathend
548 * (i.e. we have the list of pathnames),
549 * the only supported value is "!"
551 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
552 if (!isoption && pathend != -1) {
553 fprintf(stderr, "err: %s: filename|dirname must either "
554 "precede options or follow options\n",
558 if (!isoption && pathstart == -1)
559 pathstart = optind - 1;
560 if (isoption && pathstart != -1 && pathend == -1) {
561 pathend = optind - 2;
562 if ((c == 1 && strcmp(optarg, "!") == 0) ||
563 c == 'P' || c == 'p' || c == 'O' ||
564 c == 'q' || c == 'r' || c == 'v')
565 pathend = optind - 1;
572 /* unknown; opt is "!" or path component,
573 * checking done above.
575 if (strcmp(optarg, "!") == 0)
579 xtime = ¶m.atime;
580 xsign = ¶m.asign;
583 xtime = ¶m.ctime;
584 xsign = ¶m.csign;
588 xtime = ¶m.mtime;
589 xsign = ¶m.msign;
593 if (optarg[0] == '-')
595 else if (optarg[0] == '+')
604 ret = set_time(&t, xtime, optarg);
612 param.maxdepth = strtol(optarg, 0, 0);
616 param.gid = strtol(optarg, &endptr, 10);
617 if (optarg == endptr) {
618 fprintf(stderr, "Bad gid: %s\n", optarg);
621 param.exclude_gid = !!neg_opt;
626 param.gid = strtol(optarg, &endptr, 10);
627 if (optarg == endptr) {
628 ret = name2id(¶m.gid, optarg, GROUP);
630 fprintf(stderr, "Group/GID: %s cannot "
631 "be found.\n", optarg);
635 param.exclude_gid = !!neg_opt;
640 param.uid = strtol(optarg, &endptr, 10);
641 if (optarg == endptr) {
642 fprintf(stderr, "Bad uid: %s\n", optarg);
645 param.exclude_uid = !!neg_opt;
650 param.uid = strtol(optarg, &endptr, 10);
651 if (optarg == endptr) {
652 ret = name2id(¶m.uid, optarg, USER);
654 fprintf(stderr, "User/UID: %s cannot "
655 "be found.\n", optarg);
659 param.exclude_uid = !!neg_opt;
664 if (strlen(optarg) > LOV_MAXPOOLNAME) {
666 "Pool name %s is too long"
667 " (max is %d)\n", optarg,
671 /* we do check for empty pool because empty pool
672 * is used to find V1 lov attributes */
673 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
674 param.poolname[LOV_MAXPOOLNAME] = '\0';
675 param.exclude_pool = !!neg_opt;
676 param.check_pool = 1;
680 param.pattern = (char *)optarg;
681 param.exclude_pattern = !!neg_opt;
684 char *buf, *token, *next, *p;
687 len = strlen((char *)optarg);
691 strcpy(buf, (char *)optarg);
693 if (param.num_alloc_obds == 0) {
694 param.obduuid = malloc(FIND_MAX_OSTS *
695 sizeof(struct obd_uuid));
696 if (param.obduuid == NULL)
698 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
701 for (token = buf; token && *token; token = next) {
702 p = strchr(token, ',');
708 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
732 param.exclude_type = !!neg_opt;
734 case 'b': param.type = S_IFBLK; break;
735 case 'c': param.type = S_IFCHR; break;
736 case 'd': param.type = S_IFDIR; break;
737 case 'f': param.type = S_IFREG; break;
738 case 'l': param.type = S_IFLNK; break;
739 case 'p': param.type = S_IFIFO; break;
740 case 's': param.type = S_IFSOCK; break;
741 #ifdef S_IFDOOR /* Solaris only */
742 case 'D': param.type = S_IFDOOR; break;
744 default: fprintf(stderr, "error: %s: bad type '%s'\n",
751 if (optarg[0] == '-')
753 else if (optarg[0] == '+')
762 if (optarg[0] == '+')
763 param.size_sign = -1;
764 else if (optarg[0] == '-')
765 param.size_sign = +1;
769 ret = parse_size(optarg, ¶m.size,
770 ¶m.size_units, 0);
772 fprintf(stderr,"error: bad size '%s'\n",
785 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
786 argv[0], argv[optind - 1]);
791 if (pathstart == -1) {
792 fprintf(stderr, "error: %s: no filename|pathname\n",
795 } else if (pathend == -1) {
803 static int deprecated_warning;
804 if (!deprecated_warning) {
805 fprintf(stderr, "lfs find: -q, -r, -v options "
806 "deprecated. Use 'lfs getstripe' instead.\n");
807 deprecated_warning = 1;
809 if (!param.recursive && param.maxdepth == -1)
815 ret = llapi_find(argv[pathstart], ¶m);
817 ret = llapi_getstripe(argv[pathstart], ¶m);
818 } while (++pathstart < pathend && !ret);
821 fprintf(stderr, "error: %s failed for %s.\n",
822 argv[0], argv[optind - 1]);
824 if (param.obduuid && param.num_alloc_obds)
830 static int lfs_getstripe(int argc, char **argv)
832 struct option long_opts[] = {
834 {"quiet", 0, 0, 'q'},
835 {"recursive", 0, 0, 'r'},
836 {"count", 0, 0, 'c'},
838 {"index", 0, 0, 'i'},
839 {"offset", 0, 0, 'o'},
841 {"verbose", 0, 0, 'v'},
844 char short_opts[] = "hO:qrvcsiop";
846 struct find_param param = { 0 };
849 while ((c = getopt_long(argc, argv, short_opts,
850 long_opts, NULL)) != -1) {
855 "error: %s: only one obduuid allowed",
859 param.obduuid = (struct obd_uuid *)optarg;
868 param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
872 param.verbose |= VERBOSE_COUNT;
875 param.verbose |= VERBOSE_SIZE;
879 param.verbose |= VERBOSE_OFFSET;
882 param.verbose |= VERBOSE_POOL;
887 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
888 argv[0], argv[optind - 1]);
896 param.maxdepth = param.recursive ? -1 : 1;
899 rc = llapi_getstripe(argv[optind], ¶m);
900 } while (++optind < argc && !rc);
903 fprintf(stderr, "error: %s failed for %s.\n",
904 argv[0], argv[optind - 1]);
908 static int lfs_osts(int argc, char **argv)
911 struct mntent *mnt = NULL;
912 struct find_param param;
918 fp = setmntent(MOUNTED, "r");
921 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
925 while (feof(fp) == 0 && ferror(fp) ==0) {
926 memset(¶m, 0, sizeof(param));
927 if (llapi_is_lustre_mnt(mnt)) {
928 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
931 "error: %s: failed on %s\n",
932 argv[0], mnt->mnt_dir);
942 #define COOK(value) \
945 while (value > 1024) { \
959 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
961 char rpath[PATH_MAX] = {'\0'};
963 int rc, len, out_len = 0;
965 if (!realpath(path, rpath)) {
967 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
968 path, strerror(-rc));
974 while (feof(fp) == 0 && ferror(fp) == 0) {
975 if (llapi_is_lustre_mnt(mnt)) {
976 len = strlen(mnt->mnt_dir);
978 !strncmp(rpath, mnt->mnt_dir, len)) {
980 memset(mntdir, 0, dir_len);
981 strncpy(mntdir, mnt->mnt_dir, dir_len);
990 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
994 static int showdf(char *mntdir, struct obd_statfs *stat,
995 char *uuid, int ishow, int cooked,
996 char *type, int index, int rc)
998 long long avail, used, total;
1000 char *suffix = "KMGTPEZY";
1001 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
1002 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
1010 avail = stat->os_ffree;
1011 used = stat->os_files - stat->os_ffree;
1012 total = stat->os_files;
1014 int shift = cooked ? 0 : 10;
1016 avail = (stat->os_bavail * stat->os_bsize) >> shift;
1017 used = ((stat->os_blocks - stat->os_bfree) *
1018 stat->os_bsize) >> shift;
1019 total = (stat->os_blocks * stat->os_bsize) >> shift;
1023 ratio = (double)used / (double)total;
1029 cook_val = (double)total;
1032 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
1034 sprintf(tbuf, CDF, total);
1036 cook_val = (double)used;
1039 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
1041 sprintf(ubuf, CDF, used);
1043 cook_val = (double)avail;
1046 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
1048 sprintf(abuf, CDF, avail);
1050 sprintf(tbuf, CDF, total);
1051 sprintf(ubuf, CDF, used);
1052 sprintf(abuf, CDF, avail);
1055 sprintf(rbuf, RDF, (int)(ratio * 100));
1056 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1057 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1059 printf("[%s:%d]\n", type, index);
1065 printf(UUF": inactive device\n", uuid);
1068 printf(UUF": %s\n", uuid, strerror(-rc));
1075 static int mntdf(char *mntdir, int ishow, int cooked)
1077 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1078 struct obd_uuid uuid_buf;
1083 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1084 "UUID", "Inodes", "IUsed", "IFree",
1085 "IUse%", "Mounted on");
1087 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1088 "UUID", cooked ? "bytes" : "1K-blocks",
1089 "Used", "Available", "Use%", "Mounted on");
1091 for (index = 0; ; index++) {
1092 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1093 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1094 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1095 &stat_buf, &uuid_buf);
1102 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1103 rc == -ENODATA || rc == 0) {
1104 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1105 ishow, cooked, "MDT", index, rc);
1108 "error: llapi_obd_statfs(%s): %s (%d)\n",
1109 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1113 sum.os_ffree += stat_buf.os_ffree;
1114 sum.os_files += stat_buf.os_files;
1118 for (index = 0; ; index++) {
1119 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1120 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1121 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1122 &stat_buf, &uuid_buf);
1129 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1130 rc == -ENODATA || rc == 0) {
1131 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1132 ishow, cooked, "OST", index, rc);
1135 "error: llapi_obd_statfs failed: %s (%d)\n",
1140 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1141 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1142 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1147 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1152 static int lfs_df(int argc, char **argv)
1156 struct mntent *mnt = NULL;
1157 char *mntdir = NULL;
1158 int ishow = 0, cooked = 0;
1162 while ((c = getopt(argc, argv, "ih")) != -1) {
1175 path = argv[optind];
1177 fp = setmntent(MOUNTED, "r");
1180 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1181 argv[0], MOUNTED, strerror(errno));
1185 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1186 fprintf(stderr, "error: cannot allocate %d bytes\n",
1190 memset(mntdir, 0, PATH_MAX);
1193 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1200 rc = mntdf(mntdir, ishow, cooked);
1204 mnt = getmntent(fp);
1205 while (feof(fp) == 0 && ferror(fp) == 0) {
1206 if (llapi_is_lustre_mnt(mnt)) {
1207 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1212 mnt = getmntent(fp);
1221 static int lfs_check(int argc, char **argv)
1225 struct mntent *mnt = NULL;
1234 obd_types[0] = obd_type1;
1235 obd_types[1] = obd_type2;
1237 if (strcmp(argv[1], "osts") == 0) {
1238 strcpy(obd_types[0], "osc");
1239 } else if (strcmp(argv[1], "mds") == 0) {
1240 strcpy(obd_types[0], "mdc");
1241 } else if (strcmp(argv[1], "servers") == 0) {
1243 strcpy(obd_types[0], "osc");
1244 strcpy(obd_types[1], "mdc");
1246 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1251 fp = setmntent(MOUNTED, "r");
1253 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1256 mnt = getmntent(fp);
1257 while (feof(fp) == 0 && ferror(fp) ==0) {
1258 if (llapi_is_lustre_mnt(mnt))
1260 mnt = getmntent(fp);
1266 fprintf(stderr, "No suitable Lustre mount found\n");
1270 rc = llapi_target_iterate(num_types, obd_types,
1271 mnt->mnt_dir, llapi_ping_target);
1274 fprintf(stderr, "error: %s: %s status failed\n",
1281 static int lfs_catinfo(int argc, char **argv)
1284 struct mntent *mnt = NULL;
1287 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1290 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1293 fp = setmntent(MOUNTED, "r");
1295 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1298 mnt = getmntent(fp);
1299 while (feof(fp) == 0 && ferror(fp) == 0) {
1300 if (llapi_is_lustre_mnt(mnt))
1302 mnt = getmntent(fp);
1309 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1311 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1313 fprintf(stderr, "no lustre_lite mounted.\n");
1320 int lfs_join(int argc, char **argv)
1322 char *name_head, *name_tail;
1328 name_head = argv[1];
1329 fd = open(name_head, O_WRONLY);
1331 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1335 size = lseek(fd, 0, SEEK_END);
1336 if (size % JOIN_FILE_ALIGN) {
1337 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1338 name_head, (long long)size, JOIN_FILE_ALIGN);
1342 name_tail = argv[2];
1343 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1347 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1353 #ifdef HAVE_SYS_QUOTA_H
1354 static int lfs_quotachown(int argc, char **argv)
1361 while ((c = getopt(argc, argv, "i")) != -1) {
1367 fprintf(stderr, "error: %s: option '-%c' "
1368 "unrecognized\n", argv[0], c);
1374 rc = llapi_quotachown(argv[optind], flag);
1376 fprintf(stderr,"error: change file owner/group failed.\n");
1380 static int lfs_quotacheck(int argc, char **argv)
1382 int c, check_type = 0;
1384 struct if_quotacheck qchk;
1385 struct if_quotactl qctl;
1386 char *obd_type = (char *)qchk.obd_type;
1389 memset(&qchk, 0, sizeof(qchk));
1392 while ((c = getopt(argc, argv, "ug")) != -1) {
1401 fprintf(stderr, "error: %s: option '-%c' "
1402 "unrecognized\n", argv[0], c);
1409 else /* do quotacheck for both user & group quota by default */
1417 memset(&qctl, 0, sizeof(qctl));
1418 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1419 qctl.qc_type = check_type;
1420 rc = llapi_quotactl(mnt, &qctl);
1421 if (rc && errno != EALREADY) {
1422 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1426 rc = llapi_quotacheck(mnt, check_type);
1428 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1432 rc = llapi_poll_quotacheck(mnt, &qchk);
1435 fprintf(stderr, "%s %s ", obd_type,
1436 obd_uuid2str(&qchk.obd_uuid));
1437 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1441 memset(&qctl, 0, sizeof(qctl));
1442 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1443 qctl.qc_type = check_type;
1444 rc = llapi_quotactl(mnt, &qctl);
1445 if (rc && errno != EALREADY) {
1447 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1448 obd_uuid2str(&qctl.obd_uuid));
1449 fprintf(stderr, "%s turn on quota failed: %s\n",
1450 argv[0], strerror(errno));
1457 static int lfs_quotaon(int argc, char **argv)
1461 struct if_quotactl qctl;
1462 char *obd_type = (char *)qctl.obd_type;
1465 memset(&qctl, 0, sizeof(qctl));
1466 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1469 while ((c = getopt(argc, argv, "ugf")) != -1) {
1472 qctl.qc_type |= 0x01;
1475 qctl.qc_type |= 0x02;
1478 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1481 fprintf(stderr, "error: %s: option '-%c' "
1482 "unrecognized\n", argv[0], c);
1489 else /* by default, enable quota for both user & group */
1490 qctl.qc_type = 0x02;
1497 rc = llapi_quotactl(mnt, &qctl);
1499 if (errno == EALREADY) {
1500 fprintf(stderr, "\n%s quotas are enabled already.\n",
1501 qctl.qc_type == 0x02 ? "user/group" :
1502 (qctl.qc_type == 0x00 ? "user" : "group"));
1506 fprintf(stderr, "%s %s ", obd_type,
1507 obd_uuid2str(&qctl.obd_uuid));
1508 fprintf(stderr, "%s failed: %s\n", argv[0],
1516 static int lfs_quotaoff(int argc, char **argv)
1520 struct if_quotactl qctl;
1521 char *obd_type = (char *)qctl.obd_type;
1524 memset(&qctl, 0, sizeof(qctl));
1525 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1528 while ((c = getopt(argc, argv, "ug")) != -1) {
1531 qctl.qc_type |= 0x01;
1534 qctl.qc_type |= 0x02;
1537 fprintf(stderr, "error: %s: option '-%c' "
1538 "unrecognized\n", argv[0], c);
1545 else /* by default, disable quota for both user & group */
1546 qctl.qc_type = 0x02;
1553 rc = llapi_quotactl(mnt, &qctl);
1555 if (errno == EALREADY) {
1556 fprintf(stderr, "\n%s quotas are disabled already.\n",
1557 qctl.qc_type == 0x02 ? "user/group" :
1558 (qctl.qc_type == 0x00 ? "user" : "group"));
1562 fprintf(stderr, "%s %s ", obd_type,
1563 obd_uuid2str(&qctl.obd_uuid));
1564 fprintf(stderr, "quotaoff failed: %s\n",
1572 static int lfs_quotainv(int argc, char **argv)
1576 struct if_quotactl qctl;
1579 memset(&qctl, 0, sizeof(qctl));
1580 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1583 while ((c = getopt(argc, argv, "ugf")) != -1) {
1586 qctl.qc_type |= 0x01;
1589 qctl.qc_type |= 0x02;
1592 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1595 fprintf(stderr, "error: %s: option '-%c' "
1596 "unrecognized\n", argv[0], c);
1603 else /* by default, invalidate quota for both user & group */
1604 qctl.qc_type = 0x02;
1611 rc = llapi_quotactl(mnt, &qctl);
1613 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1620 #define ARG2INT(nr, str, msg) \
1623 nr = strtol(str, &endp, 0); \
1625 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1630 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1632 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1633 * returns the value or ULONG_MAX on integer overflow or incorrect format
1635 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1636 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1637 * 3. empty integer value is interpreted as 0
1640 static unsigned long str2sec(const char* timestr) {
1641 const char spec[] = "smhdw";
1642 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1643 unsigned long val = 0;
1646 if (strpbrk(timestr, spec) == NULL) {
1647 /* no specifiers inside the time string,
1648 should treat it as an integer value */
1649 val = strtoul(timestr, &tail, 10);
1650 return *tail ? ULONG_MAX : val;
1653 /* format string is XXwXXdXXhXXmXXs */
1659 v = strtoul(timestr, &tail, 10);
1660 if (v == ULONG_MAX || *tail == '\0')
1661 /* value too large (ULONG_MAX or more)
1662 or missing specifier */
1665 ptr = strchr(spec, *tail);
1667 /* unknown specifier */
1672 /* check if product will overflow the type */
1673 if (!(v < ULONG_MAX / mult[ind]))
1676 ADD_OVERFLOW(val, mult[ind] * v);
1677 if (val == ULONG_MAX)
1689 #define ARG2ULL(nr, str, defscale) \
1691 unsigned long long limit, units = 0; \
1694 rc = parse_size(str, &limit, &units, 1); \
1696 fprintf(stderr, "error: bad limit value %s\n", str); \
1699 nr = ((units == 0) ? (defscale) : 1) * limit; \
1702 static inline int has_times_option(int argc, char **argv)
1706 for (i = 1; i < argc; i++)
1707 if (!strcmp(argv[i], "-t"))
1713 int lfs_setquota_times(int argc, char **argv)
1716 struct if_quotactl qctl;
1717 char *mnt, *obd_type = (char *)qctl.obd_type;
1718 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1719 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1720 struct option long_opts[] = {
1721 {"user", no_argument, 0, 'u'},
1722 {"group", no_argument, 0, 'g'},
1723 {"block-grace", required_argument, 0, 'b'},
1724 {"inode-grace", required_argument, 0, 'i'},
1725 {"times", no_argument, 0, 't'},
1729 memset(&qctl, 0, sizeof(qctl));
1730 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1731 qctl.qc_type = UGQUOTA;
1734 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1738 if (qctl.qc_type != UGQUOTA) {
1739 fprintf(stderr, "error: -u and -g can't be used "
1740 "more than once\n");
1743 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1746 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1747 fprintf(stderr, "error: bad block-grace: %s\n",
1751 dqb->dqb_valid |= QIF_BTIME;
1754 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1755 fprintf(stderr, "error: bad inode-grace: %s\n",
1759 dqb->dqb_valid |= QIF_ITIME;
1761 case 't': /* Yes, of course! */
1763 default: /* getopt prints error message for us when opterr != 0 */
1768 if (qctl.qc_type == UGQUOTA) {
1769 fprintf(stderr, "error: neither -u nor -g specified\n");
1773 if (optind != argc - 1) {
1774 fprintf(stderr, "error: unexpected parameters encountered\n");
1779 rc = llapi_quotactl(mnt, &qctl);
1782 fprintf(stderr, "%s %s ", obd_type,
1783 obd_uuid2str(&qctl.obd_uuid));
1784 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1791 #define BSLIMIT (1 << 0)
1792 #define BHLIMIT (1 << 1)
1793 #define ISLIMIT (1 << 2)
1794 #define IHLIMIT (1 << 3)
1796 int lfs_setquota(int argc, char **argv)
1799 struct if_quotactl qctl;
1800 char *mnt, *obd_type = (char *)qctl.obd_type;
1801 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1802 struct option long_opts[] = {
1803 {"user", required_argument, 0, 'u'},
1804 {"group", required_argument, 0, 'g'},
1805 {"block-softlimit", required_argument, 0, 'b'},
1806 {"block-hardlimit", required_argument, 0, 'B'},
1807 {"inode-softlimit", required_argument, 0, 'i'},
1808 {"inode-hardlimit", required_argument, 0, 'I'},
1811 unsigned limit_mask = 0;
1813 if (has_times_option(argc, argv))
1814 return lfs_setquota_times(argc, argv);
1816 memset(&qctl, 0, sizeof(qctl));
1817 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1818 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1819 * so it can be used as a marker that qc_type
1820 * isn't reinitialized from command line */
1823 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1827 if (qctl.qc_type != UGQUOTA) {
1828 fprintf(stderr, "error: -u and -g can't be used"
1829 " more than once\n");
1832 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1833 rc = name2id(&qctl.qc_id, optarg,
1834 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1836 fprintf(stderr, "error: unknown id %s\n",
1842 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1843 dqb->dqb_bsoftlimit >>= 10;
1844 limit_mask |= BSLIMIT;
1847 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1848 dqb->dqb_bhardlimit >>= 10;
1849 limit_mask |= BHLIMIT;
1852 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1853 limit_mask |= ISLIMIT;
1856 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1857 limit_mask |= IHLIMIT;
1859 default: /* getopt prints error message for us when opterr != 0 */
1864 if (qctl.qc_type == UGQUOTA) {
1865 fprintf(stderr, "error: neither -u nor -g are specified\n");
1869 if (limit_mask == 0) {
1870 fprintf(stderr, "error: at least one limit must be specified\n");
1874 if (optind != argc - 1) {
1875 fprintf(stderr, "error: unexpected parameters encountered\n");
1881 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1882 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1883 /* sigh, we can't just set blimits/ilimits */
1884 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1885 .qc_type = qctl.qc_type,
1886 .qc_id = qctl.qc_id};
1888 rc = llapi_quotactl(mnt, &tmp_qctl);
1890 fprintf(stderr, "error: getquota failed\n");
1894 if (!(limit_mask & BHLIMIT))
1895 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1896 if (!(limit_mask & BSLIMIT))
1897 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1898 if (!(limit_mask & IHLIMIT))
1899 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1900 if (!(limit_mask & ISLIMIT))
1901 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1904 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1905 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1907 rc = llapi_quotactl(mnt, &qctl);
1910 fprintf(stderr, "%s %s ", obd_type,
1911 obd_uuid2str(&qctl.obd_uuid));
1912 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1919 static inline char *type2name(int check_type)
1921 if (check_type == USRQUOTA)
1923 else if (check_type == GRPQUOTA)
1930 /* Converts seconds value into format string
1931 * result is returned in buf
1933 * 1. result is in descenting order: 1w2d3h4m5s
1934 * 2. zero fields are not filled (except for p. 3): 5d1s
1935 * 3. zero seconds value is presented as "0s"
1937 static void sec2str(time_t seconds, char *buf)
1939 const char spec[] = "smhdw";
1940 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1945 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1946 c = seconds / mult[i];
1948 if (c > 0 || (i == 0 && buf == tail))
1949 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1956 static void diff2str(time_t seconds, char *buf, time_t now)
1962 if (seconds <= now) {
1963 strcpy(buf, "none");
1966 sec2str(seconds - now, buf);
1969 static void print_quota_title(char *name, struct if_quotactl *qctl)
1971 printf("Disk quotas for %s %s (%cid %u):\n",
1972 type2name(qctl->qc_type), name,
1973 *type2name(qctl->qc_type), qctl->qc_id);
1974 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1976 "kbytes", "quota", "limit", "grace",
1977 "files", "quota", "limit", "grace");
1980 static void print_quota(char *mnt, struct if_quotactl *qctl, int type, int rc)
1986 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1987 int bover = 0, iover = 0;
1988 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1990 if (dqb->dqb_bhardlimit &&
1991 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1993 } else if (dqb->dqb_bsoftlimit &&
1994 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1995 if (dqb->dqb_btime > now) {
2002 if (dqb->dqb_ihardlimit &&
2003 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2005 } else if (dqb->dqb_isoftlimit &&
2006 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
2007 if (dqb->dqb_btime > now) {
2014 #if 0 /* XXX: always print quotas even when no usages */
2015 if (dqb->dqb_curspace || dqb->dqb_curinodes)
2021 if (strlen(mnt) > 15)
2022 printf("%s\n%15s", mnt, "");
2024 printf("%15s", mnt);
2027 diff2str(dqb->dqb_btime, timebuf, now);
2028 if (rc == -1 && errno == EREMOTEIO)
2029 sprintf(numbuf[0], LPU64"*",
2030 toqb(dqb->dqb_curspace));
2033 (dqb->dqb_valid & QIF_SPACE) ?
2034 LPU64 : "["LPU64"]",
2035 toqb(dqb->dqb_curspace));
2036 if (type == QC_GENERAL)
2037 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2038 ? LPU64 : "["LPU64"]",
2039 dqb->dqb_bsoftlimit);
2041 sprintf(numbuf[1], "%s", "");
2042 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2043 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2044 printf(" %7s%c %6s %7s %7s",
2045 numbuf[0], bover ? '*' : ' ', numbuf[1],
2046 numbuf[2], bover > 1 ? timebuf : "");
2049 diff2str(dqb->dqb_itime, timebuf, now);
2051 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2052 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2053 if (type == QC_GENERAL)
2054 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2055 ? LPU64 : "["LPU64"]",
2056 dqb->dqb_isoftlimit);
2058 sprintf(numbuf[1], "%s", "");
2059 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2060 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2061 if (type != QC_OSTIDX)
2062 printf(" %7s%c %6s %7s %7s",
2063 numbuf[0], iover ? '*' : ' ', numbuf[1],
2064 numbuf[2], iover > 1 ? timebuf : "");
2067 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2068 qctl->qc_cmd == Q_GETOINFO) {
2072 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2073 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2074 printf("Block grace time: %s; Inode grace time: %s\n",
2075 bgtimebuf, igtimebuf);
2079 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2081 int rc = 0, rc1 = 0, count = 0;
2082 __u32 valid = qctl->qc_valid;
2084 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2086 fprintf(stderr, "can not get %s count: %s\n",
2087 is_mdt ? "mdt": "ost", strerror(errno));
2091 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2092 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2093 rc = llapi_quotactl(mnt, qctl);
2095 /* It is remote client case. */
2096 if (errno == EOPNOTSUPP) {
2103 fprintf(stderr, "quotactl %s%d failed.\n",
2104 is_mdt ? "mdt": "ost", qctl->qc_idx);
2108 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid, 0);
2112 qctl->qc_valid = valid;
2116 static int lfs_quota(int argc, char **argv)
2119 char *mnt, *name = NULL;
2120 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2121 .qc_type = UGQUOTA };
2122 char *obd_type = (char *)qctl.obd_type;
2123 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2124 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2125 __u32 valid = QC_GENERAL, idx = 0;
2128 while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2131 if (qctl.qc_type != UGQUOTA) {
2132 fprintf(stderr, "error: use either -u or -g\n");
2135 qctl.qc_type = USRQUOTA;
2138 if (qctl.qc_type != UGQUOTA) {
2139 fprintf(stderr, "error: use either -u or -g\n");
2142 qctl.qc_type = GRPQUOTA;
2145 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2148 valid = qctl.qc_valid = QC_UUID;
2149 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2152 valid = qctl.qc_valid = QC_MDTIDX;
2153 idx = qctl.qc_idx = atoi(optarg);
2156 valid = qctl.qc_valid = QC_OSTIDX;
2157 idx = qctl.qc_idx = atoi(optarg);
2163 fprintf(stderr, "error: %s: option '-%c' "
2164 "unrecognized\n", argv[0], c);
2169 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2170 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2171 optind == argc - 1) {
2173 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2174 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2175 qctl.qc_valid = valid;
2178 qctl.qc_type = USRQUOTA;
2179 qctl.qc_id = geteuid();
2181 qctl.qc_type = GRPQUOTA;
2182 qctl.qc_id = getegid();
2184 rc = id2name(&name, qctl.qc_id,
2185 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2188 /* lfs quota -u username /path/to/lustre/mount */
2189 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2190 /* options should be followed by u/g-name and mntpoint */
2191 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2192 fprintf(stderr, "error: missing quota argument(s)\n");
2196 name = argv[optind++];
2197 rc = name2id(&qctl.qc_id, name,
2198 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2200 fprintf(stderr,"error: can't find id for name %s: %s\n",
2201 name, strerror(errno));
2204 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2205 fprintf(stderr, "error: missing quota info argument(s)\n");
2209 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2210 print_quota_title(name, &qctl);
2214 rc1 = llapi_quotactl(mnt, &qctl);
2215 if (rc1 == -1 && errno == EALREADY) {
2216 fprintf(stderr, "\n%s quotas are not enabled.\n",
2217 qctl.qc_type == USRQUOTA ? "user" : "group");
2220 if (rc1 && *obd_type)
2221 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2223 if (qctl.qc_valid != QC_GENERAL)
2226 print_quota(mnt, &qctl, QC_GENERAL, rc1);
2228 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2229 rc2 = print_obd_quota(mnt, &qctl, 1);
2230 rc3 = print_obd_quota(mnt, &qctl, 0);
2233 if (rc1 || rc2 || rc3)
2234 printf("Some errors happened when getting quota info. "
2235 "Some devices may be not working or deactivated. "
2236 "The data in \"[]\" is inaccurate.\n");
2244 #endif /* HAVE_SYS_QUOTA_H! */
2246 static int flushctx_ioctl(char *mp)
2250 fd = open(mp, O_RDONLY);
2252 fprintf(stderr, "flushctx: error open %s: %s\n",
2253 mp, strerror(errno));
2257 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2259 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2260 mp, strerror(errno));
2266 static int lfs_flushctx(int argc, char **argv)
2268 int kdestroy = 0, c;
2270 char procline[PATH_MAX], *line;
2274 while ((c = getopt(argc, argv, "k")) != -1) {
2280 fprintf(stderr, "error: %s: option '-%c' "
2281 "unrecognized\n", argv[0], c);
2287 system("kdestroy > /dev/null");
2289 if (optind >= argc) {
2290 /* flush for all mounted lustre fs. */
2291 proc = fopen("/proc/mounts", "r");
2293 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2298 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2303 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2304 fprintf(stderr, "%s: unexpected format in "
2310 if (strcmp(fs, "lustre") != 0)
2312 /* we use '@' to determine it's a client. are there
2313 * any other better way?
2315 if (strchr(dev, '@') == NULL)
2318 if (flushctx_ioctl(mp))
2322 /* flush fs as specified */
2323 while (optind < argc) {
2324 if (flushctx_ioctl(argv[optind++]))
2332 static int lfs_lsetfacl(int argc, char **argv)
2335 return(llapi_lsetfacl(argc, argv));
2338 static int lfs_lgetfacl(int argc, char **argv)
2341 return(llapi_lgetfacl(argc, argv));
2344 static int lfs_rsetfacl(int argc, char **argv)
2347 return(llapi_rsetfacl(argc, argv));
2350 static int lfs_rgetfacl(int argc, char **argv)
2353 return(llapi_rgetfacl(argc, argv));
2356 static int lfs_cp(int argc, char **argv)
2358 return(llapi_cp(argc, argv));
2361 static int lfs_ls(int argc, char **argv)
2363 return(llapi_ls(argc, argv));
2366 /* A helper function to return single, whole lines delimited by newline.
2367 Returns length of line. Not reentrant! */
2368 static int get_next_full_line(int fd, char **ptr)
2370 static char buf[8192]; /* bigger than MAX_PATH_LENGTH */
2371 static char *sptr = buf, *eptr = buf;
2372 static int len, rem;
2374 if ((*ptr == NULL) /* first time */
2375 || (eptr >= buf + len) /* buffer empty */) {
2377 len = read(fd, buf, sizeof(buf));
2385 while (eptr < buf + len) {
2387 /* parse full lines */
2388 if (*eptr == '\n') {
2391 return (eptr - sptr);
2395 /* partial line; move to front of buf */
2396 rem = buf + len - sptr;
2397 memcpy(buf, sptr, rem);
2400 len = read(fd, eptr, sizeof(buf) - rem);
2407 static int lfs_changelog(int argc, char **argv)
2409 long long startrec = 0, endrec = 0, recnum;
2411 char c, *mdd, *ptr = NULL;
2412 struct option long_opts[] = {
2413 {"follow", no_argument, 0, 'f'},
2416 char short_opts[] = "f";
2420 while ((c = getopt_long(argc, argv, short_opts,
2421 long_opts, NULL)) != -1) {
2429 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2430 argv[0], argv[optind - 1]);
2437 mdd = argv[optind++];
2439 startrec = strtoll(argv[optind++], NULL, 10);
2441 endrec = strtoll(argv[optind++], NULL, 10);
2443 fd = llapi_changelog_open(mdd, startrec);
2445 fprintf(stderr, "%s Can't open changelog: %s\n", argv[0],
2446 strerror(errno = -fd));
2450 while ((len = get_next_full_line(fd, &ptr)) >= 0) {
2452 struct pollfd pfds[1];
2458 pfds[0].events = POLLIN;
2459 rc = poll(pfds, 1, -1);
2464 /* eg. 2 02MKDIR 4405821890 t=[0x100000400/0x5] p=[0x100000400/0x4] pics */
2465 sscanf(ptr, "%lld *", &recnum);
2466 if (endrec && recnum > endrec)
2468 if (recnum < startrec)
2470 printf("%.*s\n", len, ptr);
2476 fprintf(stderr, "read err %d\n", errno);
2483 static int lfs_changelog_clear(int argc, char **argv)
2491 endrec = strtoll(argv[3], NULL, 10);
2493 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
2495 fprintf(stderr, "%s error: %s\n", argv[0],
2496 strerror(errno = -rc));
2500 static int lfs_fid2path(int argc, char **argv)
2502 struct option long_opts[] = {
2503 {"cur", no_argument, 0, 'c'},
2504 {"link", required_argument, 0, 'l'},
2505 {"rec", required_argument, 0, 'r'},
2508 char c, short_opts[] = "cl:r:";
2509 char *device, *fid, *path;
2510 long long recno = -1;
2517 while ((c = getopt_long(argc, argv, short_opts,
2518 long_opts, NULL)) != -1) {
2524 linkno = strtol(optarg, NULL, 10);
2527 recno = strtoll(optarg, NULL, 10);
2532 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2533 argv[0], argv[optind - 1]);
2538 device = argv[optind++];
2539 fid = argv[optind++];
2543 path = calloc(1, PATH_MAX);
2545 lnktmp = (linkno >= 0) ? linkno : 0;
2547 int oldtmp = lnktmp;
2548 long long rectmp = recno;
2549 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2552 fprintf(stderr, "%s error: %s\n", argv[0],
2553 strerror(errno = -rc));
2558 fprintf(stdout, "%lld %s\n", rectmp, path);
2560 fprintf(stdout, "%s\n", path);
2563 /* specified linkno */
2565 if (oldtmp == lnktmp)
2574 static int lfs_path2fid(int argc, char **argv)
2584 rc = llapi_path2fid(path, &fid);
2586 fprintf(stderr, "can't get fid for %s: %s\n", path,
2587 strerror(errno = -rc));
2591 printf(DFID"\n", PFID(&fid));
2596 int main(int argc, char **argv)
2602 ptl_initialize(argc, argv);
2603 if (obd_initialize(argc, argv) < 0)
2605 if (dbg_initialize(argc, argv) < 0)
2608 Parser_init("lfs > ", cmdlist);
2611 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2613 rc = Parser_commands();
2616 obd_finalize(argc, argv);
2617 return rc < 0 ? -rc : rc;