1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
56 #include <sys/types.h>
62 #ifdef HAVE_SYS_QUOTA_H
63 # include <sys/quota.h>
69 #include <lnet/api-support.h>
70 #include <lnet/lnetctl.h>
72 #include <liblustre.h>
73 #include <lustre/lustre_idl.h>
74 #include <lustre/liblustreapi.h>
75 #include <lustre/lustre_user.h>
76 #include <lustre_quota.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>]\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 of OST pool"},
131 {"getstripe", lfs_getstripe, 0,
132 "To list the striping info for a given file or files in a\n"
133 "directory or recursively for all files in a directory tree.\n"
134 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
135 " [--count | -c ] [--size | -s ] [--index | -i ]\n"
136 " [--offset | -o ] [--pool | -p ]\n"
137 " [--recursive | -r] <dir|file> ..."},
138 {"pool_list", lfs_poollist, 0,
139 "List pools or pool OSTs\n"
140 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
141 {"find", lfs_find, 0,
142 "To find files that match given parameters recursively in a directory tree.\n"
143 "usage: find <dir|file> ... \n"
144 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
145 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
146 " [--print|-p] [--obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
147 " [[!] --type|-t <filetype>] [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
148 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
149 " [[!] --pool <pool>]\n"
150 "\t !: used before an option indicates 'NOT' the requested attribute\n"
151 "\t -: used before an value indicates 'AT MOST' the requested value\n"
152 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
153 {"check", lfs_check, 0,
154 "Display the status of MDS or OSTs (as specified in the command)\n"
155 "or all the servers (MDS and OSTs).\n"
156 "usage: check <osts|mds|servers>"},
157 {"catinfo", lfs_catinfo, 0,
158 "Show information of specified type logs.\n"
159 "usage: catinfo {keyword} [node name]\n"
160 "\tkeywords are one of followings: config, deletions.\n"
161 "\tnode name must be provided when use keyword config."},
162 {"join", lfs_join, 0,
163 "join two lustre files into one.\n"
164 "obsolete, HEAD does not support it anymore.\n"},
165 {"osts", lfs_osts, 0, "osts"},
167 "report filesystem disk space usage or inodes usage"
168 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
169 "Usage: df [-i] [-h] [--pool|-p <fsname>[.<pool>] [path]"},
170 #ifdef HAVE_SYS_QUOTA_H
171 {"quotachown",lfs_quotachown, 0,
172 "Change files' owner or group on the specified filesystem.\n"
173 "usage: quotachown [-i] <filesystem>\n"
174 "\t-i: ignore error if file is not exist\n"},
175 {"quotacheck", lfs_quotacheck, 0,
176 "Scan the specified filesystem for disk usage, and create,\n"
177 "or update quota files.\n"
178 "usage: quotacheck [ -ug ] <filesystem>"},
179 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
180 "usage: quotaon [ -ugf ] <filesystem>"},
181 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
182 "usage: quotaoff [ -ug ] <filesystem>"},
183 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
184 "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
185 " -b <block-softlimit> -B <block-hardlimit>\n"
186 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
187 " setquota -t <-u|-g> <block-grace> <inode-grace> <filesystem>\n"
188 " setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
189 " [--block-softlimit <block-softlimit>]\n"
190 " [--block-hardlimit <block-hardlimit>]\n"
191 " [--inode-softlimit <inode-softlimit>]\n"
192 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
193 " setquota [-t] <-u|--user|-g|--group>\n"
194 " [--block-grace <block-grace>]\n"
195 " [--inode-grace <inode-grace>] <filesystem>\n"
196 " -b can be used instead of --block-softlimit/--block-grace\n"
197 " -B can be used instead of --block-hardlimit\n"
198 " -i can be used instead of --inode-softlimit/--inode-grace\n"
199 " -I can be used instead of --inode-hardlimit"},
200 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
201 "usage: quota [-v] [-o obd_uuid|-i mdt_idx|-I ost_idx]\n"
202 " [<-u|-g> <uname>|<uid>|<gname>|<gid>] <filesystem>\n"
203 " quota [-o obd_uuid|-i mdt_idx|-I ost_idx] -t <-u|-g> <filesystem>"},
204 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
205 "usage: quotainv [-u|-g] <filesystem>"},
207 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
208 "usage: flushctx [-k] [mountpoint...]"},
209 {"lsetfacl", lfs_lsetfacl, 0,
210 "Remote user setfacl for user/group on the same remote client.\n"
211 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
212 {"lgetfacl", lfs_lgetfacl, 0,
213 "Remote user getfacl for user/group on the same remote client.\n"
214 "usage: lgetfacl [-dRLPvh] file ..."},
215 {"rsetfacl", lfs_rsetfacl, 0,
216 "Remote user setfacl for user/group on other clients.\n"
217 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
218 {"rgetfacl", lfs_rgetfacl, 0,
219 "Remote user getfacl for user/group on other clients.\n"
220 "usage: rgetfacl [-dRLPvh] file ..."},
222 "Remote user copy files and directories.\n"
223 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
225 "Remote user list directory contents.\n"
226 "usage: ls [OPTION]... [FILE]..."},
227 {"changelog", lfs_changelog, 0,
228 "Show the metadata changes on an MDT."
229 "\nusage: changelog <mdtname> [startrec [endrec]]"},
230 {"changelog_clear", lfs_changelog_clear, 0,
231 "Indicate that old changelog records up to <endrec> are no longer of "
232 "interest to consumer <id>, allowing the system to free up space.\n"
233 "An <endrec> of 0 means all records.\n"
234 "usage: changelog_clear <mdtname> <id> <endrec>"},
235 {"fid2path", lfs_fid2path, 0,
236 "Resolve the full path to a given FID. For a specific hardlink "
237 "specify link number <linkno>.\n"
238 /* "For a historical name, specify changelog record <recno>.\n" */
239 "usage: fid2path <fsname|rootpath> <fid> [--link <linkno>]"
240 /*[--rec <recno>]*/},
241 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
242 "usage: path2fid <path>"},
243 {"help", Parser_help, 0, "help"},
244 {"exit", Parser_quit, 0, "quit"},
245 {"quit", Parser_quit, 0, "quit"},
249 static int isnumber(const char *str)
253 if (str[0] != '-' && !isdigit(str[0]))
256 for (ptr = str + 1; *ptr != '\0'; ptr++) {
265 static int lfs_setstripe(int argc, char **argv)
269 unsigned long long st_size;
270 int st_offset, st_count;
274 char *stripe_size_arg = NULL;
275 char *stripe_off_arg = NULL;
276 char *stripe_count_arg = NULL;
277 char *pool_name_arg = NULL;
278 unsigned long long size_units;
280 struct option long_opts[] = {
281 {"size", required_argument, 0, 's'},
282 {"count", required_argument, 0, 'c'},
283 {"index", required_argument, 0, 'i'},
284 {"offset", required_argument, 0, 'o'},
285 {"pool", required_argument, 0, 'p'},
286 {"delete", no_argument, 0, 'd'},
294 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,1,0,0)
295 if (argc == 5 && argv[1][0] != '-' &&
296 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
297 fprintf(stderr, "warning: deprecated usage of setstripe "
298 "positional parameters. Use -c, -i, -s instead.\n");
299 /* for compatibility with the existing positional parameter
302 stripe_size_arg = argv[2];
303 stripe_off_arg = argv[3];
304 stripe_count_arg = argv[4];
308 #warning "remove obsolete positional parameter code"
312 while ((c = getopt_long(argc, argv, "c:di:o:s:p:",
313 long_opts, NULL)) >= 0) {
319 stripe_count_arg = optarg;
322 /* delete the default striping pattern */
327 stripe_off_arg = optarg;
330 stripe_size_arg = optarg;
333 pool_name_arg = optarg;
338 fprintf(stderr, "error: %s: option '%s' "
340 argv[0], argv[optind - 1]);
345 fname = argv[optind];
348 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
349 stripe_count_arg != NULL || pool_name_arg != NULL)) {
350 fprintf(stderr, "error: %s: cannot specify -d with "
351 "-s, -c -o or -p options\n",
357 if (optind == argc) {
358 fprintf(stderr, "error: %s: missing filename|dirname\n",
363 /* get the stripe size */
364 if (stripe_size_arg != NULL) {
365 result = parse_size(stripe_size_arg, &st_size, &size_units, 0);
367 fprintf(stderr, "error: %s: bad size '%s'\n",
368 argv[0], stripe_size_arg);
372 /* get the stripe offset */
373 if (stripe_off_arg != NULL) {
374 st_offset = strtoul(stripe_off_arg, &end, 0);
376 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
377 argv[0], stripe_off_arg);
381 /* get the stripe count */
382 if (stripe_count_arg != NULL) {
383 st_count = strtoul(stripe_count_arg, &end, 0);
385 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
386 argv[0], stripe_count_arg);
392 result = llapi_file_create_pool(fname, st_size, st_offset,
393 st_count, 0, pool_name_arg);
395 fprintf(stderr,"error: %s: create stripe file '%s' "
396 "failed\n", argv[0], fname);
399 fname = argv[++optind];
400 } while (fname != NULL);
405 static int lfs_poollist(int argc, char **argv)
410 return llapi_poollist(argv[1]);
413 static int set_time(time_t *time, time_t *set, char *str)
420 else if (str[0] == '-')
426 t = strtol(str, NULL, 0);
427 if (*time < t * 24 * 60 * 60) {
430 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
434 *set = *time - t * 24 * 60 * 60;
441 static int name2id(unsigned int *id, char *name, int type)
444 struct passwd *entry;
446 if (!(entry = getpwnam(name))) {
456 if (!(entry = getgrnam(name))) {
468 static int id2name(char **name, unsigned int id, int type)
471 struct passwd *entry;
473 if (!(entry = getpwuid(id))) {
479 *name = entry->pw_name;
483 if (!(entry = getgrgid(id))) {
489 *name = entry->gr_name;
495 #define FIND_POOL_OPT 3
496 static int lfs_find(int argc, char **argv)
501 struct find_param param = { .maxdepth = -1 };
503 struct option long_opts[] = {
504 /* New find options. */
505 {"atime", required_argument, 0, 'A'},
506 {"ctime", required_argument, 0, 'C'},
507 {"mtime", required_argument, 0, 'M'},
508 {"maxdepth", required_argument, 0, 'D'},
509 {"gid", required_argument, 0, 'g'},
510 {"group", required_argument, 0, 'G'},
511 {"uid", required_argument, 0, 'u'},
512 {"user", required_argument, 0, 'U'},
513 {"name", required_argument, 0, 'n'},
514 /* no short option for pool, p/P already used */
515 {"pool", required_argument, 0, FIND_POOL_OPT},
516 /* --obd is considered as a new option. */
517 {"obd", required_argument, 0, 'O'},
518 {"ost", required_argument, 0, 'O'},
519 {"print", no_argument, 0, 'P'},
520 {"print0", no_argument, 0, 'p'},
521 /* Old find options. */
522 {"quiet", no_argument, 0, 'q'},
523 {"recursive", no_argument, 0, 'r'},
524 {"size", required_argument, 0, 's'},
525 {"type", required_argument, 0, 't'},
526 {"verbose", no_argument, 0, 'v'},
540 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
541 while ((c = getopt_long_only(argc,argv,"-A:C:D:g:G:M:n:PpO:qrs:t:u:U:v",
542 long_opts, NULL)) >= 0) {
547 /* '!' is part of option */
548 /* when getopt_long_only() finds a string which is not
549 * an option nor a known option argument it returns 1
550 * in that case if we already have found pathstart and pathend
551 * (i.e. we have the list of pathnames),
552 * the only supported value is "!"
554 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
555 if (!isoption && pathend != -1) {
556 fprintf(stderr, "err: %s: filename|dirname must either "
557 "precede options or follow options\n",
561 if (!isoption && pathstart == -1)
562 pathstart = optind - 1;
563 if (isoption && pathstart != -1 && pathend == -1) {
564 pathend = optind - 2;
565 if ((c == 1 && strcmp(optarg, "!") == 0) ||
566 c == 'P' || c == 'p' || c == 'O' ||
567 c == 'q' || c == 'r' || c == 'v')
568 pathend = optind - 1;
575 /* unknown; opt is "!" or path component,
576 * checking done above.
578 if (strcmp(optarg, "!") == 0)
582 xtime = ¶m.atime;
583 xsign = ¶m.asign;
586 xtime = ¶m.ctime;
587 xsign = ¶m.csign;
591 xtime = ¶m.mtime;
592 xsign = ¶m.msign;
596 if (optarg[0] == '-')
598 else if (optarg[0] == '+')
607 ret = set_time(&t, xtime, optarg);
615 param.maxdepth = strtol(optarg, 0, 0);
620 ret = name2id(¶m.gid, optarg, GROUP);
622 param.gid = strtoul(optarg, &endptr, 10);
623 if (*endptr != '\0') {
624 fprintf(stderr, "Group/GID: %s cannot "
625 "be found.\n", optarg);
629 param.exclude_gid = !!neg_opt;
635 ret = name2id(¶m.uid, optarg, USER);
637 param.uid = strtoul(optarg, &endptr, 10);
638 if (*endptr != '\0') {
639 fprintf(stderr, "User/UID: %s cannot "
640 "be found.\n", optarg);
644 param.exclude_uid = !!neg_opt;
649 if (strlen(optarg) > LOV_MAXPOOLNAME) {
651 "Pool name %s is too long"
652 " (max is %d)\n", optarg,
656 /* we do check for empty pool because empty pool
657 * is used to find V1 lov attributes */
658 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
659 param.poolname[LOV_MAXPOOLNAME] = '\0';
660 param.exclude_pool = !!neg_opt;
661 param.check_pool = 1;
665 param.pattern = (char *)optarg;
666 param.exclude_pattern = !!neg_opt;
669 char *buf, *token, *next, *p;
672 len = strlen((char *)optarg);
676 strcpy(buf, (char *)optarg);
678 if (param.num_alloc_obds == 0) {
679 param.obduuid = malloc(FIND_MAX_OSTS *
680 sizeof(struct obd_uuid));
681 if (param.obduuid == NULL)
683 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
686 for (token = buf; token && *token; token = next) {
687 p = strchr(token, ',');
693 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
717 param.exclude_type = !!neg_opt;
719 case 'b': param.type = S_IFBLK; break;
720 case 'c': param.type = S_IFCHR; break;
721 case 'd': param.type = S_IFDIR; break;
722 case 'f': param.type = S_IFREG; break;
723 case 'l': param.type = S_IFLNK; break;
724 case 'p': param.type = S_IFIFO; break;
725 case 's': param.type = S_IFSOCK; break;
726 #ifdef S_IFDOOR /* Solaris only */
727 case 'D': param.type = S_IFDOOR; break;
729 default: fprintf(stderr, "error: %s: bad type '%s'\n",
736 if (optarg[0] == '-')
738 else if (optarg[0] == '+')
747 if (optarg[0] == '+')
748 param.size_sign = -1;
749 else if (optarg[0] == '-')
750 param.size_sign = +1;
754 ret = parse_size(optarg, ¶m.size,
755 ¶m.size_units, 0);
757 fprintf(stderr,"error: bad size '%s'\n",
770 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
771 argv[0], argv[optind - 1]);
776 if (pathstart == -1) {
777 fprintf(stderr, "error: %s: no filename|pathname\n",
780 } else if (pathend == -1) {
788 static int deprecated_warning;
789 if (!deprecated_warning) {
790 fprintf(stderr, "lfs find: -q, -r, -v options "
791 "deprecated. Use 'lfs getstripe' instead.\n");
792 deprecated_warning = 1;
794 if (!param.recursive && param.maxdepth == -1)
800 ret = llapi_find(argv[pathstart], ¶m);
802 ret = llapi_getstripe(argv[pathstart], ¶m);
803 } while (++pathstart < pathend && !ret);
806 fprintf(stderr, "error: %s failed for %s.\n",
807 argv[0], argv[optind - 1]);
809 if (param.obduuid && param.num_alloc_obds)
815 static int lfs_getstripe(int argc, char **argv)
817 struct option long_opts[] = {
819 {"quiet", 0, 0, 'q'},
820 {"recursive", 0, 0, 'r'},
821 {"count", 0, 0, 'c'},
823 {"index", 0, 0, 'i'},
824 {"offset", 0, 0, 'o'},
826 {"verbose", 0, 0, 'v'},
829 char short_opts[] = "hO:qrvcsiop";
831 struct find_param param = { 0 };
834 while ((c = getopt_long(argc, argv, short_opts,
835 long_opts, NULL)) != -1) {
840 "error: %s: only one obduuid allowed",
844 param.obduuid = (struct obd_uuid *)optarg;
853 param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
857 param.verbose |= VERBOSE_COUNT;
860 param.verbose |= VERBOSE_SIZE;
864 param.verbose |= VERBOSE_OFFSET;
867 param.verbose |= VERBOSE_POOL;
872 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
873 argv[0], argv[optind - 1]);
881 param.maxdepth = param.recursive ? -1 : 1;
884 rc = llapi_getstripe(argv[optind], ¶m);
885 } while (++optind < argc && !rc);
888 fprintf(stderr, "error: %s failed for %s.\n",
889 argv[0], argv[optind - 1]);
893 static int lfs_osts(int argc, char **argv)
895 char mntdir[PATH_MAX] = {'\0'};
896 struct find_param param;
902 while (llapi_search_mounts(NULL, index++, mntdir, NULL) == 0) {
903 memset(¶m, 0, sizeof(param));
904 rc = llapi_getstripe(mntdir, ¶m);
906 fprintf(stderr, "error: %s: failed on %s\n",
909 memset(mntdir, 0, PATH_MAX);
915 #define COOK(value) \
918 while (value > 1024) { \
932 static int showdf(char *mntdir, struct obd_statfs *stat,
933 char *uuid, int ishow, int cooked,
934 char *type, int index, int rc)
936 long long avail, used, total;
938 char *suffix = "KMGTPEZY";
939 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
940 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
948 avail = stat->os_ffree;
949 used = stat->os_files - stat->os_ffree;
950 total = stat->os_files;
952 int shift = cooked ? 0 : 10;
954 avail = (stat->os_bavail * stat->os_bsize) >> shift;
955 used = ((stat->os_blocks - stat->os_bfree) *
956 stat->os_bsize) >> shift;
957 total = (stat->os_blocks * stat->os_bsize) >> shift;
961 ratio = (double)used / (double)total;
967 cook_val = (double)total;
970 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
972 sprintf(tbuf, CDF, total);
974 cook_val = (double)used;
977 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
979 sprintf(ubuf, CDF, used);
981 cook_val = (double)avail;
984 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
986 sprintf(abuf, CDF, avail);
988 sprintf(tbuf, CDF, total);
989 sprintf(ubuf, CDF, used);
990 sprintf(abuf, CDF, avail);
993 sprintf(rbuf, RDF, (int)(ratio * 100));
994 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
995 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
997 printf("[%s:%d]\n", type, index);
1003 printf(UUF": inactive device\n", uuid);
1006 printf(UUF": %s\n", uuid, strerror(-rc));
1013 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow, int cooked)
1015 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1016 struct obd_uuid uuid_buf;
1017 char *poolname = NULL;
1022 poolname = strchr(pool, '.');
1023 if (poolname != NULL) {
1024 if (strncmp(fsname, pool, strlen(fsname))) {
1025 fprintf(stderr, "filesystem name incorrect\n");
1034 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1035 "UUID", "Inodes", "IUsed", "IFree",
1036 "IUse%", "Mounted on");
1038 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1039 "UUID", cooked ? "bytes" : "1K-blocks",
1040 "Used", "Available", "Use%", "Mounted on");
1042 for (index = 0; ; index++) {
1043 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1044 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1045 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
1046 &stat_buf, &uuid_buf);
1053 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1054 rc == -ENODATA || rc == 0) {
1055 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1056 ishow, cooked, "MDT", index, rc);
1059 "error: llapi_obd_statfs(%s): %s (%d)\n",
1060 obd_uuid2str(&uuid_buf), strerror(-rc), rc);
1064 sum.os_ffree += stat_buf.os_ffree;
1065 sum.os_files += stat_buf.os_files;
1069 for (index = 0; ; index++) {
1070 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1071 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1072 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
1073 &stat_buf, &uuid_buf);
1080 if (llapi_search_ost(fsname, poolname,
1081 obd_uuid2str(&uuid_buf)) != 1)
1084 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
1085 rc == -ENODATA || rc == 0) {
1086 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1087 ishow, cooked, "OST", index, rc);
1090 "error: llapi_obd_statfs failed: %s (%d)\n",
1095 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
1096 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
1097 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
1102 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1107 static int lfs_df(int argc, char **argv)
1110 char *mntdir = NULL;
1111 int ishow = 0, cooked = 0;
1113 char fsname[PATH_MAX], *pool_name = NULL;
1114 struct option long_opts[] = {
1115 {"pool", required_argument, 0, 'p'},
1120 while ((c = getopt_long(argc, argv, "ihp:", long_opts, NULL)) != -1) {
1136 path = argv[optind];
1138 if ((mntdir = malloc(PATH_MAX)) == NULL) {
1139 fprintf(stderr, "error: cannot allocate %d bytes\n",
1143 memset(mntdir, 0, PATH_MAX);
1146 char rpath[PATH_MAX] = {'\0'};
1148 if (!realpath(path, rpath)) {
1150 fprintf(stderr, "error: invalid path '%s': %s\n",
1151 path, strerror(-rc));
1153 rc = llapi_search_mounts(rpath, 0, mntdir, fsname);
1154 if (rc == 0 && mntdir[0] != '\0') {
1155 rc = mntdf(mntdir, fsname, pool_name,
1163 while (llapi_search_mounts(NULL, index++, mntdir, fsname)==0) {
1164 rc = mntdf(mntdir, fsname, pool_name,
1176 static int lfs_check(int argc, char **argv)
1179 char mntdir[PATH_MAX] = {'\0'};
1188 obd_types[0] = obd_type1;
1189 obd_types[1] = obd_type2;
1191 if (strcmp(argv[1], "osts") == 0) {
1192 strcpy(obd_types[0], "osc");
1193 } else if (strcmp(argv[1], "mds") == 0) {
1194 strcpy(obd_types[0], "mdc");
1195 } else if (strcmp(argv[1], "servers") == 0) {
1197 strcpy(obd_types[0], "osc");
1198 strcpy(obd_types[1], "mdc");
1200 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1205 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1206 if (rc < 0 || mntdir[0] == '\0') {
1207 fprintf(stderr, "No suitable Lustre mount found\n");
1211 rc = llapi_target_iterate(num_types, obd_types,
1212 mntdir, llapi_ping_target);
1215 fprintf(stderr, "error: %s: %s status failed\n",
1222 static int lfs_catinfo(int argc, char **argv)
1224 char mntdir[PATH_MAX] = {'\0'};
1227 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1230 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1233 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1234 if (rc == 0 && mntdir[0] != '\0') {
1236 rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1238 rc = llapi_catinfo(mntdir, argv[1], NULL);
1240 fprintf(stderr, "no lustre_lite mounted.\n");
1247 static int lfs_join(int argc, char **argv)
1249 fprintf(stderr, "join two lustre files into one.\n"
1250 "obsolete, HEAD does not support it anymore.\n");
1254 #ifdef HAVE_SYS_QUOTA_H
1255 static int lfs_quotachown(int argc, char **argv)
1262 while ((c = getopt(argc, argv, "i")) != -1) {
1268 fprintf(stderr, "error: %s: option '-%c' "
1269 "unrecognized\n", argv[0], c);
1275 rc = llapi_quotachown(argv[optind], flag);
1277 fprintf(stderr,"error: change file owner/group failed.\n");
1281 static int lfs_quotacheck(int argc, char **argv)
1283 int c, check_type = 0;
1285 struct if_quotacheck qchk;
1286 struct if_quotactl qctl;
1287 char *obd_type = (char *)qchk.obd_type;
1290 memset(&qchk, 0, sizeof(qchk));
1293 while ((c = getopt(argc, argv, "ug")) != -1) {
1302 fprintf(stderr, "error: %s: option '-%c' "
1303 "unrecognized\n", argv[0], c);
1310 else /* do quotacheck for both user & group quota by default */
1318 rc = llapi_quotacheck(mnt, check_type);
1320 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1324 rc = llapi_poll_quotacheck(mnt, &qchk);
1327 fprintf(stderr, "%s %s ", obd_type,
1328 obd_uuid2str(&qchk.obd_uuid));
1329 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1333 memset(&qctl, 0, sizeof(qctl));
1334 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1335 qctl.qc_type = check_type;
1336 rc = llapi_quotactl(mnt, &qctl);
1337 if (rc && errno != EALREADY) {
1339 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1340 obd_uuid2str(&qctl.obd_uuid));
1341 fprintf(stderr, "%s turn on quota failed: %s\n",
1342 argv[0], strerror(errno));
1349 static int lfs_quotaon(int argc, char **argv)
1353 struct if_quotactl qctl;
1354 char *obd_type = (char *)qctl.obd_type;
1357 memset(&qctl, 0, sizeof(qctl));
1358 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1361 while ((c = getopt(argc, argv, "ugf")) != -1) {
1364 qctl.qc_type |= 0x01;
1367 qctl.qc_type |= 0x02;
1370 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1373 fprintf(stderr, "error: %s: option '-%c' "
1374 "unrecognized\n", argv[0], c);
1381 else /* by default, enable quota for both user & group */
1382 qctl.qc_type = 0x02;
1389 rc = llapi_quotactl(mnt, &qctl);
1391 if (errno == EALREADY) {
1392 fprintf(stderr, "\n%s quotas are enabled already.\n",
1393 qctl.qc_type == 0x02 ? "user/group" :
1394 (qctl.qc_type == 0x00 ? "user" : "group"));
1396 } else if (errno == ENOENT) {
1397 fprintf(stderr, "error: cannot find quota database, "
1398 "make sure you have run quotacheck\n");
1401 fprintf(stderr, "%s %s ", obd_type,
1402 obd_uuid2str(&qctl.obd_uuid));
1403 fprintf(stderr, "%s failed: %s\n", argv[0],
1411 static int lfs_quotaoff(int argc, char **argv)
1415 struct if_quotactl qctl;
1416 char *obd_type = (char *)qctl.obd_type;
1419 memset(&qctl, 0, sizeof(qctl));
1420 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1423 while ((c = getopt(argc, argv, "ug")) != -1) {
1426 qctl.qc_type |= 0x01;
1429 qctl.qc_type |= 0x02;
1432 fprintf(stderr, "error: %s: option '-%c' "
1433 "unrecognized\n", argv[0], c);
1440 else /* by default, disable quota for both user & group */
1441 qctl.qc_type = 0x02;
1448 rc = llapi_quotactl(mnt, &qctl);
1450 if (errno == EALREADY) {
1451 fprintf(stderr, "\n%s quotas are disabled already.\n",
1452 qctl.qc_type == 0x02 ? "user/group" :
1453 (qctl.qc_type == 0x00 ? "user" : "group"));
1457 fprintf(stderr, "%s %s ", obd_type,
1458 obd_uuid2str(&qctl.obd_uuid));
1459 fprintf(stderr, "quotaoff failed: %s\n",
1467 static int lfs_quotainv(int argc, char **argv)
1471 struct if_quotactl qctl;
1474 memset(&qctl, 0, sizeof(qctl));
1475 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1478 while ((c = getopt(argc, argv, "ugf")) != -1) {
1481 qctl.qc_type |= 0x01;
1484 qctl.qc_type |= 0x02;
1487 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1490 fprintf(stderr, "error: %s: option '-%c' "
1491 "unrecognized\n", argv[0], c);
1498 else /* by default, invalidate quota for both user & group */
1499 qctl.qc_type = 0x02;
1506 rc = llapi_quotactl(mnt, &qctl);
1508 fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
1515 #define ARG2INT(nr, str, msg) \
1518 nr = strtol(str, &endp, 0); \
1520 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1525 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1527 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1528 * returns the value or ULONG_MAX on integer overflow or incorrect format
1530 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1531 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1532 * 3. empty integer value is interpreted as 0
1535 static unsigned long str2sec(const char* timestr) {
1536 const char spec[] = "smhdw";
1537 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1538 unsigned long val = 0;
1541 if (strpbrk(timestr, spec) == NULL) {
1542 /* no specifiers inside the time string,
1543 should treat it as an integer value */
1544 val = strtoul(timestr, &tail, 10);
1545 return *tail ? ULONG_MAX : val;
1548 /* format string is XXwXXdXXhXXmXXs */
1554 v = strtoul(timestr, &tail, 10);
1555 if (v == ULONG_MAX || *tail == '\0')
1556 /* value too large (ULONG_MAX or more)
1557 or missing specifier */
1560 ptr = strchr(spec, *tail);
1562 /* unknown specifier */
1567 /* check if product will overflow the type */
1568 if (!(v < ULONG_MAX / mult[ind]))
1571 ADD_OVERFLOW(val, mult[ind] * v);
1572 if (val == ULONG_MAX)
1584 #define ARG2ULL(nr, str, defscale) \
1586 unsigned long long limit, units = 0; \
1589 rc = parse_size(str, &limit, &units, 1); \
1591 fprintf(stderr, "error: bad limit value %s\n", str); \
1594 nr = ((units == 0) ? (defscale) : 1) * limit; \
1597 static inline int has_times_option(int argc, char **argv)
1601 for (i = 1; i < argc; i++)
1602 if (!strcmp(argv[i], "-t"))
1608 int lfs_setquota_times(int argc, char **argv)
1611 struct if_quotactl qctl;
1612 char *mnt, *obd_type = (char *)qctl.obd_type;
1613 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1614 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1615 struct option long_opts[] = {
1616 {"user", no_argument, 0, 'u'},
1617 {"group", no_argument, 0, 'g'},
1618 {"block-grace", required_argument, 0, 'b'},
1619 {"inode-grace", required_argument, 0, 'i'},
1620 {"times", no_argument, 0, 't'},
1624 memset(&qctl, 0, sizeof(qctl));
1625 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1626 qctl.qc_type = UGQUOTA;
1629 while ((c = getopt_long(argc, argv, "ugb:i:t", long_opts, NULL)) != -1) {
1633 if (qctl.qc_type != UGQUOTA) {
1634 fprintf(stderr, "error: -u and -g can't be used "
1635 "more than once\n");
1638 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1641 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1642 fprintf(stderr, "error: bad block-grace: %s\n",
1646 dqb->dqb_valid |= QIF_BTIME;
1649 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1650 fprintf(stderr, "error: bad inode-grace: %s\n",
1654 dqb->dqb_valid |= QIF_ITIME;
1656 case 't': /* Yes, of course! */
1658 default: /* getopt prints error message for us when opterr != 0 */
1663 if (qctl.qc_type == UGQUOTA) {
1664 fprintf(stderr, "error: neither -u nor -g specified\n");
1668 if (optind != argc - 1) {
1669 fprintf(stderr, "error: unexpected parameters encountered\n");
1674 rc = llapi_quotactl(mnt, &qctl);
1677 fprintf(stderr, "%s %s ", obd_type,
1678 obd_uuid2str(&qctl.obd_uuid));
1679 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1686 #define BSLIMIT (1 << 0)
1687 #define BHLIMIT (1 << 1)
1688 #define ISLIMIT (1 << 2)
1689 #define IHLIMIT (1 << 3)
1691 int lfs_setquota(int argc, char **argv)
1694 struct if_quotactl qctl;
1695 char *mnt, *obd_type = (char *)qctl.obd_type;
1696 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1697 struct option long_opts[] = {
1698 {"user", required_argument, 0, 'u'},
1699 {"group", required_argument, 0, 'g'},
1700 {"block-softlimit", required_argument, 0, 'b'},
1701 {"block-hardlimit", required_argument, 0, 'B'},
1702 {"inode-softlimit", required_argument, 0, 'i'},
1703 {"inode-hardlimit", required_argument, 0, 'I'},
1706 unsigned limit_mask = 0;
1709 if (has_times_option(argc, argv))
1710 return lfs_setquota_times(argc, argv);
1712 memset(&qctl, 0, sizeof(qctl));
1713 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1714 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1715 * so it can be used as a marker that qc_type
1716 * isn't reinitialized from command line */
1719 while ((c = getopt_long(argc, argv, "u:g:b:B:i:I:", long_opts, NULL)) != -1) {
1723 if (qctl.qc_type != UGQUOTA) {
1724 fprintf(stderr, "error: -u and -g can't be used"
1725 " more than once\n");
1728 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1729 rc = name2id(&qctl.qc_id, optarg,
1730 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1732 qctl.qc_id = strtoul(optarg, &endptr, 10);
1733 if (*endptr != '\0') {
1734 fprintf(stderr, "error: can't find id "
1735 "for name %s\n", optarg);
1741 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1742 dqb->dqb_bsoftlimit >>= 10;
1743 limit_mask |= BSLIMIT;
1746 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1747 dqb->dqb_bhardlimit >>= 10;
1748 limit_mask |= BHLIMIT;
1751 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1752 limit_mask |= ISLIMIT;
1755 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1756 limit_mask |= IHLIMIT;
1758 default: /* getopt prints error message for us when opterr != 0 */
1763 if (qctl.qc_type == UGQUOTA) {
1764 fprintf(stderr, "error: neither -u nor -g was specified\n");
1768 if (limit_mask == 0) {
1769 fprintf(stderr, "error: at least one limit must be specified\n");
1773 if (optind != argc - 1) {
1774 fprintf(stderr, "error: unexpected parameters encountered\n");
1780 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1781 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1782 /* sigh, we can't just set blimits/ilimits */
1783 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1784 .qc_type = qctl.qc_type,
1785 .qc_id = qctl.qc_id};
1787 rc = llapi_quotactl(mnt, &tmp_qctl);
1789 fprintf(stderr, "error: setquota failed while retrieving"
1790 " current quota settings (%s)\n",
1795 if (!(limit_mask & BHLIMIT))
1796 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1797 if (!(limit_mask & BSLIMIT))
1798 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1799 if (!(limit_mask & IHLIMIT))
1800 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1801 if (!(limit_mask & ISLIMIT))
1802 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1804 /* Keep grace times if we have got no softlimit arguments */
1805 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
1806 dqb->dqb_valid |= QIF_BTIME;
1807 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
1810 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
1811 dqb->dqb_valid |= QIF_ITIME;
1812 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
1816 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1817 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1819 rc = llapi_quotactl(mnt, &qctl);
1822 fprintf(stderr, "%s %s ", obd_type,
1823 obd_uuid2str(&qctl.obd_uuid));
1824 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1831 static inline char *type2name(int check_type)
1833 if (check_type == USRQUOTA)
1835 else if (check_type == GRPQUOTA)
1842 /* Converts seconds value into format string
1843 * result is returned in buf
1845 * 1. result is in descenting order: 1w2d3h4m5s
1846 * 2. zero fields are not filled (except for p. 3): 5d1s
1847 * 3. zero seconds value is presented as "0s"
1849 static void sec2str(time_t seconds, char *buf)
1851 const char spec[] = "smhdw";
1852 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1857 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1858 c = seconds / mult[i];
1860 if (c > 0 || (i == 0 && buf == tail))
1861 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1868 static void diff2str(time_t seconds, char *buf, time_t now)
1874 if (seconds <= now) {
1875 strcpy(buf, "none");
1878 sec2str(seconds - now, buf);
1881 static void print_quota_title(char *name, struct if_quotactl *qctl)
1883 printf("Disk quotas for %s %s (%cid %u):\n",
1884 type2name(qctl->qc_type), name,
1885 *type2name(qctl->qc_type), qctl->qc_id);
1886 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1888 "kbytes", "quota", "limit", "grace",
1889 "files", "quota", "limit", "grace");
1892 static void print_quota(char *mnt, struct if_quotactl *qctl, int type, int rc)
1898 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1899 int bover = 0, iover = 0;
1900 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1902 if (dqb->dqb_bhardlimit &&
1903 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1905 } else if (dqb->dqb_bsoftlimit &&
1906 toqb(dqb->dqb_curspace) >= dqb->dqb_bsoftlimit) {
1907 if (dqb->dqb_btime > now) {
1914 if (dqb->dqb_ihardlimit &&
1915 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1917 } else if (dqb->dqb_isoftlimit &&
1918 dqb->dqb_curinodes >= dqb->dqb_isoftlimit) {
1919 if (dqb->dqb_btime > now) {
1926 #if 0 /* XXX: always print quotas even when no usages */
1927 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1933 if (strlen(mnt) > 15)
1934 printf("%s\n%15s", mnt, "");
1936 printf("%15s", mnt);
1939 diff2str(dqb->dqb_btime, timebuf, now);
1940 if (rc == -1 && errno == EREMOTEIO)
1941 sprintf(numbuf[0], LPU64"*",
1942 toqb(dqb->dqb_curspace));
1945 (dqb->dqb_valid & QIF_SPACE) ?
1946 LPU64 : "["LPU64"]",
1947 toqb(dqb->dqb_curspace));
1948 if (type == QC_GENERAL)
1949 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
1950 ? LPU64 : "["LPU64"]",
1951 dqb->dqb_bsoftlimit);
1953 sprintf(numbuf[1], "%s", "");
1954 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
1955 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
1956 printf(" %7s%c %6s %7s %7s",
1957 numbuf[0], bover ? '*' : ' ', numbuf[1],
1958 numbuf[2], bover > 1 ? timebuf : "");
1961 diff2str(dqb->dqb_itime, timebuf, now);
1963 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
1964 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
1965 if (type == QC_GENERAL)
1966 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
1967 ? LPU64 : "["LPU64"]",
1968 dqb->dqb_isoftlimit);
1970 sprintf(numbuf[1], "%s", "");
1971 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
1972 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
1973 if (type != QC_OSTIDX)
1974 printf(" %7s%c %6s %7s %7s",
1975 numbuf[0], iover ? '*' : ' ', numbuf[1],
1976 numbuf[2], iover > 1 ? timebuf : "");
1979 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1980 qctl->qc_cmd == Q_GETOINFO) {
1984 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1985 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1986 printf("Block grace time: %s; Inode grace time: %s\n",
1987 bgtimebuf, igtimebuf);
1991 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
1993 int rc = 0, rc1 = 0, count = 0;
1994 __u32 valid = qctl->qc_valid;
1996 rc = llapi_get_obd_count(mnt, &count, is_mdt);
1998 fprintf(stderr, "can not get %s count: %s\n",
1999 is_mdt ? "mdt": "ost", strerror(errno));
2003 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2004 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2005 rc = llapi_quotactl(mnt, qctl);
2007 /* It is remote client case. */
2008 if (errno == EOPNOTSUPP) {
2015 fprintf(stderr, "quotactl %s%d failed.\n",
2016 is_mdt ? "mdt": "ost", qctl->qc_idx);
2020 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid, 0);
2024 qctl->qc_valid = valid;
2028 static int lfs_quota(int argc, char **argv)
2031 char *mnt, *name = NULL;
2032 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2033 .qc_type = UGQUOTA };
2034 char *obd_type = (char *)qctl.obd_type;
2035 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2036 int rc, rc1 = 0, rc2 = 0, rc3 = 0, verbose = 0, pass = 0;
2038 __u32 valid = QC_GENERAL, idx = 0;
2041 while ((c = getopt(argc, argv, "ugto:i:I:v")) != -1) {
2044 if (qctl.qc_type != UGQUOTA) {
2045 fprintf(stderr, "error: use either -u or -g\n");
2048 qctl.qc_type = USRQUOTA;
2051 if (qctl.qc_type != UGQUOTA) {
2052 fprintf(stderr, "error: use either -u or -g\n");
2055 qctl.qc_type = GRPQUOTA;
2058 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2061 valid = qctl.qc_valid = QC_UUID;
2062 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2065 valid = qctl.qc_valid = QC_MDTIDX;
2066 idx = qctl.qc_idx = atoi(optarg);
2069 valid = qctl.qc_valid = QC_OSTIDX;
2070 idx = qctl.qc_idx = atoi(optarg);
2076 fprintf(stderr, "error: %s: option '-%c' "
2077 "unrecognized\n", argv[0], c);
2082 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2083 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2084 optind == argc - 1) {
2086 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2087 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2088 qctl.qc_valid = valid;
2091 qctl.qc_type = USRQUOTA;
2092 qctl.qc_id = geteuid();
2094 qctl.qc_type = GRPQUOTA;
2095 qctl.qc_id = getegid();
2097 rc = id2name(&name, qctl.qc_id,
2098 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2101 /* lfs quota -u username /path/to/lustre/mount */
2102 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2103 /* options should be followed by u/g-name and mntpoint */
2104 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2105 fprintf(stderr, "error: missing quota argument(s)\n");
2109 name = argv[optind++];
2110 rc = name2id(&qctl.qc_id, name,
2111 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2113 qctl.qc_id = strtoul(name, &endptr, 10);
2114 if (*endptr != '\0') {
2115 fprintf(stderr, "error: can't find id for name "
2120 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2121 fprintf(stderr, "error: missing quota info argument(s)\n");
2125 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA)
2126 print_quota_title(name, &qctl);
2130 rc1 = llapi_quotactl(mnt, &qctl);
2131 if (rc1 == -1 && errno == EALREADY) {
2132 fprintf(stderr, "\n%s quotas are not enabled.\n",
2133 qctl.qc_type == USRQUOTA ? "user" : "group");
2136 if (rc1 && *obd_type)
2137 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2139 if (qctl.qc_valid != QC_GENERAL)
2142 print_quota(mnt, &qctl, QC_GENERAL, rc1);
2144 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2145 rc2 = print_obd_quota(mnt, &qctl, 1);
2146 rc3 = print_obd_quota(mnt, &qctl, 0);
2149 if (rc1 || rc2 || rc3)
2150 printf("Some errors happened when getting quota info. "
2151 "Some devices may be not working or deactivated. "
2152 "The data in \"[]\" is inaccurate.\n");
2160 #endif /* HAVE_SYS_QUOTA_H! */
2162 static int flushctx_ioctl(char *mp)
2166 fd = open(mp, O_RDONLY);
2168 fprintf(stderr, "flushctx: error open %s: %s\n",
2169 mp, strerror(errno));
2173 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2175 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2176 mp, strerror(errno));
2182 static int lfs_flushctx(int argc, char **argv)
2184 int kdestroy = 0, c;
2186 char procline[PATH_MAX], *line;
2190 while ((c = getopt(argc, argv, "k")) != -1) {
2196 fprintf(stderr, "error: %s: option '-%c' "
2197 "unrecognized\n", argv[0], c);
2204 if ((rc = system("kdestroy > /dev/null")) != 0) {
2205 rc = WEXITSTATUS(rc);
2206 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
2210 if (optind >= argc) {
2211 /* flush for all mounted lustre fs. */
2212 proc = fopen("/proc/mounts", "r");
2214 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2219 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2224 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2225 fprintf(stderr, "%s: unexpected format in "
2231 if (strcmp(fs, "lustre") != 0)
2233 /* we use '@' to determine it's a client. are there
2234 * any other better way?
2236 if (strchr(dev, '@') == NULL)
2239 if (flushctx_ioctl(mp))
2243 /* flush fs as specified */
2244 while (optind < argc) {
2245 if (flushctx_ioctl(argv[optind++]))
2253 static int lfs_lsetfacl(int argc, char **argv)
2256 return(llapi_lsetfacl(argc, argv));
2259 static int lfs_lgetfacl(int argc, char **argv)
2262 return(llapi_lgetfacl(argc, argv));
2265 static int lfs_rsetfacl(int argc, char **argv)
2268 return(llapi_rsetfacl(argc, argv));
2271 static int lfs_rgetfacl(int argc, char **argv)
2274 return(llapi_rgetfacl(argc, argv));
2277 static int lfs_cp(int argc, char **argv)
2279 return(llapi_cp(argc, argv));
2282 static int lfs_ls(int argc, char **argv)
2284 return(llapi_ls(argc, argv));
2287 static int lfs_changelog(int argc, char **argv)
2289 void *changelog_priv;
2290 struct changelog_rec *rec;
2291 long long startrec = 0, endrec = 0;
2293 struct option long_opts[] = {
2294 {"follow", no_argument, 0, 'f'},
2297 char short_opts[] = "f";
2301 while ((rc = getopt_long(argc, argv, short_opts,
2302 long_opts, NULL)) != -1) {
2310 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2311 argv[0], argv[optind - 1]);
2318 mdd = argv[optind++];
2320 startrec = strtoll(argv[optind++], NULL, 10);
2322 endrec = strtoll(argv[optind++], NULL, 10);
2324 rc = llapi_changelog_start(&changelog_priv,
2325 CHANGELOG_FLAG_BLOCK |
2326 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
2329 fprintf(stderr, "Can't start changelog: %s\n",
2330 strerror(errno = -rc));
2334 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
2338 if (endrec && rec->cr_index > endrec)
2340 if (rec->cr_index < startrec)
2343 secs = rec->cr_time >> 30;
2344 gmtime_r(&secs, &ts);
2345 printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
2346 "0x%x t="DFID, rec->cr_index, rec->cr_type,
2347 changelog_type2str(rec->cr_type),
2348 ts.tm_hour, ts.tm_min, ts.tm_sec,
2349 (int)(rec->cr_time & ((1<<30) - 1)),
2350 ts.tm_year+1900, ts.tm_mon+1, ts.tm_mday,
2351 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
2352 if (rec->cr_namelen)
2353 /* namespace rec includes parent and filename */
2354 printf(" p="DFID" %.*s\n", PFID(&rec->cr_pfid),
2355 rec->cr_namelen, rec->cr_name);
2359 llapi_changelog_free(&rec);
2362 llapi_changelog_fini(&changelog_priv);
2365 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
2367 return (rc == 1 ? 0 : rc);
2370 static int lfs_changelog_clear(int argc, char **argv)
2378 endrec = strtoll(argv[3], NULL, 10);
2380 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
2382 fprintf(stderr, "%s error: %s\n", argv[0],
2383 strerror(errno = -rc));
2387 static int lfs_fid2path(int argc, char **argv)
2389 struct option long_opts[] = {
2390 {"cur", no_argument, 0, 'c'},
2391 {"link", required_argument, 0, 'l'},
2392 {"rec", required_argument, 0, 'r'},
2395 char short_opts[] = "cl:r:";
2396 char *device, *fid, *path;
2397 long long recno = -1;
2405 while ((rc = getopt_long(argc, argv, short_opts,
2406 long_opts, NULL)) != -1) {
2412 linkno = strtol(optarg, NULL, 10);
2415 recno = strtoll(optarg, NULL, 10);
2420 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2421 argv[0], argv[optind - 1]);
2425 device = argv[optind++];
2426 fid = argv[optind++];
2430 path = calloc(1, PATH_MAX);
2432 lnktmp = (linkno >= 0) ? linkno : 0;
2434 int oldtmp = lnktmp;
2435 long long rectmp = recno;
2436 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2439 fprintf(stderr, "%s error: %s\n", argv[0],
2440 strerror(errno = -rc));
2445 fprintf(stdout, "%lld %s\n", rectmp, path);
2447 fprintf(stdout, "%s\n", path);
2450 /* specified linkno */
2452 if (oldtmp == lnktmp)
2461 static int lfs_path2fid(int argc, char **argv)
2471 rc = llapi_path2fid(path, &fid);
2473 fprintf(stderr, "can't get fid for %s: %s\n", path,
2474 strerror(errno = -rc));
2478 printf(DFID"\n", PFID(&fid));
2483 int main(int argc, char **argv)
2489 ptl_initialize(argc, argv);
2490 if (obd_initialize(argc, argv) < 0)
2492 if (dbg_initialize(argc, argv) < 0)
2495 Parser_init("lfs > ", cmdlist);
2498 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2500 rc = Parser_commands();
2503 obd_finalize(argc, argv);
2504 return rc < 0 ? -rc : rc;