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 (c) 2003, 2010, Oracle and/or its affiliates. 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_getname(int argc, char **argv);
90 static int lfs_check(int argc, char **argv);
91 static int lfs_catinfo(int argc, char **argv);
92 #ifdef HAVE_SYS_QUOTA_H
93 static int lfs_quotachown(int argc, char **argv);
94 static int lfs_quotacheck(int argc, char **argv);
95 static int lfs_quotaon(int argc, char **argv);
96 static int lfs_quotaoff(int argc, char **argv);
97 static int lfs_setquota(int argc, char **argv);
98 static int lfs_quota(int argc, char **argv);
99 static int lfs_quotainv(int argc, char **argv);
101 static int lfs_flushctx(int argc, char **argv);
102 static int lfs_join(int argc, char **argv);
103 static int lfs_lsetfacl(int argc, char **argv);
104 static int lfs_lgetfacl(int argc, char **argv);
105 static int lfs_rsetfacl(int argc, char **argv);
106 static int lfs_rgetfacl(int argc, char **argv);
107 static int lfs_cp(int argc, char **argv);
108 static int lfs_ls(int argc, char **argv);
109 static int lfs_poollist(int argc, char **argv);
110 static int lfs_changelog(int argc, char **argv);
111 static int lfs_changelog_clear(int argc, char **argv);
112 static int lfs_fid2path(int argc, char **argv);
113 static int lfs_path2fid(int argc, char **argv);
115 /* all avaialable commands */
116 command_t cmdlist[] = {
117 {"setstripe", lfs_setstripe, 0,
118 "Create a new file with a specific striping pattern or\n"
119 "set the default striping pattern on an existing directory or\n"
120 "delete the default striping pattern from an existing directory\n"
121 "usage: setstripe [--size|-s stripe_size] [--count|-c stripe_count]\n"
122 " [--index|-i|--offset|-o start_ost_index]\n"
123 " [--pool|-p <pool>] <directory|filename>\n"
125 " setstripe -d <directory> (to delete default striping)\n"
126 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
127 "\t Can be specified with k, m or g (in KB, MB and GB\n"
129 "\tstart_ost_index: OST index of first stripe (-1 default)\n"
130 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"
131 "\tpool: Name of OST pool to use (default none)"},
132 {"getstripe", lfs_getstripe, 0,
133 "To list the striping info for a given file or files in a\n"
134 "directory or recursively for all files in a directory tree.\n"
135 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
136 " [--count | -c ] [--index | -i | --offset | -o]\n"
137 " [--size | -s ] [--pool | -p ] [--directory | -d]\n"
138 " [--mdt | -M] [--recursive | -r] [--raw | -R]\n"
139 " <directory|filename> ..."},
140 {"pool_list", lfs_poollist, 0,
141 "List pools or pool OSTs\n"
142 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
143 {"find", lfs_find, 0,
144 "To find files that match given parameters recursively in a directory tree.\n"
145 "usage: find <directory|filename> ...\n"
146 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
147 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
148 " [--print|-p] [[!] --obd|-O <uuid[s]>] [[!] --size|-s [+-]N[bkMGTP]]\n"
149 " [[!] --type|-t <filetype>] [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
150 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
151 " [[!] --pool <pool>]\n"
152 "\t !: used before an option indicates 'NOT' the requested attribute\n"
153 "\t -: used before an value indicates 'AT MOST' the requested value\n"
154 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
155 {"check", lfs_check, 0,
156 "Display the status of MDS or OSTs (as specified in the command)\n"
157 "or all the servers (MDS and OSTs).\n"
158 "usage: check <osts|mds|servers>"},
159 {"catinfo", lfs_catinfo, 0,
160 "Show information of specified type logs.\n"
161 "usage: catinfo {keyword} [node name]\n"
162 "\tkeywords are one of followings: config, deletions.\n"
163 "\tnode name must be provided when use keyword config."},
164 {"join", lfs_join, 0,
165 "join two lustre files into one.\n"
166 "obsolete, HEAD does not support it anymore.\n"},
167 {"osts", lfs_osts, 0, "list OSTs connected to client "
168 "[for specified path only]\n" "usage: osts [path]"},
170 "report filesystem disk space usage or inodes usage"
171 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
172 "Usage: df [-i] [-h] [--pool|-p <fsname>[.<pool>] [path]"},
173 {"getname", lfs_getname, 0, "list instances and specified mount points "
174 "[for specified path only]\n"
175 "Usage: getname [-h]|[path ...] "},
176 #ifdef HAVE_SYS_QUOTA_H
177 {"quotachown",lfs_quotachown, 0,
178 "Change files' owner or group on the specified filesystem.\n"
179 "usage: quotachown [-i] <filesystem>\n"
180 "\t-i: ignore error if file is not exist\n"},
181 {"quotacheck", lfs_quotacheck, 0,
182 "Scan the specified filesystem for disk usage, and create,\n"
183 "or update quota files.\n"
184 "usage: quotacheck [ -ug ] <filesystem>"},
185 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
186 "usage: quotaon [ -ugf ] <filesystem>"},
187 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
188 "usage: quotaoff [ -ug ] <filesystem>"},
189 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
190 "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
191 " -b <block-softlimit> -B <block-hardlimit>\n"
192 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
193 " setquota -t <-u|-g> <block-grace> <inode-grace> <filesystem>\n"
194 " setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
195 " [--block-softlimit <block-softlimit>]\n"
196 " [--block-hardlimit <block-hardlimit>]\n"
197 " [--inode-softlimit <inode-softlimit>]\n"
198 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
199 " setquota [-t] <-u|--user|-g|--group>\n"
200 " [--block-grace <block-grace>]\n"
201 " [--inode-grace <inode-grace>] <filesystem>\n"
202 " -b can be used instead of --block-softlimit/--block-grace\n"
203 " -B can be used instead of --block-hardlimit\n"
204 " -i can be used instead of --inode-softlimit/--inode-grace\n"
205 " -I can be used instead of --inode-hardlimit"},
206 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
207 "usage: quota [-q] [-v] [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>]\n"
208 " [<-u|-g> <uname>|<uid>|<gname>|<gid>] <filesystem>\n"
209 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g> <filesystem>"},
210 {"quotainv", lfs_quotainv, 0, "Invalidate quota data.\n"
211 "usage: quotainv [-u|-g] <filesystem>"},
213 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
214 "usage: flushctx [-k] [mountpoint...]"},
215 {"lsetfacl", lfs_lsetfacl, 0,
216 "Remote user setfacl for user/group on the same remote client.\n"
217 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
218 {"lgetfacl", lfs_lgetfacl, 0,
219 "Remote user getfacl for user/group on the same remote client.\n"
220 "usage: lgetfacl [-dRLPvh] file ..."},
221 {"rsetfacl", lfs_rsetfacl, 0,
222 "Remote user setfacl for user/group on other clients.\n"
223 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
224 {"rgetfacl", lfs_rgetfacl, 0,
225 "Remote user getfacl for user/group on other clients.\n"
226 "usage: rgetfacl [-dRLPvh] file ..."},
228 "Remote user copy files and directories.\n"
229 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
231 "Remote user list directory contents.\n"
232 "usage: ls [OPTION]... [FILE]..."},
233 {"changelog", lfs_changelog, 0,
234 "Show the metadata changes on an MDT."
235 "\nusage: changelog <mdtname> [startrec [endrec]]"},
236 {"changelog_clear", lfs_changelog_clear, 0,
237 "Indicate that old changelog records up to <endrec> are no longer of "
238 "interest to consumer <id>, allowing the system to free up space.\n"
239 "An <endrec> of 0 means all records.\n"
240 "usage: changelog_clear <mdtname> <id> <endrec>"},
241 {"fid2path", lfs_fid2path, 0,
242 "Resolve the full path to a given FID. For a specific hardlink "
243 "specify link number <linkno>.\n"
244 /* "For a historical name, specify changelog record <recno>.\n" */
245 "usage: fid2path <fsname|rootpath> <fid> [--link <linkno>]"
246 /*[--rec <recno>]*/},
247 {"path2fid", lfs_path2fid, 0, "Display the fid for a given path.\n"
248 "usage: path2fid <path>"},
249 {"help", Parser_help, 0, "help"},
250 {"exit", Parser_quit, 0, "quit"},
251 {"quit", Parser_quit, 0, "quit"},
255 static int isnumber(const char *str)
259 if (str[0] != '-' && !isdigit(str[0]))
262 for (ptr = str + 1; *ptr != '\0'; ptr++) {
271 static int lfs_setstripe(int argc, char **argv)
275 unsigned long long st_size;
276 int st_offset, st_count;
280 char *stripe_size_arg = NULL;
281 char *stripe_off_arg = NULL;
282 char *stripe_count_arg = NULL;
283 char *pool_name_arg = NULL;
284 unsigned long long size_units = 1;
286 struct option long_opts[] = {
287 {"count", required_argument, 0, 'c'},
288 {"delete", no_argument, 0, 'd'},
289 {"index", required_argument, 0, 'i'},
290 {"offset", required_argument, 0, 'o'},
291 {"pool", required_argument, 0, 'p'},
292 {"size", required_argument, 0, 's'},
300 #if LUSTRE_VERSION < OBD_OCD_VERSION(2,4,50,0)
301 if (argc == 5 && argv[1][0] != '-' &&
302 isnumber(argv[2]) && isnumber(argv[3]) && isnumber(argv[4])) {
303 fprintf(stderr, "error: obsolete usage of setstripe "
304 "positional parameters. Use -c, -i, -s instead.\n");
308 #warning "remove obsolete positional parameter code"
312 while ((c = getopt_long(argc, argv, "c:di:o:p:s:",
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 = strtol(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, .size_units = 0 };
502 struct option long_opts[] = {
503 {"atime", required_argument, 0, 'A'},
504 {"ctime", required_argument, 0, 'C'},
505 {"maxdepth", required_argument, 0, 'D'},
506 {"gid", required_argument, 0, 'g'},
507 {"group", required_argument, 0, 'G'},
508 {"mtime", required_argument, 0, 'M'},
509 {"name", required_argument, 0, 'n'},
510 /* --obd is considered as a new option. */
511 {"obd", required_argument, 0, 'O'},
512 {"ost", required_argument, 0, 'O'},
513 /* no short option for pool, p/P already used */
514 {"pool", required_argument, 0, FIND_POOL_OPT},
515 {"print0", no_argument, 0, 'p'},
516 {"print", no_argument, 0, 'P'},
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 {"uid", required_argument, 0, 'u'},
522 {"user", required_argument, 0, 'U'},
523 {"verbose", no_argument, 0, 'v'},
537 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
538 while ((c = getopt_long_only(argc,argv,"-A:C:D:g:G:M:n:O:Ppqrs:t:u:U:v",
539 long_opts, NULL)) >= 0) {
544 /* '!' is part of option */
545 /* when getopt_long_only() finds a string which is not
546 * an option nor a known option argument it returns 1
547 * in that case if we already have found pathstart and pathend
548 * (i.e. we have the list of pathnames),
549 * the only supported value is "!"
551 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
552 if (!isoption && pathend != -1) {
553 fprintf(stderr, "err: %s: filename|dirname must either "
554 "precede options or follow options\n",
558 if (!isoption && pathstart == -1)
559 pathstart = optind - 1;
560 if (isoption && pathstart != -1 && pathend == -1) {
561 pathend = optind - 2;
562 if ((c == 1 && strcmp(optarg, "!") == 0) ||
563 c == 'P' || c == 'p' || c == 'O' ||
564 c == 'q' || c == 'r' || c == 'v')
565 pathend = optind - 1;
572 /* unknown; opt is "!" or path component,
573 * checking done above.
575 if (strcmp(optarg, "!") == 0)
579 xtime = ¶m.atime;
580 xsign = ¶m.asign;
581 param.exclude_atime = !!neg_opt;
584 xtime = ¶m.ctime;
585 xsign = ¶m.csign;
586 param.exclude_ctime = !!neg_opt;
590 xtime = ¶m.mtime;
591 xsign = ¶m.msign;
592 param.exclude_mtime = !!neg_opt;
595 ret = set_time(&t, xtime, optarg);
603 param.maxdepth = strtol(optarg, 0, 0);
608 ret = name2id(¶m.gid, optarg, GROUP);
610 param.gid = strtoul(optarg, &endptr, 10);
611 if (*endptr != '\0') {
612 fprintf(stderr, "Group/GID: %s cannot "
613 "be found.\n", optarg);
617 param.exclude_gid = !!neg_opt;
623 ret = name2id(¶m.uid, optarg, USER);
625 param.uid = strtoul(optarg, &endptr, 10);
626 if (*endptr != '\0') {
627 fprintf(stderr, "User/UID: %s cannot "
628 "be found.\n", optarg);
632 param.exclude_uid = !!neg_opt;
637 if (strlen(optarg) > LOV_MAXPOOLNAME) {
639 "Pool name %s is too long"
640 " (max is %d)\n", optarg,
644 /* we do check for empty pool because empty pool
645 * is used to find V1 lov attributes */
646 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
647 param.poolname[LOV_MAXPOOLNAME] = '\0';
648 param.exclude_pool = !!neg_opt;
649 param.check_pool = 1;
653 param.pattern = (char *)optarg;
654 param.exclude_pattern = !!neg_opt;
657 char *buf, *token, *next, *p;
660 len = strlen((char *)optarg);
664 strcpy(buf, (char *)optarg);
666 param.exclude_obd = !!neg_opt;
668 if (param.num_alloc_obds == 0) {
669 param.obduuid = malloc(FIND_MAX_OSTS *
670 sizeof(struct obd_uuid));
671 if (param.obduuid == NULL)
673 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
676 for (token = buf; token && *token; token = next) {
677 p = strchr(token, ',');
683 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
707 param.exclude_type = !!neg_opt;
709 case 'b': param.type = S_IFBLK; break;
710 case 'c': param.type = S_IFCHR; break;
711 case 'd': param.type = S_IFDIR; break;
712 case 'f': param.type = S_IFREG; break;
713 case 'l': param.type = S_IFLNK; break;
714 case 'p': param.type = S_IFIFO; break;
715 case 's': param.type = S_IFSOCK; break;
716 #ifdef S_IFDOOR /* Solaris only */
717 case 'D': param.type = S_IFDOOR; break;
719 default: fprintf(stderr, "error: %s: bad type '%s'\n",
725 if (optarg[0] == '+')
726 param.size_sign = -1;
727 else if (optarg[0] == '-')
728 param.size_sign = +1;
732 ret = parse_size(optarg, ¶m.size,
733 ¶m.size_units, 0);
735 fprintf(stderr,"error: bad size '%s'\n",
739 param.check_size = 1;
740 param.exclude_size = !!neg_opt;
750 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
751 argv[0], argv[optind - 1]);
756 if (pathstart == -1) {
757 fprintf(stderr, "error: %s: no filename|pathname\n",
760 } else if (pathend == -1) {
768 static int deprecated_warning;
769 if (!deprecated_warning) {
770 fprintf(stderr, "lfs find: -q, -r, -v options "
771 "deprecated. Use 'lfs getstripe' instead.\n");
772 deprecated_warning = 1;
774 if (!param.recursive && param.maxdepth == -1)
780 ret = llapi_find(argv[pathstart], ¶m);
782 ret = llapi_getstripe(argv[pathstart], ¶m);
783 } while (++pathstart < pathend && !ret);
786 fprintf(stderr, "error: %s failed for %s.\n",
787 argv[0], argv[optind - 1]);
789 if (param.obduuid && param.num_alloc_obds)
795 static int lfs_getstripe(int argc, char **argv)
797 struct option long_opts[] = {
798 {"count", 0, 0, 'c'},
799 {"directory", 0, 0, 'd'},
800 {"index", 0, 0, 'i'},
802 {"offset", 0, 0, 'o'},
805 {"quiet", 0, 0, 'q'},
806 {"recursive", 0, 0, 'r'},
809 {"verbose", 0, 0, 'v'},
813 struct find_param param = { 0 };
817 while ((c = getopt_long(argc, argv, "cdhiMoO:pqrRsv",
818 long_opts, NULL)) != -1) {
823 "error: %s: only one obduuid allowed",
827 param.obduuid = (struct obd_uuid *)optarg;
839 param.verbose = VERBOSE_ALL | VERBOSE_DETAIL;
842 if (!(param.verbose & VERBOSE_DETAIL)) {
843 param.verbose |= VERBOSE_COUNT;
848 if (!(param.verbose & VERBOSE_DETAIL)) {
849 param.verbose |= VERBOSE_SIZE;
855 if (!(param.verbose & VERBOSE_DETAIL)) {
856 param.verbose |= VERBOSE_OFFSET;
861 if (!(param.verbose & VERBOSE_DETAIL)) {
862 param.verbose |= VERBOSE_POOL;
867 param.get_mdt_index = 1;
875 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
876 argv[0], argv[optind - 1]);
888 param.verbose = VERBOSE_ALL;
890 param.verbose = VERBOSE_OBJID;
893 rc = llapi_getstripe(argv[optind], ¶m);
894 } while (++optind < argc && !rc);
897 fprintf(stderr, "error: %s failed for %s.\n",
898 argv[0], argv[optind - 1]);
902 static int lfs_osts(int argc, char **argv)
904 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
905 struct find_param param;
911 if (argc == 2 && !realpath(argv[1], path)) {
913 fprintf(stderr, "error: invalid path '%s': %s\n",
914 argv[1], strerror(-rc));
918 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
919 /* Check if we have a mount point */
920 if (mntdir[0] == '\0')
923 memset(¶m, 0, sizeof(param));
924 rc = llapi_ostlist(mntdir, ¶m);
926 fprintf(stderr, "error: %s: failed on %s\n",
931 memset(mntdir, 0, PATH_MAX);
937 #define COOK(value) \
940 while (value > 1024) { \
949 #define HDF "%8.1f%c"
953 static int showdf(char *mntdir, struct obd_statfs *stat,
954 char *uuid, int ishow, int cooked,
955 char *type, int index, int rc)
957 long long avail, used, total;
959 char *suffix = "KMGTPEZY";
960 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
961 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
969 avail = stat->os_ffree;
970 used = stat->os_files - stat->os_ffree;
971 total = stat->os_files;
973 int shift = cooked ? 0 : 10;
975 avail = (stat->os_bavail * stat->os_bsize) >> shift;
976 used = ((stat->os_blocks - stat->os_bfree) *
977 stat->os_bsize) >> shift;
978 total = (stat->os_blocks * stat->os_bsize) >> shift;
981 if ((used + avail) > 0)
982 ratio = (double)used / (double)(used + avail);
988 cook_val = (double)total;
991 sprintf(tbuf, HDF, cook_val, suffix[i - 1]);
993 sprintf(tbuf, CDF, total);
995 cook_val = (double)used;
998 sprintf(ubuf, HDF, cook_val, suffix[i - 1]);
1000 sprintf(ubuf, CDF, used);
1002 cook_val = (double)avail;
1005 sprintf(abuf, HDF, cook_val, suffix[i - 1]);
1007 sprintf(abuf, CDF, avail);
1009 sprintf(tbuf, CDF, total);
1010 sprintf(ubuf, CDF, used);
1011 sprintf(abuf, CDF, avail);
1014 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
1015 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1016 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1018 printf("[%s:%d]\n", type, index);
1024 printf(UUF": inactive device\n", uuid);
1027 printf(UUF": %s\n", uuid, strerror(-rc));
1034 struct ll_stat_type {
1039 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow, int cooked)
1041 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1042 struct obd_uuid uuid_buf;
1043 char *poolname = NULL;
1044 struct ll_stat_type types[] = { { LL_STATFS_MDC, "MDT" },
1045 { LL_STATFS_LOV, "OST" },
1047 struct ll_stat_type *tp;
1052 poolname = strchr(pool, '.');
1053 if (poolname != NULL) {
1054 if (strncmp(fsname, pool, strlen(fsname))) {
1055 fprintf(stderr, "filesystem name incorrect\n");
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 (tp = types; tp->st_name != NULL; tp++) {
1073 for (index = 0; ; index++) {
1074 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1075 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1076 rc = llapi_obd_statfs(mntdir, tp->st_op, index,
1077 &stat_buf, &uuid_buf);
1081 if (poolname && tp->st_op == LL_STATFS_LOV &&
1082 llapi_search_ost(fsname, poolname,
1083 obd_uuid2str(&uuid_buf)) != 1)
1086 /* the llapi_obd_statfs() call may have returned with
1087 * an error, but if it filled in uuid_buf we will at
1088 * lease use that to print out a message for that OBD.
1089 * If we didn't get anything in the uuid_buf, then fill
1090 * it in so that we can print an error message. */
1091 if (uuid_buf.uuid[0] == '\0')
1092 sprintf(uuid_buf.uuid, "%s%04x",
1093 tp->st_name, index);
1094 showdf(mntdir,&stat_buf,obd_uuid2str(&uuid_buf),
1095 ishow, cooked, tp->st_name, index, rc);
1098 if (tp->st_op == LL_STATFS_MDC) {
1099 sum.os_ffree += stat_buf.os_ffree;
1100 sum.os_files += stat_buf.os_files;
1101 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
1102 sum.os_blocks += stat_buf.os_blocks *
1104 sum.os_bfree += stat_buf.os_bfree *
1106 sum.os_bavail += stat_buf.os_bavail *
1109 } else if (rc == -EINVAL || rc == -EFAULT) {
1116 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
1121 static int lfs_df(int argc, char **argv)
1123 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1124 int ishow = 0, cooked = 0;
1125 int c, rc = 0, index = 0;
1126 char fsname[PATH_MAX] = "", *pool_name = NULL;
1127 struct option long_opts[] = {
1128 {"pool", required_argument, 0, 'p'},
1133 while ((c = getopt_long(argc, argv, "hip:", long_opts, NULL)) != -1) {
1148 if (optind < argc && !realpath(argv[optind], path)) {
1150 fprintf(stderr, "error: invalid path '%s': %s\n",
1151 argv[optind], strerror(-rc));
1155 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1156 /* Check if we have a mount point */
1157 if (mntdir[0] == '\0')
1160 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked);
1161 if (rc || path[0] != '\0')
1163 fsname[0] = '\0'; /* avoid matching in next loop */
1164 mntdir[0] = '\0'; /* avoid matching in next loop */
1170 static int lfs_getname(int argc, char **argv)
1172 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
1173 int rc = 0, index = 0, c;
1174 char buf[sizeof(struct obd_uuid)];
1177 while ((c = getopt(argc, argv, "h")) != -1)
1180 if (optind == argc) { /* no paths specified, get all paths. */
1181 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1182 rc = llapi_getname(mntdir, buf, sizeof(buf));
1185 "cannot get name for `%s': %s\n",
1186 mntdir, strerror(-rc));
1190 printf("%s %s\n", buf, mntdir);
1192 path[0] = fsname[0] = mntdir[0] = 0;
1194 } else { /* paths specified, only attempt to search these. */
1195 for (; optind < argc; optind++) {
1196 rc = llapi_getname(argv[optind], buf, sizeof(buf));
1199 "cannot get name for `%s': %s\n",
1200 argv[optind], strerror(-rc));
1204 printf("%s %s\n", buf, argv[optind]);
1210 static int lfs_check(int argc, char **argv)
1213 char mntdir[PATH_MAX] = {'\0'};
1222 obd_types[0] = obd_type1;
1223 obd_types[1] = obd_type2;
1225 if (strcmp(argv[1], "osts") == 0) {
1226 strcpy(obd_types[0], "osc");
1227 } else if (strcmp(argv[1], "mds") == 0) {
1228 strcpy(obd_types[0], "mdc");
1229 } else if (strcmp(argv[1], "servers") == 0) {
1231 strcpy(obd_types[0], "osc");
1232 strcpy(obd_types[1], "mdc");
1234 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1239 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1240 if (rc < 0 || mntdir[0] == '\0') {
1241 fprintf(stderr, "No suitable Lustre mount found\n");
1245 rc = llapi_target_iterate(num_types, obd_types,
1246 mntdir, llapi_ping_target);
1249 fprintf(stderr, "error: %s: %s status failed\n",
1256 static int lfs_catinfo(int argc, char **argv)
1258 char mntdir[PATH_MAX] = {'\0'};
1261 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1264 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1267 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
1268 if (rc == 0 && mntdir[0] != '\0') {
1270 rc = llapi_catinfo(mntdir, argv[1], argv[2]);
1272 rc = llapi_catinfo(mntdir, argv[1], NULL);
1274 fprintf(stderr, "no lustre_lite mounted.\n");
1281 static int lfs_join(int argc, char **argv)
1283 fprintf(stderr, "join two lustre files into one.\n"
1284 "obsolete, HEAD does not support it anymore.\n");
1288 #ifdef HAVE_SYS_QUOTA_H
1289 static int lfs_quotachown(int argc, char **argv)
1296 while ((c = getopt(argc, argv, "i")) != -1) {
1302 fprintf(stderr, "error: %s: option '-%c' "
1303 "unrecognized\n", argv[0], c);
1309 rc = llapi_quotachown(argv[optind], flag);
1311 fprintf(stderr,"error: change file owner/group failed.\n");
1315 static int lfs_quotacheck(int argc, char **argv)
1317 int c, check_type = 0;
1319 struct if_quotacheck qchk;
1320 struct if_quotactl qctl;
1321 char *obd_type = (char *)qchk.obd_type;
1324 memset(&qchk, 0, sizeof(qchk));
1327 while ((c = getopt(argc, argv, "gu")) != -1) {
1336 fprintf(stderr, "error: %s: option '-%c' "
1337 "unrecognized\n", argv[0], c);
1344 else /* do quotacheck for both user & group quota by default */
1352 rc = llapi_quotacheck(mnt, check_type);
1354 fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
1358 rc = llapi_poll_quotacheck(mnt, &qchk);
1361 fprintf(stderr, "%s %s ", obd_type,
1362 obd_uuid2str(&qchk.obd_uuid));
1363 fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
1367 memset(&qctl, 0, sizeof(qctl));
1368 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1369 qctl.qc_type = check_type;
1370 rc = llapi_quotactl(mnt, &qctl);
1371 if (rc && rc != -EALREADY) {
1373 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
1374 obd_uuid2str(&qctl.obd_uuid));
1375 fprintf(stderr, "%s turn on quota failed: %s\n",
1376 argv[0], strerror(-rc));
1383 static int lfs_quotaon(int argc, char **argv)
1387 struct if_quotactl qctl;
1388 char *obd_type = (char *)qctl.obd_type;
1391 memset(&qctl, 0, sizeof(qctl));
1392 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1395 while ((c = getopt(argc, argv, "fgu")) != -1) {
1398 qctl.qc_type |= 0x01;
1401 qctl.qc_type |= 0x02;
1404 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1407 fprintf(stderr, "error: %s: option '-%c' "
1408 "unrecognized\n", argv[0], c);
1415 else /* by default, enable quota for both user & group */
1416 qctl.qc_type = 0x02;
1423 rc = llapi_quotactl(mnt, &qctl);
1425 if (rc == -EALREADY) {
1427 } else if (rc == -ENOENT) {
1428 fprintf(stderr, "error: cannot find quota database, "
1429 "make sure you have run quotacheck\n");
1432 fprintf(stderr, "%s %s ", obd_type,
1433 obd_uuid2str(&qctl.obd_uuid));
1434 fprintf(stderr, "%s failed: %s\n", argv[0],
1442 static int lfs_quotaoff(int argc, char **argv)
1446 struct if_quotactl qctl;
1447 char *obd_type = (char *)qctl.obd_type;
1450 memset(&qctl, 0, sizeof(qctl));
1451 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1454 while ((c = getopt(argc, argv, "gu")) != -1) {
1457 qctl.qc_type |= 0x01;
1460 qctl.qc_type |= 0x02;
1463 fprintf(stderr, "error: %s: option '-%c' "
1464 "unrecognized\n", argv[0], c);
1471 else /* by default, disable quota for both user & group */
1472 qctl.qc_type = 0x02;
1479 rc = llapi_quotactl(mnt, &qctl);
1481 if (rc == -EALREADY) {
1485 fprintf(stderr, "%s %s ", obd_type,
1486 obd_uuid2str(&qctl.obd_uuid));
1487 fprintf(stderr, "quotaoff failed: %s\n",
1495 static int lfs_quotainv(int argc, char **argv)
1499 struct if_quotactl qctl;
1502 memset(&qctl, 0, sizeof(qctl));
1503 qctl.qc_cmd = LUSTRE_Q_INVALIDATE;
1506 while ((c = getopt(argc, argv, "fgu")) != -1) {
1509 qctl.qc_type |= 0x01;
1512 qctl.qc_type |= 0x02;
1515 qctl.qc_cmd = LUSTRE_Q_FINVALIDATE;
1518 fprintf(stderr, "error: %s: option '-%c' "
1519 "unrecognized\n", argv[0], c);
1526 else /* by default, invalidate quota for both user & group */
1527 qctl.qc_type = 0x02;
1534 rc = llapi_quotactl(mnt, &qctl);
1536 fprintf(stderr, "quotainv failed: %s\n", strerror(-rc));
1543 #define ARG2INT(nr, str, msg) \
1546 nr = strtol(str, &endp, 0); \
1548 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1553 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1555 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1556 * returns the value or ULONG_MAX on integer overflow or incorrect format
1558 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1559 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1560 * 3. empty integer value is interpreted as 0
1562 static unsigned long str2sec(const char* timestr)
1564 const char spec[] = "smhdw";
1565 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1566 unsigned long val = 0;
1569 if (strpbrk(timestr, spec) == NULL) {
1570 /* no specifiers inside the time string,
1571 should treat it as an integer value */
1572 val = strtoul(timestr, &tail, 10);
1573 return *tail ? ULONG_MAX : val;
1576 /* format string is XXwXXdXXhXXmXXs */
1582 v = strtoul(timestr, &tail, 10);
1583 if (v == ULONG_MAX || *tail == '\0')
1584 /* value too large (ULONG_MAX or more)
1585 or missing specifier */
1588 ptr = strchr(spec, *tail);
1590 /* unknown specifier */
1595 /* check if product will overflow the type */
1596 if (!(v < ULONG_MAX / mult[ind]))
1599 ADD_OVERFLOW(val, mult[ind] * v);
1600 if (val == ULONG_MAX)
1612 #define ARG2ULL(nr, str, def_units) \
1614 unsigned long long limit, units = def_units; \
1617 rc = parse_size(str, &limit, &units, 1); \
1619 fprintf(stderr, "error: bad limit value %s\n", str); \
1625 static inline int has_times_option(int argc, char **argv)
1629 for (i = 1; i < argc; i++)
1630 if (!strcmp(argv[i], "-t"))
1636 int lfs_setquota_times(int argc, char **argv)
1639 struct if_quotactl qctl;
1640 char *mnt, *obd_type = (char *)qctl.obd_type;
1641 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1642 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1643 struct option long_opts[] = {
1644 {"block-grace", required_argument, 0, 'b'},
1645 {"group", no_argument, 0, 'g'},
1646 {"inode-grace", required_argument, 0, 'i'},
1647 {"times", no_argument, 0, 't'},
1648 {"user", no_argument, 0, 'u'},
1652 memset(&qctl, 0, sizeof(qctl));
1653 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1654 qctl.qc_type = UGQUOTA;
1657 while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) {
1661 if (qctl.qc_type != UGQUOTA) {
1662 fprintf(stderr, "error: -u and -g can't be used "
1663 "more than once\n");
1666 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1669 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
1670 fprintf(stderr, "error: bad block-grace: %s\n",
1674 dqb->dqb_valid |= QIF_BTIME;
1677 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
1678 fprintf(stderr, "error: bad inode-grace: %s\n",
1682 dqb->dqb_valid |= QIF_ITIME;
1684 case 't': /* Yes, of course! */
1686 default: /* getopt prints error message for us when opterr != 0 */
1691 if (qctl.qc_type == UGQUOTA) {
1692 fprintf(stderr, "error: neither -u nor -g specified\n");
1696 if (optind != argc - 1) {
1697 fprintf(stderr, "error: unexpected parameters encountered\n");
1702 rc = llapi_quotactl(mnt, &qctl);
1705 fprintf(stderr, "%s %s ", obd_type,
1706 obd_uuid2str(&qctl.obd_uuid));
1707 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
1714 #define BSLIMIT (1 << 0)
1715 #define BHLIMIT (1 << 1)
1716 #define ISLIMIT (1 << 2)
1717 #define IHLIMIT (1 << 3)
1719 int lfs_setquota(int argc, char **argv)
1722 struct if_quotactl qctl;
1723 char *mnt, *obd_type = (char *)qctl.obd_type;
1724 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1725 struct option long_opts[] = {
1726 {"block-softlimit", required_argument, 0, 'b'},
1727 {"block-hardlimit", required_argument, 0, 'B'},
1728 {"group", required_argument, 0, 'g'},
1729 {"inode-softlimit", required_argument, 0, 'i'},
1730 {"inode-hardlimit", required_argument, 0, 'I'},
1731 {"user", required_argument, 0, 'u'},
1734 unsigned limit_mask = 0;
1737 if (has_times_option(argc, argv))
1738 return lfs_setquota_times(argc, argv);
1740 memset(&qctl, 0, sizeof(qctl));
1741 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1742 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
1743 * so it can be used as a marker that qc_type
1744 * isn't reinitialized from command line */
1747 while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) {
1751 if (qctl.qc_type != UGQUOTA) {
1752 fprintf(stderr, "error: -u and -g can't be used"
1753 " more than once\n");
1756 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
1757 rc = name2id(&qctl.qc_id, optarg,
1758 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
1760 qctl.qc_id = strtoul(optarg, &endptr, 10);
1761 if (*endptr != '\0') {
1762 fprintf(stderr, "error: can't find id "
1763 "for name %s\n", optarg);
1769 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
1770 dqb->dqb_bsoftlimit >>= 10;
1771 limit_mask |= BSLIMIT;
1774 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
1775 dqb->dqb_bhardlimit >>= 10;
1776 limit_mask |= BHLIMIT;
1779 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
1780 limit_mask |= ISLIMIT;
1783 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
1784 limit_mask |= IHLIMIT;
1786 default: /* getopt prints error message for us when opterr != 0 */
1791 if (qctl.qc_type == UGQUOTA) {
1792 fprintf(stderr, "error: neither -u nor -g was specified\n");
1796 if (limit_mask == 0) {
1797 fprintf(stderr, "error: at least one limit must be specified\n");
1801 if (optind != argc - 1) {
1802 fprintf(stderr, "error: unexpected parameters encountered\n");
1808 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
1809 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
1810 /* sigh, we can't just set blimits/ilimits */
1811 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
1812 .qc_type = qctl.qc_type,
1813 .qc_id = qctl.qc_id};
1815 rc = llapi_quotactl(mnt, &tmp_qctl);
1817 fprintf(stderr, "error: setquota failed while retrieving"
1818 " current quota settings (%s)\n",
1823 if (!(limit_mask & BHLIMIT))
1824 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
1825 if (!(limit_mask & BSLIMIT))
1826 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
1827 if (!(limit_mask & IHLIMIT))
1828 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
1829 if (!(limit_mask & ISLIMIT))
1830 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
1832 /* Keep grace times if we have got no softlimit arguments */
1833 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
1834 dqb->dqb_valid |= QIF_BTIME;
1835 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
1838 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
1839 dqb->dqb_valid |= QIF_ITIME;
1840 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
1844 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
1845 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
1847 rc = llapi_quotactl(mnt, &qctl);
1850 fprintf(stderr, "%s %s ", obd_type,
1851 obd_uuid2str(&qctl.obd_uuid));
1852 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
1859 static inline char *type2name(int check_type)
1861 if (check_type == USRQUOTA)
1863 else if (check_type == GRPQUOTA)
1869 /* Converts seconds value into format string
1870 * result is returned in buf
1872 * 1. result is in descenting order: 1w2d3h4m5s
1873 * 2. zero fields are not filled (except for p. 3): 5d1s
1874 * 3. zero seconds value is presented as "0s"
1876 static char * __sec2str(time_t seconds, char *buf)
1878 const char spec[] = "smhdw";
1879 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1884 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1885 c = seconds / mult[i];
1887 if (c > 0 || (i == 0 && buf == tail))
1888 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1896 static void sec2str(time_t seconds, char *buf, int rc)
1903 tail = __sec2str(seconds, tail);
1905 if (rc && tail - buf < 39) {
1911 static void diff2str(time_t seconds, char *buf, time_t now)
1917 if (seconds <= now) {
1918 strcpy(buf, "none");
1921 __sec2str(seconds - now, buf);
1924 static void print_quota_title(char *name, struct if_quotactl *qctl)
1926 printf("Disk quotas for %s %s (%cid %u):\n",
1927 type2name(qctl->qc_type), name,
1928 *type2name(qctl->qc_type), qctl->qc_id);
1929 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1931 "kbytes", "quota", "limit", "grace",
1932 "files", "quota", "limit", "grace");
1935 static void print_quota(char *mnt, struct if_quotactl *qctl, int type, int rc)
1941 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1942 int bover = 0, iover = 0;
1943 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1945 if (dqb->dqb_bhardlimit &&
1946 toqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
1948 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
1949 if (dqb->dqb_btime > now) {
1956 if (dqb->dqb_ihardlimit &&
1957 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
1959 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
1960 if (dqb->dqb_btime > now) {
1967 #if 0 /* XXX: always print quotas even when no usages */
1968 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1974 if (strlen(mnt) > 15)
1975 printf("%s\n%15s", mnt, "");
1977 printf("%15s", mnt);
1980 diff2str(dqb->dqb_btime, timebuf, now);
1981 if (rc == -EREMOTEIO)
1982 sprintf(numbuf[0], LPU64"*",
1983 toqb(dqb->dqb_curspace));
1986 (dqb->dqb_valid & QIF_SPACE) ?
1987 LPU64 : "["LPU64"]",
1988 toqb(dqb->dqb_curspace));
1989 if (type == QC_GENERAL)
1990 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS)
1991 ? LPU64 : "["LPU64"]",
1992 dqb->dqb_bsoftlimit);
1994 sprintf(numbuf[1], "%s", "-");
1995 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS)
1996 ? LPU64 : "["LPU64"]", dqb->dqb_bhardlimit);
1997 printf(" %7s%c %6s %7s %7s",
1998 numbuf[0], bover ? '*' : ' ', numbuf[1],
1999 numbuf[2], bover > 1 ? timebuf : "-");
2002 diff2str(dqb->dqb_itime, timebuf, now);
2004 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2005 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2006 if (type == QC_GENERAL)
2007 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS)
2008 ? LPU64 : "["LPU64"]",
2009 dqb->dqb_isoftlimit);
2011 sprintf(numbuf[1], "%s", "-");
2012 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2013 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2014 if (type != QC_OSTIDX)
2015 printf(" %7s%c %6s %7s %7s",
2016 numbuf[0], iover ? '*' : ' ', numbuf[1],
2017 numbuf[2], iover > 1 ? timebuf : "-");
2019 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
2022 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2023 qctl->qc_cmd == Q_GETOINFO) {
2027 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
2028 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
2029 printf("Block grace time: %s; Inode grace time: %s\n",
2030 bgtimebuf, igtimebuf);
2034 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt)
2036 int rc = 0, rc1 = 0, count = 0;
2037 __u32 valid = qctl->qc_valid;
2039 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2041 fprintf(stderr, "can not get %s count: %s\n",
2042 is_mdt ? "mdt": "ost", strerror(-rc));
2046 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2047 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2048 rc = llapi_quotactl(mnt, qctl);
2050 /* It is remote client case. */
2051 if (-rc == EOPNOTSUPP) {
2058 fprintf(stderr, "quotactl %s%d failed.\n",
2059 is_mdt ? "mdt": "ost", qctl->qc_idx);
2063 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl, qctl->qc_valid, 0);
2067 qctl->qc_valid = valid;
2071 static int lfs_quota(int argc, char **argv)
2074 char *mnt, *name = NULL;
2075 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2076 .qc_type = UGQUOTA };
2077 char *obd_type = (char *)qctl.obd_type;
2078 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2079 int rc, rc1 = 0, rc2 = 0, rc3 = 0,
2080 verbose = 0, pass = 0, quiet = 0, inacc;
2082 __u32 valid = QC_GENERAL, idx = 0;
2085 while ((c = getopt(argc, argv, "gi:I:o:qtuv")) != -1) {
2088 if (qctl.qc_type != UGQUOTA) {
2089 fprintf(stderr, "error: use either -u or -g\n");
2092 qctl.qc_type = USRQUOTA;
2095 if (qctl.qc_type != UGQUOTA) {
2096 fprintf(stderr, "error: use either -u or -g\n");
2099 qctl.qc_type = GRPQUOTA;
2102 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2105 valid = qctl.qc_valid = QC_UUID;
2106 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2109 valid = qctl.qc_valid = QC_MDTIDX;
2110 idx = qctl.qc_idx = atoi(optarg);
2113 valid = qctl.qc_valid = QC_OSTIDX;
2114 idx = qctl.qc_idx = atoi(optarg);
2123 fprintf(stderr, "error: %s: option '-%c' "
2124 "unrecognized\n", argv[0], c);
2129 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2130 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2131 optind == argc - 1) {
2133 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2134 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2135 qctl.qc_valid = valid;
2138 qctl.qc_type = USRQUOTA;
2139 qctl.qc_id = geteuid();
2141 qctl.qc_type = GRPQUOTA;
2142 qctl.qc_id = getegid();
2144 rc = id2name(&name, qctl.qc_id,
2145 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2148 /* lfs quota -u username /path/to/lustre/mount */
2149 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2150 /* options should be followed by u/g-name and mntpoint */
2151 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2152 fprintf(stderr, "error: missing quota argument(s)\n");
2156 name = argv[optind++];
2157 rc = name2id(&qctl.qc_id, name,
2158 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2160 qctl.qc_id = strtoul(name, &endptr, 10);
2161 if (*endptr != '\0') {
2162 fprintf(stderr, "error: can't find id for name "
2167 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2168 fprintf(stderr, "error: missing quota info argument(s)\n");
2174 rc1 = llapi_quotactl(mnt, &qctl);
2178 fprintf(stderr, "%s quotas are not enabled.\n",
2179 qctl.qc_type == USRQUOTA ? "user" : "group");
2182 fprintf(stderr, "Permission denied.\n");
2184 /* We already got a "No such file..." message. */
2187 fprintf(stderr, "Unexpected quotactl error: %s\n",
2192 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
2193 print_quota_title(name, &qctl);
2195 if (rc1 && *obd_type)
2196 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2198 if (qctl.qc_valid != QC_GENERAL)
2201 inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
2202 ((qctl.qc_dqblk.dqb_valid&(QIF_LIMITS|QIF_USAGE))!=(QIF_LIMITS|QIF_USAGE));
2204 print_quota(mnt, &qctl, QC_GENERAL, rc1);
2206 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO && verbose) {
2207 rc2 = print_obd_quota(mnt, &qctl, 1);
2208 rc3 = print_obd_quota(mnt, &qctl, 0);
2211 if (rc1 || rc2 || rc3 || inacc)
2212 printf("Some errors happened when getting quota info. "
2213 "Some devices may be not working or deactivated. "
2214 "The data in \"[]\" is inaccurate.\n");
2222 #endif /* HAVE_SYS_QUOTA_H! */
2224 static int flushctx_ioctl(char *mp)
2228 fd = open(mp, O_RDONLY);
2230 fprintf(stderr, "flushctx: error open %s: %s\n",
2231 mp, strerror(errno));
2235 rc = ioctl(fd, LL_IOC_FLUSHCTX);
2237 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
2238 mp, strerror(errno));
2244 static int lfs_flushctx(int argc, char **argv)
2246 int kdestroy = 0, c;
2248 char procline[PATH_MAX], *line;
2252 while ((c = getopt(argc, argv, "k")) != -1) {
2258 fprintf(stderr, "error: %s: option '-%c' "
2259 "unrecognized\n", argv[0], c);
2266 if ((rc = system("kdestroy > /dev/null")) != 0) {
2267 rc = WEXITSTATUS(rc);
2268 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
2272 if (optind >= argc) {
2273 /* flush for all mounted lustre fs. */
2274 proc = fopen("/proc/mounts", "r");
2276 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
2281 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
2286 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
2287 fprintf(stderr, "%s: unexpected format in "
2293 if (strcmp(fs, "lustre") != 0)
2295 /* we use '@' to determine it's a client. are there
2296 * any other better way?
2298 if (strchr(dev, '@') == NULL)
2301 if (flushctx_ioctl(mp))
2305 /* flush fs as specified */
2306 while (optind < argc) {
2307 if (flushctx_ioctl(argv[optind++]))
2315 static int lfs_lsetfacl(int argc, char **argv)
2318 return(llapi_lsetfacl(argc, argv));
2321 static int lfs_lgetfacl(int argc, char **argv)
2324 return(llapi_lgetfacl(argc, argv));
2327 static int lfs_rsetfacl(int argc, char **argv)
2330 return(llapi_rsetfacl(argc, argv));
2333 static int lfs_rgetfacl(int argc, char **argv)
2336 return(llapi_rgetfacl(argc, argv));
2339 static int lfs_cp(int argc, char **argv)
2341 return(llapi_cp(argc, argv));
2344 static int lfs_ls(int argc, char **argv)
2346 return(llapi_ls(argc, argv));
2349 static int lfs_changelog(int argc, char **argv)
2351 void *changelog_priv;
2352 struct changelog_rec *rec;
2353 long long startrec = 0, endrec = 0;
2355 struct option long_opts[] = {
2356 {"follow", no_argument, 0, 'f'},
2359 char short_opts[] = "f";
2363 while ((rc = getopt_long(argc, argv, short_opts,
2364 long_opts, NULL)) != -1) {
2372 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2373 argv[0], argv[optind - 1]);
2380 mdd = argv[optind++];
2382 startrec = strtoll(argv[optind++], NULL, 10);
2384 endrec = strtoll(argv[optind++], NULL, 10);
2386 rc = llapi_changelog_start(&changelog_priv,
2387 CHANGELOG_FLAG_BLOCK |
2388 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
2391 fprintf(stderr, "Can't start changelog: %s\n",
2392 strerror(errno = -rc));
2396 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
2400 if (endrec && rec->cr_index > endrec) {
2401 llapi_changelog_free(&rec);
2404 if (rec->cr_index < startrec) {
2405 llapi_changelog_free(&rec);
2409 secs = rec->cr_time >> 30;
2410 gmtime_r(&secs, &ts);
2411 printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
2412 "0x%x t="DFID, rec->cr_index, rec->cr_type,
2413 changelog_type2str(rec->cr_type),
2414 ts.tm_hour, ts.tm_min, ts.tm_sec,
2415 (int)(rec->cr_time & ((1<<30) - 1)),
2416 ts.tm_year+1900, ts.tm_mon+1, ts.tm_mday,
2417 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
2418 if (rec->cr_namelen)
2419 /* namespace rec includes parent and filename */
2420 printf(" p="DFID" %.*s\n", PFID(&rec->cr_pfid),
2421 rec->cr_namelen, rec->cr_name);
2425 llapi_changelog_free(&rec);
2428 llapi_changelog_fini(&changelog_priv);
2431 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
2433 return (rc == 1 ? 0 : rc);
2436 static int lfs_changelog_clear(int argc, char **argv)
2444 endrec = strtoll(argv[3], NULL, 10);
2446 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
2448 fprintf(stderr, "%s error: %s\n", argv[0],
2449 strerror(errno = -rc));
2453 static int lfs_fid2path(int argc, char **argv)
2455 struct option long_opts[] = {
2456 {"cur", no_argument, 0, 'c'},
2457 {"link", required_argument, 0, 'l'},
2458 {"rec", required_argument, 0, 'r'},
2461 char short_opts[] = "cl:r:";
2462 char *device, *fid, *path;
2463 long long recno = -1;
2471 while ((rc = getopt_long(argc, argv, short_opts,
2472 long_opts, NULL)) != -1) {
2478 linkno = strtol(optarg, NULL, 10);
2481 recno = strtoll(optarg, NULL, 10);
2486 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2487 argv[0], argv[optind - 1]);
2491 device = argv[optind++];
2492 fid = argv[optind++];
2496 path = calloc(1, PATH_MAX);
2498 lnktmp = (linkno >= 0) ? linkno : 0;
2500 int oldtmp = lnktmp;
2501 long long rectmp = recno;
2502 rc = llapi_fid2path(device, fid, path, PATH_MAX, &rectmp,
2505 fprintf(stderr, "%s error: %s\n", argv[0],
2506 strerror(errno = -rc));
2511 fprintf(stdout, "%lld ", rectmp);
2512 if (device[0] == '/') {
2513 fprintf(stdout, "%s", device);
2514 if (device[strlen(device) - 1] != '/')
2515 fprintf(stdout, "/");
2516 } else if (path[0] == '\0') {
2517 fprintf(stdout, "/");
2519 fprintf(stdout, "%s\n", path);
2522 /* specified linkno */
2524 if (oldtmp == lnktmp)
2533 static int lfs_path2fid(int argc, char **argv)
2543 rc = llapi_path2fid(path, &fid);
2545 fprintf(stderr, "can't get fid for %s: %s\n", path,
2546 strerror(errno = -rc));
2550 printf(DFID"\n", PFID(&fid));
2555 int main(int argc, char **argv)
2561 ptl_initialize(argc, argv);
2562 if (obd_initialize(argc, argv) < 0)
2564 if (dbg_initialize(argc, argv) < 0)
2567 Parser_init("lfs > ", cmdlist);
2570 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
2572 rc = Parser_commands();
2575 obd_finalize(argc, argv);
2576 return rc < 0 ? -rc : rc;