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 " [--recursive | -r] <dir|file> ..."},
136 {"pool_list", lfs_poollist, 0,
137 "List pools or pool OSTs\n"
138 "usage: pool_list <fsname>[.<poolname>] | <pathname>\n"},
139 {"find", lfs_find, 0,
140 "To find files that match given parameters recursively in a directory tree.\n"
141 "usage: find <dir|file> ... \n"
142 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
143 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
144 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
145 " [[!] --type|-t <filetype>] [[!] --gid|-g N] [[!] --group|-G <name>]\n"
146 " [[!] --uid|-u N] [[!] --user|-U <name>]\n"
147 " [[!] --pool <name>]\n"
148 "\t !: used before an option indicates 'NOT' the requested attribute\n"
149 "\t -: used before an value indicates 'AT MOST' the requested value\n"
150 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
151 {"check", lfs_check, 0,
152 "Display the status of MDS or OSTs (as specified in the command)\n"
153 "or all the servers (MDS and OSTs).\n"
154 "usage: check <osts|mds|servers>"},
155 {"catinfo", lfs_catinfo, 0,
156 "Show information of specified type logs.\n"
157 "usage: catinfo {keyword} [node name]\n"
158 "\tkeywords are one of followings: config, deletions.\n"
159 "\tnode name must be provided when use keyword config."},
160 {"join", lfs_join, 0,
161 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
162 "usage: join <filename_A> <filename_B>\n"},
163 {"osts", lfs_osts, 0, "osts"},
165 "report filesystem disk space usage or inodes usage"
167 "Usage: df [-i] [-h] [path]"},
168 #ifdef HAVE_SYS_QUOTA_H
169 {"quotachown",lfs_quotachown, 0,
170 "Change files' owner or group on the specified filesystem.\n"
171 "usage: quotachown [-i] <filesystem>\n"
172 "\t-i: ignore error if file is not exist\n"},
173 {"quotacheck", lfs_quotacheck, 0,
174 "Scan the specified filesystem for disk usage, and create,\n"
175 "or update quota files.\n"
176 "usage: quotacheck [ -ug ] <filesystem>"},
177 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
178 "usage: quotaon [ -ugf ] <filesystem>"},
179 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
180 "usage: quotaoff [ -ug ] <filesystem>"},
181 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
182 "usage: setquota [ -u | -g ] <name> -b <block-softlimit> -B <block-hardlimit> -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
183 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>\n"
184 " setquota [ -u | --user | -g | --group ] <name>\n"
185 " [--block-softlimit <block-softlimit>]\n"
186 " [--block-hardlimit <block-hardlimit>]\n"
187 " [--inode-softlimit <inode-softlimit>]\n"
188 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
189 " setquota [-t] [ -u | --user | -g | --group ]\n"
190 " [--block-grace <block-grace>]\n"
191 " [--inode-grace <inode-grace>] <filesystem>\n"
192 " -b can be used instead of --block-softlimit/--block-grace\n"
193 " -B can be used instead of --block-hardlimit\n"
194 " -i can be used instead of --inode-softlimit/--inode-grace\n"
195 " -I can be used instead of --inode-hardlimit"},
196 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
197 "usage: quota [-v] [-o obd_uuid|-i mdt_idx|-I ost_idx] [{-u|-g <name>}|-t] <filesystem>"},
198 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
199 "usage: quotainv [-u|-g] <filesystem>"},
201 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
202 "usage: flushctx [-k] [mountpoint...]"},
203 {"lsetfacl", lfs_lsetfacl, 0,
204 "Remote user setfacl for user/group on the same remote client.\n"
205 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
206 {"lgetfacl", lfs_lgetfacl, 0,
207 "Remote user getfacl for user/group on the same remote client.\n"
208 "usage: lgetfacl [-dRLPvh] file ..."},
209 {"rsetfacl", lfs_rsetfacl, 0,
210 "Remote user setfacl for user/group on other clients.\n"
211 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
212 {"rgetfacl", lfs_rgetfacl, 0,
213 "Remote user getfacl for user/group on other clients.\n"
214 "usage: rgetfacl [-dRLPvh] file ..."},
216 "Remote user copy files and directories.\n"
217 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
219 "Remote user list directory contents.\n"
220 "usage: ls [OPTION]... [FILE]..."},
221 {"changelog", lfs_changelog, 0,
222 "Show the metadata changes on an MDT."
223 "\nusage: changelog [--follow] <mdtname> [startrec [endrec]]"
224 "\n(note: --follow is only valid when run on MDT node)"},
225 {"changelog_clear", lfs_changelog_clear, 0,
226 "Indicate that old changelog records up to <endrec> are no longer of "
227 "interest to consumer <id>, allowing the system to free up space.\n"
228 "An <endrec> of 0 means all records.\n"
229 "usage: changelog_clear <mdtname> <id> <endrec>"},
230 {"fid2path", lfs_fid2path, 0,
231 "Resolve the full path to a given FID. For a specific hardlink "
232 "specify link number <linkno>.\n"
233 /* "For a historical name, specify changelog record <recno>.\n" */
234 "usage: fid2path <mdtname> <fid> [--link <linkno>]"/*[--rec <recno>]*/},
235 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
236 "usage: path2fid <path>"},
237 {"help", Parser_help, 0, "help"},
238 {"exit", Parser_quit, 0, "quit"},
239 {"quit", Parser_quit, 0, "quit"},
243 static int isnumber(const char *str)
247 if (str[0] != '-' && !isdigit(str[0]))
250 for (ptr = str + 1; *ptr != '\0'; ptr++) {
259 static int lfs_setstripe(int argc, char **argv)
263 unsigned long long st_size;
264 int st_offset, st_count;
268 char *stripe_size_arg = NULL;
269 char *stripe_off_arg = NULL;
270 char *stripe_count_arg = NULL;
271 char *pool_name_arg = NULL;
272 unsigned long long size_units;
274 struct option long_opts[] = {
275 {"size", required_argument, 0, 's'},
276 {"count", required_argument, 0, 'c'},
277 {"index", required_argument, 0, 'i'},
278 {"offset", required_argument, 0, 'o'},
279 {"pool", required_argument, 0, 'p'},
280 {"delete", no_argument, 0, 'd'},
288 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
289 if (argc == 5 && argv[1][0] != '-' &&
290 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
291 fprintf(stderr, "warning: deprecated usage of setstripe "
292 "positional parameters. Use -c, -i, -s instead.\n");
293 /* for compatibility with the existing positional parameter
296 stripe_size_arg = argv[2];
297 stripe_off_arg = argv[3];
298 stripe_count_arg = argv[4];
302 #warning "remove obsolete positional parameter code"
306 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
307 long_opts, NULL)) >= 0) {
313 stripe_count_arg = optarg;
316 /* delete the default striping pattern */
321 stripe_off_arg = optarg;
324 stripe_size_arg = optarg;
327 pool_name_arg = optarg;
332 fprintf(stderr, "error: %s: option '%s' "
334 argv[0], argv[optind - 1]);
339 fname = argv[optind];
342 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
343 stripe_count_arg != NULL || pool_name_arg != NULL)) {
344 fprintf(stderr, "error: %s: cannot specify -d with "
345 "-s, -c -o or -p options\n",
351 if (optind == argc) {
352 fprintf(stderr, "error: %s: missing filename|dirname\n",
357 /* get the stripe size */
358 if (stripe_size_arg != NULL) {
359 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
361 fprintf(stderr, "error: %s: bad size '%s'\n",
362 argv[0], stripe_size_arg);
366 /* get the stripe offset */
367 if (stripe_off_arg != NULL) {
368 st_offset = strtoul(stripe_off_arg, &end, 0);
370 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
371 argv[0], stripe_off_arg);
375 /* get the stripe count */
376 if (stripe_count_arg != NULL) {
377 st_count = strtoul(stripe_count_arg, &end, 0);
379 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
380 argv[0], stripe_count_arg);
386 result = llapi_file_create_pool(fname, st_size, st_offset,
387 st_count, 0, pool_name_arg);
389 fprintf(stderr,"error: %s: create stripe file '%s' "
390 "failed\n", argv[0], fname);
393 fname = argv[++optind];
394 } while (fname != NULL);
399 static int lfs_poollist(int argc, char **argv)
404 return llapi_poollist(argv[1]);
407 static int set_time(time_t *time, time_t *set, char *str)
414 else if (str[0] == '-')
420 t = strtol(str, NULL, 0);
421 if (*time < t * 24 * 60 * 60) {
424 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
428 *set = *time - t * 24 * 60 * 60;
435 static int name2id(unsigned int *id, char *name, int type)
438 struct passwd *entry;
440 if (!(entry = getpwnam(name))) {
450 if (!(entry = getgrnam(name))) {
462 static int id2name(char **name, unsigned int id, int type)
465 struct passwd *entry;
467 if (!(entry = getpwuid(id))) {
473 *name = entry->pw_name;
477 if (!(entry = getgrgid(id))) {
483 *name = entry->gr_name;
489 #define FIND_POOL_OPT 3
490 static int lfs_find(int argc, char **argv)
495 struct find_param param = { .maxdepth = -1 };
497 struct option long_opts[] = {
498 /* New find options. */
499 {"atime", required_argument, 0, 'A'},
500 {"ctime", required_argument, 0, 'C'},
501 {"mtime", required_argument, 0, 'M'},
502 {"maxdepth", required_argument, 0, 'D'},
503 {"gid", required_argument, 0, 'g'},
504 {"group", required_argument, 0, 'G'},
505 {"uid", required_argument, 0, 'u'},
506 {"user", required_argument, 0, 'U'},
507 {"name", required_argument, 0, 'n'},
508 /* no short option for pool, p/P already used */
509 {"pool", required_argument, 0, FIND_POOL_OPT},
510 /* --obd is considered as a new option. */
511 {"obd", required_argument, 0, 'O'},
512 {"ost", required_argument, 0, 'O'},
513 {"print", no_argument, 0, 'P'},
514 {"print0", no_argument, 0, 'p'},
515 /* Old find options. */
516 {"quiet", no_argument, 0, 'q'},
517 {"recursive", no_argument, 0, 'r'},
518 {"size", required_argument, 0, 's'},
519 {"type", required_argument, 0, 't'},
520 {"verbose", no_argument, 0, 'v'},
534 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
535 while ((c = getopt_long_only(argc,argv,"-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
536 long_opts, NULL)) >= 0) {
541 /* '!' is part of option */
542 /* when getopt_long_only() finds a string which is not
543 * an option nor a known option argument it returns 1
544 * in that case if we already have found pathstart and pathend
545 * (i.e. we have the list of pathnames),
546 * the only supported value is "!"
548 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
549 if (!isoption && pathend != -1) {
550 fprintf(stderr, "err: %s: filename|dirname must either "
551 "precede options or follow options\n",
555 if (!isoption && pathstart == -1)
556 pathstart = optind - 1;
557 if (isoption && pathstart != -1 && pathend == -1) {
558 pathend = optind - 2;
559 if ((c == 1 && strcmp(optarg, "!") == 0) ||
560 c == 'P' || c == 'p' || c == 'O' ||
561 c == 'q' || c == 'r' || c == 'v')
562 pathend = optind - 1;
569 /* unknown; opt is "!" or path component,
570 * checking done above.
572 if (strcmp(optarg, "!") == 0)
576 xtime = ¶m.atime;
577 xsign = ¶m.asign;
580 xtime = ¶m.ctime;
581 xsign = ¶m.csign;
585 xtime = ¶m.mtime;
586 xsign = ¶m.msign;
590 if (optarg[0] == '-')
592 else if (optarg[0] == '+')
601 ret = set_time(&t, xtime, optarg);
609 param.maxdepth = strtol(optarg, 0, 0);
613 param.gid = strtol(optarg, &endptr, 10);
614 if (optarg == endptr) {
615 fprintf(stderr, "Bad gid: %s\n", optarg);
618 param.exclude_gid = !!neg_opt;
623 param.gid = strtol(optarg, &endptr, 10);
624 if (optarg == endptr) {
625 ret = name2id(¶m.gid, optarg, GROUP);
627 fprintf(stderr, "Group/GID: %s cannot "
628 "be found.\n", optarg);
632 param.exclude_gid = !!neg_opt;
637 param.uid = strtol(optarg, &endptr, 10);
638 if (optarg == endptr) {
639 fprintf(stderr, "Bad uid: %s\n", optarg);
642 param.exclude_uid = !!neg_opt;
647 param.uid = strtol(optarg, &endptr, 10);
648 if (optarg == endptr) {
649 ret = name2id(¶m.uid, optarg, USER);
651 fprintf(stderr, "User/UID: %s cannot "
652 "be found.\n", optarg);
656 param.exclude_uid = !!neg_opt;
661 if (strlen(optarg) > LOV_MAXPOOLNAME) {
663 "Pool name %s is too long"
664 " (max is %d)\n", optarg,
668 /* we do check for empty pool because empty pool
669 * is used to find V1 lov attributes */
670 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
671 param.poolname[LOV_MAXPOOLNAME] = '\0';
672 param.exclude_pool = !!neg_opt;
673 param.check_pool = 1;
677 param.pattern = (char *)optarg;
678 param.exclude_pattern = !!neg_opt;
681 char *buf, *token, *next, *p;
684 len = strlen((char *)optarg);
688 strcpy(buf, (char *)optarg);
690 if (param.num_alloc_obds == 0) {
691 param.obduuid = malloc(FIND_MAX_OSTS *
692 sizeof(struct obd_uuid));
693 if (param.obduuid == NULL)
695 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
698 for (token = buf; token && *token; token = next) {
699 p = strchr(token, ',');
705 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
729 param.exclude_type = !!neg_opt;
731 case 'b': param.type = S_IFBLK; break;
732 case 'c': param.type = S_IFCHR; break;
733 case 'd': param.type = S_IFDIR; break;
734 case 'f': param.type = S_IFREG; break;
735 case 'l': param.type = S_IFLNK; break;
736 case 'p': param.type = S_IFIFO; break;
737 case 's': param.type = S_IFSOCK; break;
738 #ifdef S_IFDOOR /* Solaris only */
739 case 'D': param.type = S_IFDOOR; break;
741 default: fprintf(stderr, "error: %s: bad type '%s'\n",
748 if (optarg[0] == '-')
750 else if (optarg[0] == '+')
759 if (optarg[0] == '+')
760 param.size_sign = -1;
761 else if (optarg[0] == '-')
762 param.size_sign = +1;
766 ret = parse_size(optarg, ¶m.size,
767 ¶m.size_units, 0);
769 fprintf(stderr,"error: bad size '%s'\n",
782 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
783 argv[0], argv[optind - 1]);
788 if (pathstart == -1) {
789 fprintf(stderr, "error: %s: no filename|pathname\n",
792 } else if (pathend == -1) {
800 static int deprecated_warning;
801 if (!deprecated_warning) {
802 fprintf(stderr, "lfs find: -q, -r, -v options "
803 "deprecated. Use 'lfs getstripe' instead.\n");
804 deprecated_warning = 1;
806 if (!param.recursive && param.maxdepth == -1)
812 ret = llapi_find(argv[pathstart], ¶m);
814 ret = llapi_getstripe(argv[pathstart], ¶m);
815 } while (++pathstart < pathend && !ret);
818 fprintf(stderr, "error: %s failed for %s.\n",
819 argv[0], argv[optind - 1]);
821 if (param.obduuid && param.num_alloc_obds)
827 static int lfs_getstripe(int argc, char **argv)
829 struct option long_opts[] = {
831 {"quiet", 0, 0, 'q'},
832 {"recursive", 0, 0, 'r'},
833 {"verbose", 0, 0, 'v'},
836 char short_opts[] = "hO:qrv";
838 struct find_param param = { 0 };
841 while ((c = getopt_long(argc, argv, short_opts,
842 long_opts, NULL)) != -1) {
847 "error: %s: only one obduuid allowed",
851 param.obduuid = (struct obd_uuid *)optarg;
867 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
868 argv[0], argv[optind - 1]);
876 param.maxdepth = param.recursive ? -1 : 1;
879 rc = llapi_getstripe(argv[optind], ¶m);
880 } while (++optind < argc && !rc);
883 fprintf(stderr, "error: %s failed for %s.\n",
884 argv[0], argv[optind - 1]);
888 static int lfs_osts(int argc, char **argv)
891 struct mntent *mnt = NULL;
892 struct find_param param;
898 fp = setmntent(MOUNTED, "r");
901 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
905 while (feof(fp) == 0 && ferror(fp) ==0) {
906 memset(¶m, 0, sizeof(param));
907 if (llapi_is_lustre_mnt(mnt)) {
908 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
911 "error: %s: failed on %s\n",
912 argv[0], mnt->mnt_dir);
922 #define COOK(value) \
925 while (value > 1024) { \
939 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
941 char rpath[PATH_MAX] = {'\0'};
943 int rc, len, out_len = 0;
945 if (!realpath(path, rpath)) {
947 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
948 path, strerror(-rc));
954 while (feof(fp) == 0 && ferror(fp) == 0) {
955 if (llapi_is_lustre_mnt(mnt)) {
956 len = strlen(mnt->mnt_dir);
958 !strncmp(rpath, mnt->mnt_dir, len)) {
960 memset(mntdir, 0, dir_len);
961 strncpy(mntdir, mnt->mnt_dir, dir_len);
970 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
974 static int showdf(char *mntdir, struct obd_statfs *stat,
975 char *uuid, int ishow, int cooked,
976 char *type, int index, int rc)
978 long long avail, used, total;
980 char *suffix = "KMGTPEZY";
981 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
982 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
990 avail = stat->os_ffree;
991 used = stat->os_files - stat->os_ffree;
992 total = stat->os_files;
994 int shift = cooked ? 0 : 10;
996 avail = (stat->os_bavail * stat->os_bsize) >> shift;
997 used = ((stat->os_blocks - stat->os_bfree) *
998 stat->os_bsize) >> shift;
999 total = (stat->os_blocks * stat->os_bsize) >> shift;
1003 ratio = (double)used / (double)total;
1009 cook_val = (double)total;
1012 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
1014 sprintf(tbuf, CDF, total);
1016 cook_val = (double)used;
1019 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
1021 sprintf(ubuf, CDF, used);
1023 cook_val = (double)avail;
1026 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
1028 sprintf(abuf, CDF, avail);
1030 sprintf(tbuf, CDF, total);
1031 sprintf(ubuf, CDF, used);
1032 sprintf(abuf, CDF, avail);
1035 sprintf(rbuf, RDF, (int)(ratio * 100));
1036 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1037 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1039 printf("[%s:%d]\n", type, index);
1045 printf(UUF": inactive device\n", uuid);
1048 printf(UUF": %s\n", uuid, strerror(-rc));
1055 static int mntdf(char *mntdir, int ishow, int cooked)
1057 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1058 struct obd_uuid uuid_buf;
1063 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1064 "UUID", "Inodes", "IUsed", "IFree",
1065 "IUse%", "Mounted on");
1067 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1068 "UUID", cooked ? "bytes" : "1K-blocks",
1069 "Used", "Available", "Use%", "Mounted on");
1071 for (index = 0; ; index++) {
1072 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1073 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1074 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1075 &stat_buf, &uuid_buf);
1082 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1083 rc == -ENODATA || rc == 0) {
1084 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1085 ishow, cooked, "MDT", index, rc);
1088 "error: llapi_obd_statfs(%s): %s (%d)\n",
1089 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1093 sum.os_ffree += stat_buf.os_ffree;
1094 sum.os_files += stat_buf.os_files;
1098 for (index = 0; ; index++) {
1099 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1100 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1101 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1102 &stat_buf, &uuid_buf);
1109 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1110 rc == -ENODATA || rc == 0) {
1111 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1112 ishow, cooked, "OST", index, rc);
1115 "error: llapi_obd_statfs failed: %s (%d)\n",
1120 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1121 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1122 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1127 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1132 static int lfs_df(int argc, char **argv)
1136 struct mntent *mnt = NULL;
1137 char *mntdir = NULL;
1138 int ishow = 0, cooked = 0;
1142 while ((c = getopt(argc, argv, "ih")) != -1) {
1155 path = argv[optind];
1157 fp = setmntent(MOUNTED, "r");
1160 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1161 argv[0], MOUNTED, strerror(errno));
1165 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1166 fprintf(stderr, "error: cannot allocate %d bytes\n",
1170 memset(mntdir, 0, PATH_MAX);
1173 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1180 rc = mntdf(mntdir, ishow, cooked);
1184 mnt = getmntent(fp);
1185 while (feof(fp) == 0 && ferror(fp) == 0) {
1186 if (llapi_is_lustre_mnt(mnt)) {
1187 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1192 mnt = getmntent(fp);
1201 static int lfs_check(int argc, char **argv)
1205 struct mntent *mnt = NULL;
1214 obd_types[0] = obd_type1;
1215 obd_types[1] = obd_type2;
1217 if (strcmp(argv[1], "osts") == 0) {
1218 strcpy(obd_types[0], "osc");
1219 } else if (strcmp(argv[1], "mds") == 0) {
1220 strcpy(obd_types[0], "mdc");
1221 } else if (strcmp(argv[1], "servers") == 0) {
1223 strcpy(obd_types[0], "osc");
1224 strcpy(obd_types[1], "mdc");
1226 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1231 fp = setmntent(MOUNTED, "r");
1233 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1236 mnt = getmntent(fp);
1237 while (feof(fp) == 0 && ferror(fp) ==0) {
1238 if (llapi_is_lustre_mnt(mnt))
1240 mnt = getmntent(fp);
1246 fprintf(stderr, "No suitable Lustre mount found\n");
1250 rc = llapi_target_iterate(num_types, obd_types,
1251 mnt->mnt_dir, llapi_ping_target);
1254 fprintf(stderr, "error: %s: %s status failed\n",
1261 static int lfs_catinfo(int argc, char **argv)
1264 struct mntent *mnt = NULL;
1267 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1270 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1273 fp = setmntent(MOUNTED, "r");
1275 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1278 mnt = getmntent(fp);
1279 while (feof(fp) == 0 && ferror(fp) == 0) {
1280 if (llapi_is_lustre_mnt(mnt))
1282 mnt = getmntent(fp);
1289 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1291 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1293 fprintf(stderr, "no lustre_lite mounted.\n");
1300 int lfs_join(int argc, char **argv)
1302 char *name_head, *name_tail;
1308 name_head = argv[1];
1309 fd = open(name_head, O_WRONLY);
1311 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1315 size = lseek(fd, 0, SEEK_END);
1316 if (size % JOIN_FILE_ALIGN) {
1317 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1318 name_head, (long long)size, JOIN_FILE_ALIGN);
1322 name_tail = argv[2];
1323 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1327 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1333 #ifdef HAVE_SYS_QUOTA_H
1334 static int lfs_quotachown(int argc, char **argv)
1341 while ((c = getopt(argc, argv, "i")) != -1) {
1347 fprintf(stderr, "error: %s: option '-%c' "
1348 "unrecognized\n", argv[0], c);
1354 rc = llapi_quotachown(argv[optind], flag);
1356 fprintf(stderr,"error: change file owner/group failed.\n");
1360 static int lfs_quotacheck(int argc, char **argv)
1362 int c, check_type = 0;
1364 struct if_quotacheck qchk;
1365 struct if_quotactl qctl;
1366 char *obd_type = (char *)qchk.obd_type;
1369 memset(&qchk, 0, sizeof(qchk));
1372 while ((c = getopt(argc, argv, "ug")) != -1) {
1381 fprintf(stderr, "error: %s: option '-%c' "
1382 "unrecognized\n", argv[0], c);
1389 else /* do quotacheck for both user & group quota by default */
1397 memset(&qctl, 0, sizeof(qctl));
1398 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1399 qctl.qc_type = check_type;
1400 rc = llapi_quotactl(mnt, &qctl);
1402 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1406 rc = llapi_quotacheck(mnt, check_type);
1408 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1412 rc = llapi_poll_quotacheck(mnt, &qchk);
1415 fprintf(stderr, "%s %s ", obd_type,
1416 obd_uuid2str(&qchk.obd_uuid));
1417 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1421 memset(&qctl, 0, sizeof(qctl));
1422 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1423 qctl.qc_type = check_type;
1424 rc = llapi_quotactl(mnt, &qctl);
1427 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1428 obd_uuid2str(&qctl.obd_uuid));
1429 fprintf(stderr, "%s turn on quota failed: %s\n",
1430 argv[0], strerror(errno));
1437 static int lfs_quotaon(int argc, char **argv)
1441 struct if_quotactl qctl;
1442 char *obd_type = (char *)qctl.obd_type;
1445 memset(&qctl, 0, sizeof(qctl));
1446 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1449 while ((c = getopt(argc, argv, "ugf")) != -1) {
1452 qctl.qc_type |= 0x01;
1455 qctl.qc_type |= 0x02;
1458 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1461 fprintf(stderr, "error: %s: option '-%c' "
1462 "unrecognized\n", argv[0], c);
1469 else /* by default, enable quota for both user & group */
1470 qctl.qc_type = 0x02;
1477 rc = llapi_quotactl(mnt, &qctl);
1480 fprintf(stderr, "%s %s ", obd_type,
1481 obd_uuid2str(&qctl.obd_uuid));
1482 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1489 static int lfs_quotaoff(int argc, char **argv)
1493 struct if_quotactl qctl;
1494 char *obd_type = (char *)qctl.obd_type;
1497 memset(&qctl, 0, sizeof(qctl));
1498 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1501 while ((c = getopt(argc, argv, "ug")) != -1) {
1504 qctl.qc_type |= 0x01;
1507 qctl.qc_type |= 0x02;
1510 fprintf(stderr, "error: %s: option '-%c' "
1511 "unrecognized\n", argv[0], c);
1518 else /* by default, disable quota for both user & group */
1519 qctl.qc_type = 0x02;
1526 rc = llapi_quotactl(mnt, &qctl);
1527 if (rc == -1 && errno == ESRCH) {
1528 fprintf(stderr, "\n%s quotas are not enabled.\n",
1529 qctl.qc_type == 0x00 ? "user" : "group");
1534 fprintf(stderr, "%s %s ", obd_type,
1535 obd_uuid2str(&qctl.obd_uuid));
1536 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1543 static int lfs_quotainv(int argc, char **argv)
1547 struct if_quotactl qctl;
1550 memset(&qctl, 0, sizeof(qctl));
1551 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1554 while ((c = getopt(argc, argv, "ugf")) != -1) {
1557 qctl.qc_type |= 0x01;
1560 qctl.qc_type |= 0x02;
1563 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1566 fprintf(stderr, "error: %s: option '-%c' "
1567 "unrecognized\n", argv[0], c);
1574 else /* by default, invalidate quota for both user & group */
1575 qctl.qc_type = 0x02;
1582 rc = llapi_quotactl(mnt, &qctl);
1584 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1591 #define ARG2INT(nr, str, msg) \
1594 nr = strtol(str, &endp, 0); \
1596 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1601 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1603 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1604 * returns the value or ULONG_MAX on integer overflow or incorrect format
1606 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1607 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1608 * 3. empty integer value is interpreted as 0
1611 static unsigned long str2sec(const char* timestr) {
1612 const char spec[] = "smhdw";
1613 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1614 unsigned long val = 0;
1617 if (strpbrk(timestr, spec) == NULL) {
1618 /* no specifiers inside the time string,
1619 should treat it as an integer value */
1620 val = strtoul(timestr, &tail, 10);
1621 return *tail ? ULONG_MAX : val;
1624 /* format string is XXwXXdXXhXXmXXs */
1630 v = strtoul(timestr, &tail, 10);
1631 if (v == ULONG_MAX || *tail == '\0')
1632 /* value too large (ULONG_MAX or more)
1633 or missing specifier */
1636 ptr = strchr(spec, *tail);
1638 /* unknown specifier */
1643 /* check if product will overflow the type */
1644 if (!(v < ULONG_MAX / mult[ind]))
1647 ADD_OVERFLOW(val, mult[ind] * v);
1648 if (val == ULONG_MAX)
1660 #define ARG2ULL(nr, str, defscale) \
1662 unsigned long long limit, units = 0; \
1665 rc = parse_size(str, &limit, &units, 1); \
1667 fprintf(stderr, "error: bad limit value %s\n", str); \
1670 nr = ((units == 0) ? (defscale) : 1) * limit; \
1673 static inline int has_times_option(int argc, char **argv)
1677 for (i = 1; i < argc; i++)
1678 if (!strcmp(argv[i], "-t"))
1684 int lfs_setquota_times(int argc, char **argv)
1687 struct if_quotactl qctl;
1688 char *mnt, *obd_type = (char *)qctl.obd_type;
1689 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1690 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1691 struct option long_opts[] = {
1692 {"user", no_argument, 0, 'u'},
1693 {"group", no_argument, 0, 'g'},
1694 {"block-grace", required_argument, 0, 'b'},
1695 {"inode-grace", required_argument, 0, 'i'},
1696 {"times", no_argument, 0, 't'},
1700 memset(&qctl, 0, sizeof(qctl));
1701 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1702 qctl.qc_type = UGQUOTA;
1705 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1709 if (qctl.qc_type != UGQUOTA) {
1710 fprintf(stderr, "error: -u and -g can't be used "
1711 "more than once\n");
1714 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1717 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1718 fprintf(stderr, "error: bad block-grace: %s\n",
1722 dqb->dqb_valid |= QIF_BTIME;
1725 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1726 fprintf(stderr, "error: bad inode-grace: %s\n",
1730 dqb->dqb_valid |= QIF_ITIME;
1732 case 't': /* Yes, of course! */
1734 default: /* getopt prints error message for us when opterr != 0 */
1739 if (qctl.qc_type == UGQUOTA) {
1740 fprintf(stderr, "error: neither -u nor -g specified\n");
1744 if (optind != argc - 1) {
1745 fprintf(stderr, "error: unexpected parameters encountered\n");
1750 rc = llapi_quotactl(mnt, &qctl);
1753 fprintf(stderr, "%s %s ", obd_type,
1754 obd_uuid2str(&qctl.obd_uuid));
1755 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1762 #define BSLIMIT (1 << 0)
1763 #define BHLIMIT (1 << 1)
1764 #define ISLIMIT (1 << 2)
1765 #define IHLIMIT (1 << 3)
1767 int lfs_setquota(int argc, char **argv)
1770 struct if_quotactl qctl;
1771 char *mnt, *obd_type = (char *)qctl.obd_type;
1772 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1773 struct option long_opts[] = {
1774 {"user", required_argument, 0, 'u'},
1775 {"group", required_argument, 0, 'g'},
1776 {"block-softlimit", required_argument, 0, 'b'},
1777 {"block-hardlimit", required_argument, 0, 'B'},
1778 {"inode-softlimit", required_argument, 0, 'i'},
1779 {"inode-hardlimit", required_argument, 0, 'I'},
1782 unsigned limit_mask = 0;
1784 if (has_times_option(argc, argv))
1785 return lfs_setquota_times(argc, argv);
1787 memset(&qctl, 0, sizeof(qctl));
1788 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1789 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1790 * so it can be used as a marker that qc_type
1791 * isn't reinitialized from command line */
1794 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1798 if (qctl.qc_type != UGQUOTA) {
1799 fprintf(stderr, "error: -u and -g can't be used"
1800 " more than once\n");
1803 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1804 rc = name2id(&qctl.qc_id, optarg,
1805 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1807 fprintf(stderr, "error: unknown id %s\n",
1813 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1814 dqb->dqb_bsoftlimit >>= 10;
1815 limit_mask |= BSLIMIT;
1818 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1819 dqb->dqb_bhardlimit >>= 10;
1820 limit_mask |= BHLIMIT;
1823 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1824 limit_mask |= ISLIMIT;
1827 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1828 limit_mask |= IHLIMIT;
1830 default: /* getopt prints error message for us when opterr != 0 */
1835 if (qctl.qc_type == UGQUOTA) {
1836 fprintf(stderr, "error: neither -u nor -g are specified\n");
1840 if (optind != argc - 1) {
1841 fprintf(stderr, "error: unexpected parameters encountered\n");
1847 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1848 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1849 /* sigh, we can't just set blimits/ilimits */
1850 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1851 .qc_type = qctl.qc_type,
1852 .qc_id = qctl.qc_id};
1854 rc = llapi_quotactl(mnt, &tmp_qctl);
1856 fprintf(stderr, "error: getquota failed\n");
1860 if (!(limit_mask & BHLIMIT))
1861 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1862 if (!(limit_mask & BSLIMIT))
1863 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1864 if (!(limit_mask & IHLIMIT))
1865 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1866 if (!(limit_mask & ISLIMIT))
1867 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1870 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1871 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1873 rc = llapi_quotactl(mnt, &qctl);
1876 fprintf(stderr, "%s %s ", obd_type,
1877 obd_uuid2str(&qctl.obd_uuid));
1878 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1885 static inline char *type2name(int check_type)
1887 if (check_type == USRQUOTA)
1889 else if (check_type == GRPQUOTA)
1896 /* Converts seconds value into format string
1897 * result is returned in buf
1899 * 1. result is in descenting order: 1w2d3h4m5s
1900 * 2. zero fields are not filled (except for p. 3): 5d1s
1901 * 3. zero seconds value is presented as "0s"
1903 static void sec2str(time_t seconds, char *buf)
1905 const char spec[] = "smhdw";
1906 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1911 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1912 c = seconds / mult[i];
1914 if (c > 0 || (i == 0 && buf == tail))
1915 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1922 static void diff2str(time_t seconds, char *buf, time_t now)
1928 if (seconds <= now) {
1929 strcpy(buf, "none");
1932 sec2str(seconds - now, buf);
1935 static void print_quota_title(char *name, struct if_quotactl *qctl)
1937 printf("Disk quotas for %s %s (%cid %u):\n",
1938 type2name(qctl->qc_type), name,
1939 *type2name(qctl->qc_type), qctl->qc_id);
1940 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1942 "kbytes", "quota", "limit", "grace",
1943 "files", "quota", "limit", "grace");
1946 static void print_quota(char *mnt, struct if_quotactl *qctl, int type)
1952 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1953 int bover = 0, iover = 0;
1954 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1956 if (dqb->dqb_bhardlimit &&
1957 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1959 } else if (dqb->dqb_bsoftlimit &&
1960 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1961 if (dqb->dqb_btime > now) {
1968 if (dqb->dqb_ihardlimit &&
1969 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1971 } else if (dqb->dqb_isoftlimit &&
1972 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1973 if (dqb->dqb_btime > now) {
1980 #if 0 /* XXX: always print quotas even when no usages */
1981 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1987 if (strlen(mnt) > 15)
1988 printf("%s\n%15s", mnt, "");
1990 printf("%15s", mnt);
1993 diff2str(dqb->dqb_btime, timebuf, now);
1994 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
1995 LPU64 : "["LPU64"]", toqb(dqb->dqb_curspace));
1996 if (type == QC_GENERAL)
1997 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
1998 ? LPU64 : "["LPU64"]",
1999 dqb->dqb_bsoftlimit);
2001 sprintf(numbuf[1], "%s", "");
2002 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2003 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2004 printf(" %7s%c %6s %7s %7s",
2005 numbuf[0], bover ? '*' : ' ', numbuf[1],
2006 numbuf[2], bover > 1 ? timebuf : "");
2009 diff2str(dqb->dqb_itime, timebuf, now);
2011 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2012 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2013 if (type == QC_GENERAL)
2014 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2015 ? LPU64 : "["LPU64"]",
2016 dqb->dqb_isoftlimit);
2018 sprintf(numbuf[1], "%s", "");
2019 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2020 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2021 if (type != QC_OSTIDX)
2022 printf(" %7s%c %6s %7s %7s",
2023 numbuf[0], iover ? '*' : ' ', numbuf[1],
2024 numbuf[2], iover > 1 ? timebuf : "");
2027 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2028 qctl->qc_cmd == Q_GETOINFO) {
2032 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2033 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2034 printf("Block grace time: %s; Inode grace time: %s\n",
2035 bgtimebuf, igtimebuf);
2039 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2041 int rc = 0, rc1 = 0, count = 0;
2042 __u32 valid = qctl->qc_valid;
2044 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2046 fprintf(stderr, "can not get %s count: %s\n",
2047 is_mdt ? "mdt": "ost", strerror(errno));
2051 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2052 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2053 rc = llapi_quotactl(mnt, qctl);
2055 /* It is remote client case. */
2056 if (errno == EOPNOTSUPP) {
2063 fprintf(stderr, "quotactl %s%d failed.\n",
2064 is_mdt ? "mdt": "ost", qctl->qc_idx);
2068 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid);
2072 qctl->qc_valid = valid;
2076 static int lfs_quota(int argc, char **argv)
2079 char *mnt, *name = NULL;
2080 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2081 .qc_type = UGQUOTA };
2082 char *obd_type = (char *)qctl.obd_type;
2083 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2084 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2085 __u32 valid = QC_GENERAL, idx = 0;
2088 while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2091 if (qctl.qc_type != UGQUOTA) {
2092 fprintf(stderr, "error: use either -u or -g\n");
2095 qctl.qc_type = USRQUOTA;
2098 if (qctl.qc_type != UGQUOTA) {
2099 fprintf(stderr, "error: use either -u or -g\n");
2102 qctl.qc_type = GRPQUOTA;
2105 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2108 valid = qctl.qc_valid = QC_UUID;
2109 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2112 valid = qctl.qc_valid = QC_MDTIDX;
2113 idx = qctl.qc_idx = atoi(optarg);
2116 valid = qctl.qc_valid = QC_OSTIDX;
2117 idx = qctl.qc_idx = atoi(optarg);
2123 fprintf(stderr, "error: %s: option '-%c' "
2124 "unrecognized\n", argv[0], c);
2129 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2130 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2131 optind == argc - 1) {
2133 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2134 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2135 qctl.qc_valid = valid;
2138 qctl.qc_type = USRQUOTA;
2139 qctl.qc_id = geteuid();
2141 qctl.qc_type = GRPQUOTA;
2142 qctl.qc_id = getegid();
2144 rc = id2name(&name, qctl.qc_id,
2145 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2148 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2149 if (optind + 2 != argc) {
2150 fprintf(stderr, "error: missing quota argument(s)\n");
2154 name = argv[optind++];
2155 rc = name2id(&qctl.qc_id, name,
2156 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2158 fprintf(stderr,"error: can't find id for name %s: %s\n",
2159 name, strerror(errno));
2162 } else if (optind + 1 != argc) {
2163 fprintf(stderr, "error: missing quota info argument(s)\n");
2167 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2168 print_quota_title(name, &qctl);
2172 rc1 = llapi_quotactl(mnt, &qctl);
2173 if (rc1 == -1 && errno == ESRCH) {
2174 fprintf(stderr, "\n%s quotas are not enabled.\n",
2175 qctl.qc_type == USRQUOTA ? "user" : "group");
2178 if (rc1 && *obd_type)
2179 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2181 if (qctl.qc_valid != QC_GENERAL)
2184 print_quota(mnt, &qctl, QC_GENERAL);
2186 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2187 rc2 = print_obd_quota(mnt, &qctl, 1);
2188 rc3 = print_obd_quota(mnt, &qctl, 0);
2191 if (rc1 || rc2 || rc3)
2192 printf("Some errors happened when getting quota info. "
2193 "Some devices may be not working or deactivated. "
2194 "The data in \"[]\" is inaccurate.\n");
2202 #endif /* HAVE_SYS_QUOTA_H! */
2204 static int flushctx_ioctl(char *mp)
2208 fd = open(mp, O_RDONLY);
2210 fprintf(stderr, "flushctx: error open %s: %s\n",
2211 mp, strerror(errno));
2215 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2217 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2218 mp, strerror(errno));
2224 static int lfs_flushctx(int argc, char **argv)
2226 int kdestroy = 0, c;
2228 char procline[PATH_MAX], *line;
2232 while ((c = getopt(argc, argv, "k")) != -1) {
2238 fprintf(stderr, "error: %s: option '-%c' "
2239 "unrecognized\n", argv[0], c);
2245 system("kdestroy > /dev/null");
2247 if (optind >= argc) {
2248 /* flush for all mounted lustre fs. */
2249 proc = fopen("/proc/mounts", "r");
2251 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2256 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2261 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2262 fprintf(stderr, "%s: unexpected format in "
2268 if (strcmp(fs, "lustre") != 0)
2270 /* we use '@' to determine it's a client. are there
2271 * any other better way?
2273 if (strchr(dev, '@') == NULL)
2276 if (flushctx_ioctl(mp))
2280 /* flush fs as specified */
2281 while (optind < argc) {
2282 if (flushctx_ioctl(argv[optind++]))
2290 static int lfs_lsetfacl(int argc, char **argv)
2293 return(llapi_lsetfacl(argc, argv));
2296 static int lfs_lgetfacl(int argc, char **argv)
2299 return(llapi_lgetfacl(argc, argv));
2302 static int lfs_rsetfacl(int argc, char **argv)
2305 return(llapi_rsetfacl(argc, argv));
2308 static int lfs_rgetfacl(int argc, char **argv)
2311 return(llapi_rgetfacl(argc, argv));
2314 static int lfs_cp(int argc, char **argv)
2316 return(llapi_cp(argc, argv));
2319 static int lfs_ls(int argc, char **argv)
2321 return(llapi_ls(argc, argv));
2324 /* A helper function to return single, whole lines delimited by newline.
2325 Returns length of line. Not reentrant! */
2326 static int get_next_full_line(int fd, char **ptr)
2328 static char buf[8192]; /* bigger than MAX_PATH_LENGTH */
2329 static char *sptr = buf, *eptr = buf;
2330 static int len, rem;
2332 if ((*ptr == NULL) /* first time */
2333 || (eptr >= buf + len) /* buffer empty */) {
2335 len = read(fd, buf, sizeof(buf));
2343 while (eptr < buf + len) {
2345 /* parse full lines */
2346 if (*eptr == '\n') {
2349 return (eptr - sptr);
2353 /* partial line; move to front of buf */
2354 rem = buf + len - sptr;
2355 memcpy(buf, sptr, rem);
2358 len = read(fd, eptr, sizeof(buf) - rem);
2365 static int lfs_changelog(int argc, char **argv)
2367 long long startrec = 0, endrec = 0, recnum;
2369 char c, *mdd, *ptr = NULL;
2370 struct option long_opts[] = {
2371 {"follow", no_argument, 0, 'f'},
2374 char short_opts[] = "f";
2378 while ((c = getopt_long(argc, argv, short_opts,
2379 long_opts, NULL)) != -1) {
2387 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2388 argv[0], argv[optind - 1]);
2395 mdd = argv[optind++];
2397 startrec = strtoll(argv[optind++], NULL, 10);
2399 endrec = strtoll(argv[optind++], NULL, 10);
2401 fd = llapi_changelog_open(mdd, startrec);
2405 while ((len = get_next_full_line(fd, &ptr)) >= 0) {
2407 struct pollfd pfds[1];
2413 pfds[0].events = POLLIN;
2414 rc = poll(pfds, 1, -1);
2419 /* eg. 2 02MKDIR 4405821890 t=[0x100000400/0x5] p=[0x100000400/0x4] pics */
2420 sscanf(ptr, "%lld *", &recnum);
2421 if (endrec && recnum > endrec)
2423 if (recnum < startrec)
2425 printf("%.*s\n", len, ptr);
2431 fprintf(stderr, "read err %d\n", errno);
2438 static int lfs_changelog_clear(int argc, char **argv)
2445 endrec = strtoll(argv[3], NULL, 10);
2447 return(llapi_changelog_clear(argv[1], argv[2], endrec));
2450 static int lfs_fid2path(int argc, char **argv)
2452 struct option long_opts[] = {
2453 {"cur", no_argument, 0, 'c'},
2454 {"link", required_argument, 0, 'l'},
2455 {"rec", required_argument, 0, 'r'},
2458 char c, short_opts[] = "cl:r:";
2459 char *device, *fid, *path;
2460 long long recno = -1;
2467 while ((c = getopt_long(argc, argv, short_opts,
2468 long_opts, NULL)) != -1) {
2474 linkno = strtol(optarg, NULL, 10);
2477 recno = strtoll(optarg, NULL, 10);
2482 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2483 argv[0], argv[optind - 1]);
2488 device = argv[optind++];
2489 fid = argv[optind++];
2493 path = calloc(1, PATH_MAX);
2495 lnktmp = (linkno >= 0) ? linkno : 0;
2497 int oldtmp = lnktmp;
2498 long long rectmp = recno;
2499 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2502 fprintf(stderr, "%s error: %s\n", argv[0],
2503 strerror(errno = -rc));
2508 fprintf(stdout, "%lld %s\n", recno, path);
2510 fprintf(stdout, "%s\n", path);
2513 /* specified linkno */
2515 if (oldtmp == lnktmp)
2524 static int lfs_path2fid(int argc, char **argv)
2527 unsigned long long seq;
2528 unsigned long oid, ver;
2535 rc = llapi_path2fid(path, &seq, &oid, &ver);
2537 fprintf(stderr, "can't get fid for %s: %s\n", path,
2538 strerror(errno = -rc));
2542 printf(DFID"\n", seq, (unsigned int)oid, (unsigned int)ver);
2547 int main(int argc, char **argv)
2553 ptl_initialize(argc, argv);
2554 if (obd_initialize(argc, argv) < 0)
2556 if (dbg_initialize(argc, argv) < 0)
2559 Parser_init("lfs > ", cmdlist);
2562 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2564 rc = Parser_commands();
2567 obd_finalize(argc, argv);