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 <fsname|rootpath> <fid> [--link <linkno>]"
235 /*[--rec <recno>]*/},
236 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
237 "usage: path2fid <path>"},
238 {"help", Parser_help, 0, "help"},
239 {"exit", Parser_quit, 0, "quit"},
240 {"quit", Parser_quit, 0, "quit"},
244 static int isnumber(const char *str)
248 if (str[0] != '-' && !isdigit(str[0]))
251 for (ptr = str + 1; *ptr != '\0'; ptr++) {
260 static int lfs_setstripe(int argc, char **argv)
264 unsigned long long st_size;
265 int st_offset, st_count;
269 char *stripe_size_arg = NULL;
270 char *stripe_off_arg = NULL;
271 char *stripe_count_arg = NULL;
272 char *pool_name_arg = NULL;
273 unsigned long long size_units;
275 struct option long_opts[] = {
276 {"size", required_argument, 0, 's'},
277 {"count", required_argument, 0, 'c'},
278 {"index", required_argument, 0, 'i'},
279 {"offset", required_argument, 0, 'o'},
280 {"pool", required_argument, 0, 'p'},
281 {"delete", no_argument, 0, 'd'},
289 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
290 if (argc == 5 && argv[1][0] != '-' &&
291 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
292 fprintf(stderr, "warning: deprecated usage of setstripe "
293 "positional parameters. Use -c, -i, -s instead.\n");
294 /* for compatibility with the existing positional parameter
297 stripe_size_arg = argv[2];
298 stripe_off_arg = argv[3];
299 stripe_count_arg = argv[4];
303 #warning "remove obsolete positional parameter code"
307 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
308 long_opts, NULL)) >= 0) {
314 stripe_count_arg = optarg;
317 /* delete the default striping pattern */
322 stripe_off_arg = optarg;
325 stripe_size_arg = optarg;
328 pool_name_arg = optarg;
333 fprintf(stderr, "error: %s: option '%s' "
335 argv[0], argv[optind - 1]);
340 fname = argv[optind];
343 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
344 stripe_count_arg != NULL || pool_name_arg != NULL)) {
345 fprintf(stderr, "error: %s: cannot specify -d with "
346 "-s, -c -o or -p options\n",
352 if (optind == argc) {
353 fprintf(stderr, "error: %s: missing filename|dirname\n",
358 /* get the stripe size */
359 if (stripe_size_arg != NULL) {
360 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
362 fprintf(stderr, "error: %s: bad size '%s'\n",
363 argv[0], stripe_size_arg);
367 /* get the stripe offset */
368 if (stripe_off_arg != NULL) {
369 st_offset = strtoul(stripe_off_arg, &end, 0);
371 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
372 argv[0], stripe_off_arg);
376 /* get the stripe count */
377 if (stripe_count_arg != NULL) {
378 st_count = strtoul(stripe_count_arg, &end, 0);
380 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
381 argv[0], stripe_count_arg);
387 result = llapi_file_create_pool(fname, st_size, st_offset,
388 st_count, 0, pool_name_arg);
390 fprintf(stderr,"error: %s: create stripe file '%s' "
391 "failed\n", argv[0], fname);
394 fname = argv[++optind];
395 } while (fname != NULL);
400 static int lfs_poollist(int argc, char **argv)
405 return llapi_poollist(argv[1]);
408 static int set_time(time_t *time, time_t *set, char *str)
415 else if (str[0] == '-')
421 t = strtol(str, NULL, 0);
422 if (*time < t * 24 * 60 * 60) {
425 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
429 *set = *time - t * 24 * 60 * 60;
436 static int name2id(unsigned int *id, char *name, int type)
439 struct passwd *entry;
441 if (!(entry = getpwnam(name))) {
451 if (!(entry = getgrnam(name))) {
463 static int id2name(char **name, unsigned int id, int type)
466 struct passwd *entry;
468 if (!(entry = getpwuid(id))) {
474 *name = entry->pw_name;
478 if (!(entry = getgrgid(id))) {
484 *name = entry->gr_name;
490 #define FIND_POOL_OPT 3
491 static int lfs_find(int argc, char **argv)
496 struct find_param param = { .maxdepth = -1 };
498 struct option long_opts[] = {
499 /* New find options. */
500 {"atime", required_argument, 0, 'A'},
501 {"ctime", required_argument, 0, 'C'},
502 {"mtime", required_argument, 0, 'M'},
503 {"maxdepth", required_argument, 0, 'D'},
504 {"gid", required_argument, 0, 'g'},
505 {"group", required_argument, 0, 'G'},
506 {"uid", required_argument, 0, 'u'},
507 {"user", required_argument, 0, 'U'},
508 {"name", required_argument, 0, 'n'},
509 /* no short option for pool, p/P already used */
510 {"pool", required_argument, 0, FIND_POOL_OPT},
511 /* --obd is considered as a new option. */
512 {"obd", required_argument, 0, 'O'},
513 {"ost", required_argument, 0, 'O'},
514 {"print", no_argument, 0, 'P'},
515 {"print0", no_argument, 0, 'p'},
516 /* Old find options. */
517 {"quiet", no_argument, 0, 'q'},
518 {"recursive", no_argument, 0, 'r'},
519 {"size", required_argument, 0, 's'},
520 {"type", required_argument, 0, 't'},
521 {"verbose", no_argument, 0, 'v'},
535 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
536 while ((c = getopt_long_only(argc,argv,"-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
537 long_opts, NULL)) >= 0) {
542 /* '!' is part of option */
543 /* when getopt_long_only() finds a string which is not
544 * an option nor a known option argument it returns 1
545 * in that case if we already have found pathstart and pathend
546 * (i.e. we have the list of pathnames),
547 * the only supported value is "!"
549 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
550 if (!isoption && pathend != -1) {
551 fprintf(stderr, "err: %s: filename|dirname must either "
552 "precede options or follow options\n",
556 if (!isoption && pathstart == -1)
557 pathstart = optind - 1;
558 if (isoption && pathstart != -1 && pathend == -1) {
559 pathend = optind - 2;
560 if ((c == 1 && strcmp(optarg, "!") == 0) ||
561 c == 'P' || c == 'p' || c == 'O' ||
562 c == 'q' || c == 'r' || c == 'v')
563 pathend = optind - 1;
570 /* unknown; opt is "!" or path component,
571 * checking done above.
573 if (strcmp(optarg, "!") == 0)
577 xtime = ¶m.atime;
578 xsign = ¶m.asign;
581 xtime = ¶m.ctime;
582 xsign = ¶m.csign;
586 xtime = ¶m.mtime;
587 xsign = ¶m.msign;
591 if (optarg[0] == '-')
593 else if (optarg[0] == '+')
602 ret = set_time(&t, xtime, optarg);
610 param.maxdepth = strtol(optarg, 0, 0);
614 param.gid = strtol(optarg, &endptr, 10);
615 if (optarg == endptr) {
616 fprintf(stderr, "Bad gid: %s\n", optarg);
619 param.exclude_gid = !!neg_opt;
624 param.gid = strtol(optarg, &endptr, 10);
625 if (optarg == endptr) {
626 ret = name2id(¶m.gid, optarg, GROUP);
628 fprintf(stderr, "Group/GID: %s cannot "
629 "be found.\n", optarg);
633 param.exclude_gid = !!neg_opt;
638 param.uid = strtol(optarg, &endptr, 10);
639 if (optarg == endptr) {
640 fprintf(stderr, "Bad uid: %s\n", optarg);
643 param.exclude_uid = !!neg_opt;
648 param.uid = strtol(optarg, &endptr, 10);
649 if (optarg == endptr) {
650 ret = name2id(¶m.uid, optarg, USER);
652 fprintf(stderr, "User/UID: %s cannot "
653 "be found.\n", optarg);
657 param.exclude_uid = !!neg_opt;
662 if (strlen(optarg) > LOV_MAXPOOLNAME) {
664 "Pool name %s is too long"
665 " (max is %d)\n", optarg,
669 /* we do check for empty pool because empty pool
670 * is used to find V1 lov attributes */
671 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
672 param.poolname[LOV_MAXPOOLNAME] = '\0';
673 param.exclude_pool = !!neg_opt;
674 param.check_pool = 1;
678 param.pattern = (char *)optarg;
679 param.exclude_pattern = !!neg_opt;
682 char *buf, *token, *next, *p;
685 len = strlen((char *)optarg);
689 strcpy(buf, (char *)optarg);
691 if (param.num_alloc_obds == 0) {
692 param.obduuid = malloc(FIND_MAX_OSTS *
693 sizeof(struct obd_uuid));
694 if (param.obduuid == NULL)
696 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
699 for (token = buf; token && *token; token = next) {
700 p = strchr(token, ',');
706 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
730 param.exclude_type = !!neg_opt;
732 case 'b': param.type = S_IFBLK; break;
733 case 'c': param.type = S_IFCHR; break;
734 case 'd': param.type = S_IFDIR; break;
735 case 'f': param.type = S_IFREG; break;
736 case 'l': param.type = S_IFLNK; break;
737 case 'p': param.type = S_IFIFO; break;
738 case 's': param.type = S_IFSOCK; break;
739 #ifdef S_IFDOOR /* Solaris only */
740 case 'D': param.type = S_IFDOOR; break;
742 default: fprintf(stderr, "error: %s: bad type '%s'\n",
749 if (optarg[0] == '-')
751 else if (optarg[0] == '+')
760 if (optarg[0] == '+')
761 param.size_sign = -1;
762 else if (optarg[0] == '-')
763 param.size_sign = +1;
767 ret = parse_size(optarg, ¶m.size,
768 ¶m.size_units, 0);
770 fprintf(stderr,"error: bad size '%s'\n",
783 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
784 argv[0], argv[optind - 1]);
789 if (pathstart == -1) {
790 fprintf(stderr, "error: %s: no filename|pathname\n",
793 } else if (pathend == -1) {
801 static int deprecated_warning;
802 if (!deprecated_warning) {
803 fprintf(stderr, "lfs find: -q, -r, -v options "
804 "deprecated. Use 'lfs getstripe' instead.\n");
805 deprecated_warning = 1;
807 if (!param.recursive && param.maxdepth == -1)
813 ret = llapi_find(argv[pathstart], ¶m);
815 ret = llapi_getstripe(argv[pathstart], ¶m);
816 } while (++pathstart < pathend && !ret);
819 fprintf(stderr, "error: %s failed for %s.\n",
820 argv[0], argv[optind - 1]);
822 if (param.obduuid && param.num_alloc_obds)
828 static int lfs_getstripe(int argc, char **argv)
830 struct option long_opts[] = {
832 {"quiet", 0, 0, 'q'},
833 {"recursive", 0, 0, 'r'},
834 {"verbose", 0, 0, 'v'},
837 char short_opts[] = "hO:qrv";
839 struct find_param param = { 0 };
842 while ((c = getopt_long(argc, argv, short_opts,
843 long_opts, NULL)) != -1) {
848 "error: %s: only one obduuid allowed",
852 param.obduuid = (struct obd_uuid *)optarg;
868 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
869 argv[0], argv[optind - 1]);
877 param.maxdepth = param.recursive ? -1 : 1;
880 rc = llapi_getstripe(argv[optind], ¶m);
881 } while (++optind < argc && !rc);
884 fprintf(stderr, "error: %s failed for %s.\n",
885 argv[0], argv[optind - 1]);
889 static int lfs_osts(int argc, char **argv)
892 struct mntent *mnt = NULL;
893 struct find_param param;
899 fp = setmntent(MOUNTED, "r");
902 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
906 while (feof(fp) == 0 && ferror(fp) ==0) {
907 memset(¶m, 0, sizeof(param));
908 if (llapi_is_lustre_mnt(mnt)) {
909 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
912 "error: %s: failed on %s\n",
913 argv[0], mnt->mnt_dir);
923 #define COOK(value) \
926 while (value > 1024) { \
940 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
942 char rpath[PATH_MAX] = {'\0'};
944 int rc, len, out_len = 0;
946 if (!realpath(path, rpath)) {
948 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
949 path, strerror(-rc));
955 while (feof(fp) == 0 && ferror(fp) == 0) {
956 if (llapi_is_lustre_mnt(mnt)) {
957 len = strlen(mnt->mnt_dir);
959 !strncmp(rpath, mnt->mnt_dir, len)) {
961 memset(mntdir, 0, dir_len);
962 strncpy(mntdir, mnt->mnt_dir, dir_len);
971 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
975 static int showdf(char *mntdir, struct obd_statfs *stat,
976 char *uuid, int ishow, int cooked,
977 char *type, int index, int rc)
979 long long avail, used, total;
981 char *suffix = "KMGTPEZY";
982 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
983 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
991 avail = stat->os_ffree;
992 used = stat->os_files - stat->os_ffree;
993 total = stat->os_files;
995 int shift = cooked ? 0 : 10;
997 avail = (stat->os_bavail * stat->os_bsize) >> shift;
998 used = ((stat->os_blocks - stat->os_bfree) *
999 stat->os_bsize) >> shift;
1000 total = (stat->os_blocks * stat->os_bsize) >> shift;
1004 ratio = (double)used / (double)total;
1010 cook_val = (double)total;
1013 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
1015 sprintf(tbuf, CDF, total);
1017 cook_val = (double)used;
1020 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
1022 sprintf(ubuf, CDF, used);
1024 cook_val = (double)avail;
1027 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
1029 sprintf(abuf, CDF, avail);
1031 sprintf(tbuf, CDF, total);
1032 sprintf(ubuf, CDF, used);
1033 sprintf(abuf, CDF, avail);
1036 sprintf(rbuf, RDF, (int)(ratio * 100));
1037 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1038 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1040 printf("[%s:%d]\n", type, index);
1046 printf(UUF": inactive device\n", uuid);
1049 printf(UUF": %s\n", uuid, strerror(-rc));
1056 static int mntdf(char *mntdir, int ishow, int cooked)
1058 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1059 struct obd_uuid uuid_buf;
1064 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1065 "UUID", "Inodes", "IUsed", "IFree",
1066 "IUse%", "Mounted on");
1068 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1069 "UUID", cooked ? "bytes" : "1K-blocks",
1070 "Used", "Available", "Use%", "Mounted on");
1072 for (index = 0; ; index++) {
1073 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1074 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1075 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1076 &stat_buf, &uuid_buf);
1083 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1084 rc == -ENODATA || rc == 0) {
1085 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1086 ishow, cooked, "MDT", index, rc);
1089 "error: llapi_obd_statfs(%s): %s (%d)\n",
1090 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1094 sum.os_ffree += stat_buf.os_ffree;
1095 sum.os_files += stat_buf.os_files;
1099 for (index = 0; ; index++) {
1100 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1101 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1102 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1103 &stat_buf, &uuid_buf);
1110 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1111 rc == -ENODATA || rc == 0) {
1112 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1113 ishow, cooked, "OST", index, rc);
1116 "error: llapi_obd_statfs failed: %s (%d)\n",
1121 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1122 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1123 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1128 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1133 static int lfs_df(int argc, char **argv)
1137 struct mntent *mnt = NULL;
1138 char *mntdir = NULL;
1139 int ishow = 0, cooked = 0;
1143 while ((c = getopt(argc, argv, "ih")) != -1) {
1156 path = argv[optind];
1158 fp = setmntent(MOUNTED, "r");
1161 fprintf(stderr, "error: %s: open %s failed( %s )\n",
1162 argv[0], MOUNTED, strerror(errno));
1166 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1167 fprintf(stderr, "error: cannot allocate %d bytes\n",
1171 memset(mntdir, 0, PATH_MAX);
1174 rc = path2mnt(path, fp, mntdir, PATH_MAX);
1181 rc = mntdf(mntdir, ishow, cooked);
1185 mnt = getmntent(fp);
1186 while (feof(fp) == 0 && ferror(fp) == 0) {
1187 if (llapi_is_lustre_mnt(mnt)) {
1188 rc = mntdf(mnt->mnt_dir, ishow, cooked);
1193 mnt = getmntent(fp);
1202 static int lfs_check(int argc, char **argv)
1206 struct mntent *mnt = NULL;
1215 obd_types[0] = obd_type1;
1216 obd_types[1] = obd_type2;
1218 if (strcmp(argv[1], "osts") == 0) {
1219 strcpy(obd_types[0], "osc");
1220 } else if (strcmp(argv[1], "mds") == 0) {
1221 strcpy(obd_types[0], "mdc");
1222 } else if (strcmp(argv[1], "servers") == 0) {
1224 strcpy(obd_types[0], "osc");
1225 strcpy(obd_types[1], "mdc");
1227 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1232 fp = setmntent(MOUNTED, "r");
1234 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1237 mnt = getmntent(fp);
1238 while (feof(fp) == 0 && ferror(fp) ==0) {
1239 if (llapi_is_lustre_mnt(mnt))
1241 mnt = getmntent(fp);
1247 fprintf(stderr, "No suitable Lustre mount found\n");
1251 rc = llapi_target_iterate(num_types, obd_types,
1252 mnt->mnt_dir, llapi_ping_target);
1255 fprintf(stderr, "error: %s: %s status failed\n",
1262 static int lfs_catinfo(int argc, char **argv)
1265 struct mntent *mnt = NULL;
1268 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1271 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1274 fp = setmntent(MOUNTED, "r");
1276 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1279 mnt = getmntent(fp);
1280 while (feof(fp) == 0 && ferror(fp) == 0) {
1281 if (llapi_is_lustre_mnt(mnt))
1283 mnt = getmntent(fp);
1290 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1292 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1294 fprintf(stderr, "no lustre_lite mounted.\n");
1301 int lfs_join(int argc, char **argv)
1303 char *name_head, *name_tail;
1309 name_head = argv[1];
1310 fd = open(name_head, O_WRONLY);
1312 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1316 size = lseek(fd, 0, SEEK_END);
1317 if (size % JOIN_FILE_ALIGN) {
1318 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1319 name_head, (long long)size, JOIN_FILE_ALIGN);
1323 name_tail = argv[2];
1324 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1328 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1334 #ifdef HAVE_SYS_QUOTA_H
1335 static int lfs_quotachown(int argc, char **argv)
1342 while ((c = getopt(argc, argv, "i")) != -1) {
1348 fprintf(stderr, "error: %s: option '-%c' "
1349 "unrecognized\n", argv[0], c);
1355 rc = llapi_quotachown(argv[optind], flag);
1357 fprintf(stderr,"error: change file owner/group failed.\n");
1361 static int lfs_quotacheck(int argc, char **argv)
1363 int c, check_type = 0;
1365 struct if_quotacheck qchk;
1366 struct if_quotactl qctl;
1367 char *obd_type = (char *)qchk.obd_type;
1370 memset(&qchk, 0, sizeof(qchk));
1373 while ((c = getopt(argc, argv, "ug")) != -1) {
1382 fprintf(stderr, "error: %s: option '-%c' "
1383 "unrecognized\n", argv[0], c);
1390 else /* do quotacheck for both user & group quota by default */
1398 memset(&qctl, 0, sizeof(qctl));
1399 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1400 qctl.qc_type = check_type;
1401 rc = llapi_quotactl(mnt, &qctl);
1403 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1407 rc = llapi_quotacheck(mnt, check_type);
1409 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1413 rc = llapi_poll_quotacheck(mnt, &qchk);
1416 fprintf(stderr, "%s %s ", obd_type,
1417 obd_uuid2str(&qchk.obd_uuid));
1418 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1422 memset(&qctl, 0, sizeof(qctl));
1423 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1424 qctl.qc_type = check_type;
1425 rc = llapi_quotactl(mnt, &qctl);
1428 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1429 obd_uuid2str(&qctl.obd_uuid));
1430 fprintf(stderr, "%s turn on quota failed: %s\n",
1431 argv[0], strerror(errno));
1438 static int lfs_quotaon(int argc, char **argv)
1442 struct if_quotactl qctl;
1443 char *obd_type = (char *)qctl.obd_type;
1446 memset(&qctl, 0, sizeof(qctl));
1447 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1450 while ((c = getopt(argc, argv, "ugf")) != -1) {
1453 qctl.qc_type |= 0x01;
1456 qctl.qc_type |= 0x02;
1459 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1462 fprintf(stderr, "error: %s: option '-%c' "
1463 "unrecognized\n", argv[0], c);
1470 else /* by default, enable quota for both user & group */
1471 qctl.qc_type = 0x02;
1478 rc = llapi_quotactl(mnt, &qctl);
1481 fprintf(stderr, "%s %s ", obd_type,
1482 obd_uuid2str(&qctl.obd_uuid));
1483 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1490 static int lfs_quotaoff(int argc, char **argv)
1494 struct if_quotactl qctl;
1495 char *obd_type = (char *)qctl.obd_type;
1498 memset(&qctl, 0, sizeof(qctl));
1499 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1502 while ((c = getopt(argc, argv, "ug")) != -1) {
1505 qctl.qc_type |= 0x01;
1508 qctl.qc_type |= 0x02;
1511 fprintf(stderr, "error: %s: option '-%c' "
1512 "unrecognized\n", argv[0], c);
1519 else /* by default, disable quota for both user & group */
1520 qctl.qc_type = 0x02;
1527 rc = llapi_quotactl(mnt, &qctl);
1528 if (rc == -1 && errno == ESRCH) {
1529 fprintf(stderr, "\n%s quotas are not enabled.\n",
1530 qctl.qc_type == 0x00 ? "user" : "group");
1535 fprintf(stderr, "%s %s ", obd_type,
1536 obd_uuid2str(&qctl.obd_uuid));
1537 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1544 static int lfs_quotainv(int argc, char **argv)
1548 struct if_quotactl qctl;
1551 memset(&qctl, 0, sizeof(qctl));
1552 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1555 while ((c = getopt(argc, argv, "ugf")) != -1) {
1558 qctl.qc_type |= 0x01;
1561 qctl.qc_type |= 0x02;
1564 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1567 fprintf(stderr, "error: %s: option '-%c' "
1568 "unrecognized\n", argv[0], c);
1575 else /* by default, invalidate quota for both user & group */
1576 qctl.qc_type = 0x02;
1583 rc = llapi_quotactl(mnt, &qctl);
1585 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1592 #define ARG2INT(nr, str, msg) \
1595 nr = strtol(str, &endp, 0); \
1597 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1602 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1604 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1605 * returns the value or ULONG_MAX on integer overflow or incorrect format
1607 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1608 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1609 * 3. empty integer value is interpreted as 0
1612 static unsigned long str2sec(const char* timestr) {
1613 const char spec[] = "smhdw";
1614 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1615 unsigned long val = 0;
1618 if (strpbrk(timestr, spec) == NULL) {
1619 /* no specifiers inside the time string,
1620 should treat it as an integer value */
1621 val = strtoul(timestr, &tail, 10);
1622 return *tail ? ULONG_MAX : val;
1625 /* format string is XXwXXdXXhXXmXXs */
1631 v = strtoul(timestr, &tail, 10);
1632 if (v == ULONG_MAX || *tail == '\0')
1633 /* value too large (ULONG_MAX or more)
1634 or missing specifier */
1637 ptr = strchr(spec, *tail);
1639 /* unknown specifier */
1644 /* check if product will overflow the type */
1645 if (!(v < ULONG_MAX / mult[ind]))
1648 ADD_OVERFLOW(val, mult[ind] * v);
1649 if (val == ULONG_MAX)
1661 #define ARG2ULL(nr, str, defscale) \
1663 unsigned long long limit, units = 0; \
1666 rc = parse_size(str, &limit, &units, 1); \
1668 fprintf(stderr, "error: bad limit value %s\n", str); \
1671 nr = ((units == 0) ? (defscale) : 1) * limit; \
1674 static inline int has_times_option(int argc, char **argv)
1678 for (i = 1; i < argc; i++)
1679 if (!strcmp(argv[i], "-t"))
1685 int lfs_setquota_times(int argc, char **argv)
1688 struct if_quotactl qctl;
1689 char *mnt, *obd_type = (char *)qctl.obd_type;
1690 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1691 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1692 struct option long_opts[] = {
1693 {"user", no_argument, 0, 'u'},
1694 {"group", no_argument, 0, 'g'},
1695 {"block-grace", required_argument, 0, 'b'},
1696 {"inode-grace", required_argument, 0, 'i'},
1697 {"times", no_argument, 0, 't'},
1701 memset(&qctl, 0, sizeof(qctl));
1702 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1703 qctl.qc_type = UGQUOTA;
1706 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1710 if (qctl.qc_type != UGQUOTA) {
1711 fprintf(stderr, "error: -u and -g can't be used "
1712 "more than once\n");
1715 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1718 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1719 fprintf(stderr, "error: bad block-grace: %s\n",
1723 dqb->dqb_valid |= QIF_BTIME;
1726 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1727 fprintf(stderr, "error: bad inode-grace: %s\n",
1731 dqb->dqb_valid |= QIF_ITIME;
1733 case 't': /* Yes, of course! */
1735 default: /* getopt prints error message for us when opterr != 0 */
1740 if (qctl.qc_type == UGQUOTA) {
1741 fprintf(stderr, "error: neither -u nor -g specified\n");
1745 if (optind != argc - 1) {
1746 fprintf(stderr, "error: unexpected parameters encountered\n");
1751 rc = llapi_quotactl(mnt, &qctl);
1754 fprintf(stderr, "%s %s ", obd_type,
1755 obd_uuid2str(&qctl.obd_uuid));
1756 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1763 #define BSLIMIT (1 << 0)
1764 #define BHLIMIT (1 << 1)
1765 #define ISLIMIT (1 << 2)
1766 #define IHLIMIT (1 << 3)
1768 int lfs_setquota(int argc, char **argv)
1771 struct if_quotactl qctl;
1772 char *mnt, *obd_type = (char *)qctl.obd_type;
1773 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1774 struct option long_opts[] = {
1775 {"user", required_argument, 0, 'u'},
1776 {"group", required_argument, 0, 'g'},
1777 {"block-softlimit", required_argument, 0, 'b'},
1778 {"block-hardlimit", required_argument, 0, 'B'},
1779 {"inode-softlimit", required_argument, 0, 'i'},
1780 {"inode-hardlimit", required_argument, 0, 'I'},
1783 unsigned limit_mask = 0;
1785 if (has_times_option(argc, argv))
1786 return lfs_setquota_times(argc, argv);
1788 memset(&qctl, 0, sizeof(qctl));
1789 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1790 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1791 * so it can be used as a marker that qc_type
1792 * isn't reinitialized from command line */
1795 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1799 if (qctl.qc_type != UGQUOTA) {
1800 fprintf(stderr, "error: -u and -g can't be used"
1801 " more than once\n");
1804 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1805 rc = name2id(&qctl.qc_id, optarg,
1806 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1808 fprintf(stderr, "error: unknown id %s\n",
1814 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1815 dqb->dqb_bsoftlimit >>= 10;
1816 limit_mask |= BSLIMIT;
1819 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1820 dqb->dqb_bhardlimit >>= 10;
1821 limit_mask |= BHLIMIT;
1824 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1825 limit_mask |= ISLIMIT;
1828 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1829 limit_mask |= IHLIMIT;
1831 default: /* getopt prints error message for us when opterr != 0 */
1836 if (qctl.qc_type == UGQUOTA) {
1837 fprintf(stderr, "error: neither -u nor -g are specified\n");
1841 if (limit_mask == 0) {
1842 fprintf(stderr, "error: at least one limit must be specified\n");
1846 if (optind != argc - 1) {
1847 fprintf(stderr, "error: unexpected parameters encountered\n");
1853 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1854 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1855 /* sigh, we can't just set blimits/ilimits */
1856 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1857 .qc_type = qctl.qc_type,
1858 .qc_id = qctl.qc_id};
1860 rc = llapi_quotactl(mnt, &tmp_qctl);
1862 fprintf(stderr, "error: getquota failed\n");
1866 if (!(limit_mask & BHLIMIT))
1867 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1868 if (!(limit_mask & BSLIMIT))
1869 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1870 if (!(limit_mask & IHLIMIT))
1871 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1872 if (!(limit_mask & ISLIMIT))
1873 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1876 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1877 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1879 rc = llapi_quotactl(mnt, &qctl);
1882 fprintf(stderr, "%s %s ", obd_type,
1883 obd_uuid2str(&qctl.obd_uuid));
1884 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1891 static inline char *type2name(int check_type)
1893 if (check_type == USRQUOTA)
1895 else if (check_type == GRPQUOTA)
1902 /* Converts seconds value into format string
1903 * result is returned in buf
1905 * 1. result is in descenting order: 1w2d3h4m5s
1906 * 2. zero fields are not filled (except for p. 3): 5d1s
1907 * 3. zero seconds value is presented as "0s"
1909 static void sec2str(time_t seconds, char *buf)
1911 const char spec[] = "smhdw";
1912 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1917 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1918 c = seconds / mult[i];
1920 if (c > 0 || (i == 0 && buf == tail))
1921 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1928 static void diff2str(time_t seconds, char *buf, time_t now)
1934 if (seconds <= now) {
1935 strcpy(buf, "none");
1938 sec2str(seconds - now, buf);
1941 static void print_quota_title(char *name, struct if_quotactl *qctl)
1943 printf("Disk quotas for %s %s (%cid %u):\n",
1944 type2name(qctl->qc_type), name,
1945 *type2name(qctl->qc_type), qctl->qc_id);
1946 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1948 "kbytes", "quota", "limit", "grace",
1949 "files", "quota", "limit", "grace");
1952 static void print_quota(char *mnt, struct if_quotactl *qctl, int type, int rc)
1958 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1959 int bover = 0, iover = 0;
1960 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1962 if (dqb->dqb_bhardlimit &&
1963 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1965 } else if (dqb->dqb_bsoftlimit &&
1966 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1967 if (dqb->dqb_btime > now) {
1974 if (dqb->dqb_ihardlimit &&
1975 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1977 } else if (dqb->dqb_isoftlimit &&
1978 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1979 if (dqb->dqb_btime > now) {
1986 #if 0 /* XXX: always print quotas even when no usages */
1987 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1993 if (strlen(mnt) > 15)
1994 printf("%s\n%15s", mnt, "");
1996 printf("%15s", mnt);
1999 diff2str(dqb->dqb_btime, timebuf, now);
2000 if (rc == -1 && errno == EREMOTEIO)
2001 sprintf(numbuf[0], LPU64"*",
2002 toqb(dqb->dqb_curspace));
2005 (dqb->dqb_valid & QIF_SPACE) ?
2006 LPU64 : "["LPU64"]",
2007 toqb(dqb->dqb_curspace));
2008 if (type == QC_GENERAL)
2009 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
2010 ? LPU64 : "["LPU64"]",
2011 dqb->dqb_bsoftlimit);
2013 sprintf(numbuf[1], "%s", "");
2014 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
2015 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
2016 printf(" %7s%c %6s %7s %7s",
2017 numbuf[0], bover ? '*' : ' ', numbuf[1],
2018 numbuf[2], bover > 1 ? timebuf : "");
2021 diff2str(dqb->dqb_itime, timebuf, now);
2023 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2024 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2025 if (type == QC_GENERAL)
2026 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2027 ? LPU64 : "["LPU64"]",
2028 dqb->dqb_isoftlimit);
2030 sprintf(numbuf[1], "%s", "");
2031 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2032 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2033 if (type != QC_OSTIDX)
2034 printf(" %7s%c %6s %7s %7s",
2035 numbuf[0], iover ? '*' : ' ', numbuf[1],
2036 numbuf[2], iover > 1 ? timebuf : "");
2039 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2040 qctl->qc_cmd == Q_GETOINFO) {
2044 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
2045 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
2046 printf("Block grace time: %s; Inode grace time: %s\n",
2047 bgtimebuf, igtimebuf);
2051 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2053 int rc = 0, rc1 = 0, count = 0;
2054 __u32 valid = qctl->qc_valid;
2056 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2058 fprintf(stderr, "can not get %s count: %s\n",
2059 is_mdt ? "mdt": "ost", strerror(errno));
2063 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2064 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2065 rc = llapi_quotactl(mnt, qctl);
2067 /* It is remote client case. */
2068 if (errno == EOPNOTSUPP) {
2075 fprintf(stderr, "quotactl %s%d failed.\n",
2076 is_mdt ? "mdt": "ost", qctl->qc_idx);
2080 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid, 0);
2084 qctl->qc_valid = valid;
2088 static int lfs_quota(int argc, char **argv)
2091 char *mnt, *name = NULL;
2092 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2093 .qc_type = UGQUOTA };
2094 char *obd_type = (char *)qctl.obd_type;
2095 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2096 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2097 __u32 valid = QC_GENERAL, idx = 0;
2100 while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2103 if (qctl.qc_type != UGQUOTA) {
2104 fprintf(stderr, "error: use either -u or -g\n");
2107 qctl.qc_type = USRQUOTA;
2110 if (qctl.qc_type != UGQUOTA) {
2111 fprintf(stderr, "error: use either -u or -g\n");
2114 qctl.qc_type = GRPQUOTA;
2117 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2120 valid = qctl.qc_valid = QC_UUID;
2121 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2124 valid = qctl.qc_valid = QC_MDTIDX;
2125 idx = qctl.qc_idx = atoi(optarg);
2128 valid = qctl.qc_valid = QC_OSTIDX;
2129 idx = qctl.qc_idx = atoi(optarg);
2135 fprintf(stderr, "error: %s: option '-%c' "
2136 "unrecognized\n", argv[0], c);
2141 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2142 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2143 optind == argc - 1) {
2145 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2146 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2147 qctl.qc_valid = valid;
2150 qctl.qc_type = USRQUOTA;
2151 qctl.qc_id = geteuid();
2153 qctl.qc_type = GRPQUOTA;
2154 qctl.qc_id = getegid();
2156 rc = id2name(&name, qctl.qc_id,
2157 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2160 /* lfs quota -u username /path/to/lustre/mount */
2161 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2162 /* options should be followed by u/g-name and mntpoint */
2163 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2164 fprintf(stderr, "error: missing quota argument(s)\n");
2168 name = argv[optind++];
2169 rc = name2id(&qctl.qc_id, name,
2170 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2172 fprintf(stderr,"error: can't find id for name %s: %s\n",
2173 name, strerror(errno));
2176 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2177 fprintf(stderr, "error: missing quota info argument(s)\n");
2181 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2182 print_quota_title(name, &qctl);
2186 rc1 = llapi_quotactl(mnt, &qctl);
2187 if (rc1 == -1 && errno == ESRCH) {
2188 fprintf(stderr, "\n%s quotas are not enabled.\n",
2189 qctl.qc_type == USRQUOTA ? "user" : "group");
2192 if (rc1 && *obd_type)
2193 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2195 if (qctl.qc_valid != QC_GENERAL)
2198 print_quota(mnt, &qctl, QC_GENERAL, rc1);
2200 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2201 rc2 = print_obd_quota(mnt, &qctl, 1);
2202 rc3 = print_obd_quota(mnt, &qctl, 0);
2205 if (rc1 || rc2 || rc3)
2206 printf("Some errors happened when getting quota info. "
2207 "Some devices may be not working or deactivated. "
2208 "The data in \"[]\" is inaccurate.\n");
2216 #endif /* HAVE_SYS_QUOTA_H! */
2218 static int flushctx_ioctl(char *mp)
2222 fd = open(mp, O_RDONLY);
2224 fprintf(stderr, "flushctx: error open %s: %s\n",
2225 mp, strerror(errno));
2229 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2231 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2232 mp, strerror(errno));
2238 static int lfs_flushctx(int argc, char **argv)
2240 int kdestroy = 0, c;
2242 char procline[PATH_MAX], *line;
2246 while ((c = getopt(argc, argv, "k")) != -1) {
2252 fprintf(stderr, "error: %s: option '-%c' "
2253 "unrecognized\n", argv[0], c);
2259 system("kdestroy > /dev/null");
2261 if (optind >= argc) {
2262 /* flush for all mounted lustre fs. */
2263 proc = fopen("/proc/mounts", "r");
2265 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2270 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2275 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2276 fprintf(stderr, "%s: unexpected format in "
2282 if (strcmp(fs, "lustre") != 0)
2284 /* we use '@' to determine it's a client. are there
2285 * any other better way?
2287 if (strchr(dev, '@') == NULL)
2290 if (flushctx_ioctl(mp))
2294 /* flush fs as specified */
2295 while (optind < argc) {
2296 if (flushctx_ioctl(argv[optind++]))
2304 static int lfs_lsetfacl(int argc, char **argv)
2307 return(llapi_lsetfacl(argc, argv));
2310 static int lfs_lgetfacl(int argc, char **argv)
2313 return(llapi_lgetfacl(argc, argv));
2316 static int lfs_rsetfacl(int argc, char **argv)
2319 return(llapi_rsetfacl(argc, argv));
2322 static int lfs_rgetfacl(int argc, char **argv)
2325 return(llapi_rgetfacl(argc, argv));
2328 static int lfs_cp(int argc, char **argv)
2330 return(llapi_cp(argc, argv));
2333 static int lfs_ls(int argc, char **argv)
2335 return(llapi_ls(argc, argv));
2338 /* A helper function to return single, whole lines delimited by newline.
2339 Returns length of line. Not reentrant! */
2340 static int get_next_full_line(int fd, char **ptr)
2342 static char buf[8192]; /* bigger than MAX_PATH_LENGTH */
2343 static char *sptr = buf, *eptr = buf;
2344 static int len, rem;
2346 if ((*ptr == NULL) /* first time */
2347 || (eptr >= buf + len) /* buffer empty */) {
2349 len = read(fd, buf, sizeof(buf));
2357 while (eptr < buf + len) {
2359 /* parse full lines */
2360 if (*eptr == '\n') {
2363 return (eptr - sptr);
2367 /* partial line; move to front of buf */
2368 rem = buf + len - sptr;
2369 memcpy(buf, sptr, rem);
2372 len = read(fd, eptr, sizeof(buf) - rem);
2379 static int lfs_changelog(int argc, char **argv)
2381 long long startrec = 0, endrec = 0, recnum;
2383 char c, *mdd, *ptr = NULL;
2384 struct option long_opts[] = {
2385 {"follow", no_argument, 0, 'f'},
2388 char short_opts[] = "f";
2392 while ((c = getopt_long(argc, argv, short_opts,
2393 long_opts, NULL)) != -1) {
2401 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2402 argv[0], argv[optind - 1]);
2409 mdd = argv[optind++];
2411 startrec = strtoll(argv[optind++], NULL, 10);
2413 endrec = strtoll(argv[optind++], NULL, 10);
2415 fd = llapi_changelog_open(mdd, startrec);
2417 fprintf(stderr, "%s Can't open changelog: %s\n", argv[0],
2418 strerror(errno = -fd));
2422 while ((len = get_next_full_line(fd, &ptr)) >= 0) {
2424 struct pollfd pfds[1];
2430 pfds[0].events = POLLIN;
2431 rc = poll(pfds, 1, -1);
2436 /* eg. 2 02MKDIR 4405821890 t=[0x100000400/0x5] p=[0x100000400/0x4] pics */
2437 sscanf(ptr, "%lld *", &recnum);
2438 if (endrec && recnum > endrec)
2440 if (recnum < startrec)
2442 printf("%.*s\n", len, ptr);
2448 fprintf(stderr, "read err %d\n", errno);
2455 static int lfs_changelog_clear(int argc, char **argv)
2463 endrec = strtoll(argv[3], NULL, 10);
2465 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
2467 fprintf(stderr, "%s error: %s\n", argv[0],
2468 strerror(errno = -rc));
2472 static int lfs_fid2path(int argc, char **argv)
2474 struct option long_opts[] = {
2475 {"cur", no_argument, 0, 'c'},
2476 {"link", required_argument, 0, 'l'},
2477 {"rec", required_argument, 0, 'r'},
2480 char c, short_opts[] = "cl:r:";
2481 char *device, *fid, *path;
2482 long long recno = -1;
2489 while ((c = getopt_long(argc, argv, short_opts,
2490 long_opts, NULL)) != -1) {
2496 linkno = strtol(optarg, NULL, 10);
2499 recno = strtoll(optarg, NULL, 10);
2504 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2505 argv[0], argv[optind - 1]);
2510 device = argv[optind++];
2511 fid = argv[optind++];
2515 path = calloc(1, PATH_MAX);
2517 lnktmp = (linkno >= 0) ? linkno : 0;
2519 int oldtmp = lnktmp;
2520 long long rectmp = recno;
2521 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2524 fprintf(stderr, "%s error: %s\n", argv[0],
2525 strerror(errno = -rc));
2530 fprintf(stdout, "%lld %s\n", rectmp, path);
2532 fprintf(stdout, "%s\n", path);
2535 /* specified linkno */
2537 if (oldtmp == lnktmp)
2546 static int lfs_path2fid(int argc, char **argv)
2549 unsigned long long seq;
2550 unsigned long oid, ver;
2557 rc = llapi_path2fid(path, &seq, &oid, &ver);
2559 fprintf(stderr, "can't get fid for %s: %s\n", path,
2560 strerror(errno = -rc));
2564 printf(DFID"\n", seq, (unsigned int)oid, (unsigned int)ver);
2569 int main(int argc, char **argv)
2575 ptl_initialize(argc, argv);
2576 if (obd_initialize(argc, argv) < 0)
2578 if (dbg_initialize(argc, argv) < 0)
2581 Parser_init("lfs > ", cmdlist);
2584 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2586 rc = Parser_commands();
2589 obd_finalize(argc, argv);