1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Robert Read <rread@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <sys/types.h>
43 #include <lnet/api-support.h>
44 #include <lnet/lnetctl.h>
46 #include <liblustre.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre/liblustreapi.h>
49 #include <lustre/lustre_user.h>
54 unsigned int libcfs_subsystem_debug = 0;
57 static int lfs_setstripe(int argc, char **argv);
58 static int lfs_find(int argc, char **argv);
59 static int lfs_getstripe(int argc, char **argv);
60 static int lfs_osts(int argc, char **argv);
61 static int lfs_df(int argc, char **argv);
62 static int lfs_check(int argc, char **argv);
63 static int lfs_catinfo(int argc, char **argv);
64 #ifdef HAVE_QUOTA_SUPPORT
65 static int lfs_quotachown(int argc, char **argv);
66 static int lfs_quotacheck(int argc, char **argv);
67 static int lfs_quotaon(int argc, char **argv);
68 static int lfs_quotaoff(int argc, char **argv);
69 static int lfs_setquota(int argc, char **argv);
70 static int lfs_quota(int argc, char **argv);
72 static int lfs_flushctx(int argc, char **argv);
73 static int lfs_join(int argc, char **argv);
74 static int lfs_lsetfacl(int argc, char **argv);
75 static int lfs_lgetfacl(int argc, char **argv);
76 static int lfs_rsetfacl(int argc, char **argv);
77 static int lfs_rgetfacl(int argc, char **argv);
78 static int lfs_cp(int argc, char **argv);
79 static int lfs_ls(int argc, char **argv);
81 /* all avaialable commands */
82 command_t cmdlist[] = {
83 {"setstripe", lfs_setstripe, 0,
84 "Create a new file with a specific striping pattern or\n"
85 "set the default striping pattern on an existing directory or\n"
86 "delete the default striping pattern from an existing directory\n"
87 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
89 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
90 " [--index|-i stripe_index]\n"
91 " [--count|-c stripe_count]\n"
93 " setstripe -d <dirname> (to delete default striping)\n"
94 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
95 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
96 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
97 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
98 {"getstripe", lfs_getstripe, 0,
99 "To list the striping info for a given filename or files in a\n"
100 "directory or recursively for all files in a directory tree.\n"
101 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
102 " [--recursive | -r] <dir|file> ..."},
103 {"find", lfs_find, 0,
104 "To find files that match given parameters recursively in a directory tree.\n"
105 "usage: find <dir/file> ... \n"
106 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
107 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
108 " [--print|-p] [--obd|-O <uuid>] [[!] --type|-t <filetype>]\n"
109 "\t !: used before an option indicates 'NOT' the requested attribute\n"
110 "\t -: used before an value indicates 'AT MOST' the requested value\n"
111 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
112 {"check", lfs_check, 0,
113 "Display the status of MDS or OSTs (as specified in the command)\n"
114 "or all the servers (MDS and OSTs).\n"
115 "usage: check <osts|mds|servers>"},
116 {"catinfo", lfs_catinfo, 0,
117 "Show information of specified type logs.\n"
118 "usage: catinfo {keyword} [node name]\n"
119 "\tkeywords are one of followings: config, deletions.\n"
120 "\tnode name must be provided when use keyword config."},
121 {"join", lfs_join, 0,
122 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
123 "usage: join <filename_A> <filename_B>\n"},
124 {"osts", lfs_osts, 0, "osts"},
126 "report filesystem disk space usage or inodes usage"
128 "Usage: df [-i] [-h] [path]"},
129 #ifdef HAVE_QUOTA_SUPPORT
130 {"quotachown",lfs_quotachown, 0,
131 "Change files' owner or group on the specified filesystem.\n"
132 "usage: quotachown [-i] <filesystem>\n"
133 "\t-i: ignore error if file is not exist\n"},
134 {"quotacheck", lfs_quotacheck, 0,
135 "Scan the specified filesystem for disk usage, and create,\n"
136 "or update quota files.\n"
137 "usage: quotacheck [ -ug ] <filesystem>"},
138 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
139 "usage: quotaon [ -ugf ] <filesystem>"},
140 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
141 "usage: quotaoff [ -ug ] <filesystem>"},
142 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
143 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
144 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
145 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
146 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
148 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
149 "usage: flushctx [-k] [mountpoint...]"},
150 {"lsetfacl", lfs_lsetfacl, 0,
151 "Remote user setfacl for user/group on the same remote client.\n"
152 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
153 {"lgetfacl", lfs_lgetfacl, 0,
154 "Remote user getfacl for user/group on the same remote client.\n"
155 "usage: lgetfacl [-dRLPvh] file ..."},
156 {"rsetfacl", lfs_rsetfacl, 0,
157 "Remote user setfacl for user/group on other clients.\n"
158 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
159 {"rgetfacl", lfs_rgetfacl, 0,
160 "Remote user getfacl for user/group on other clients.\n"
161 "usage: rgetfacl [-dRLPvh] file ..."},
163 "Remote user copy files and directories.\n"
164 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
166 "Remote user list directory contents.\n"
167 "usage: ls [OPTION]... [FILE]..."},
168 {"help", Parser_help, 0, "help"},
169 {"exit", Parser_quit, 0, "quit"},
170 {"quit", Parser_quit, 0, "quit"},
175 static int lfs_setstripe(int argc, char **argv)
179 unsigned long long st_size;
180 int st_offset, st_count;
184 char *stripe_size_arg = NULL;
185 char *stripe_off_arg = NULL;
186 char *stripe_count_arg = NULL;
187 unsigned long long size_units;
189 struct option long_opts[] = {
190 {"size", required_argument, 0, 's'},
191 {"count", required_argument, 0, 'c'},
192 {"index", required_argument, 0, 'i'},
193 {"delete", no_argument, 0, 'd'},
200 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
201 /* for compatibility with the existing positional parameter
205 } else if (argc == 5 &&
206 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
207 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
208 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
209 /* for compatibility with the existing positional parameter
212 stripe_size_arg = argv[2];
213 stripe_off_arg = argv[3];
214 stripe_count_arg = argv[4];
218 while ((c = getopt_long(argc, argv, "c:di:s:",
219 long_opts, NULL)) >= 0) {
225 stripe_count_arg = optarg;
228 /* delete the default striping pattern */
232 stripe_off_arg = optarg;
235 stripe_size_arg = optarg;
240 fprintf(stderr, "error: %s: option '%s' "
242 argv[0], argv[optind - 1]);
247 fname = argv[optind];
253 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
254 stripe_count_arg != NULL)) {
255 fprintf(stderr, "error: %s: cannot specify -d with "
256 "-s, -c or -i options\n",
261 if (optind != argc - 1) {
262 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
264 argv[0], argv[argc - 1]);
268 /* get the stripe size */
269 if (stripe_size_arg != NULL) {
270 result = parse_size(stripe_size_arg, &st_size, &size_units);
272 fprintf(stderr,"error: bad size '%s'\n",
277 /* get the stripe offset */
278 if (stripe_off_arg != NULL) {
279 st_offset = strtoul(stripe_off_arg, &end, 0);
281 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
282 argv[0], stripe_off_arg);
286 /* get the stripe count */
287 if (stripe_count_arg != NULL) {
288 st_count = strtoul(stripe_count_arg, &end, 0);
290 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
291 argv[0], stripe_count_arg);
296 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
298 fprintf(stderr, "error: %s: create stripe file failed\n",
304 static int set_time(time_t *time, time_t *set, char *str)
311 else if (str[0] == '-')
317 t = strtol(str, NULL, 0);
318 if (*time < t * 24 * 60 * 60) {
321 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
325 *set = *time - t * 24 * 60 * 60;
329 static int lfs_find(int argc, char **argv)
334 struct find_param param = { .maxdepth = -1 };
336 struct option long_opts[] = {
337 /* New find options. */
338 {"atime", required_argument, 0, 'A'},
339 {"ctime", required_argument, 0, 'C'},
340 {"mtime", required_argument, 0, 'M'},
341 {"maxdepth", required_argument, 0, 'D'},
342 {"name", required_argument, 0, 'n'},
343 /* --obd is considered as a new option. */
344 {"obd", required_argument, 0, 'O'},
345 {"ost", required_argument, 0, 'O'},
346 {"print", no_argument, 0, 'P'},
347 {"print0", no_argument, 0, 'p'},
348 /* Old find options. */
349 {"quiet", no_argument, 0, 'q'},
350 {"recursive", no_argument, 0, 'r'},
351 {"size", required_argument, 0, 's'},
352 {"type", required_argument, 0, 't'},
353 {"verbose", no_argument, 0, 'v'},
366 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrs:t:v",
367 long_opts, NULL)) >= 0) {
372 /* '!' is part of option */
373 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
374 if (!isoption && pathend != -1) {
375 fprintf(stderr, "err: %s: filename|dirname must either "
376 "precede options or follow options\n",
380 if (!isoption && pathstart == -1)
381 pathstart = optind - 1;
382 if (isoption && pathstart != -1 && pathend == -1) {
383 pathend = optind - 2;
384 if ((c == 1 && strcmp(optarg, "!") == 0) ||
385 c == 'P' || c == 'p' || c == 'O' ||
386 c == 'q' || c == 'r' || c == 'v')
387 pathend = optind - 1;
394 if (strcmp(optarg, "!") == 0)
398 xtime = ¶m.atime;
399 xsign = ¶m.asign;
402 xtime = ¶m.ctime;
403 xsign = ¶m.csign;
407 xtime = ¶m.mtime;
408 xsign = ¶m.msign;
412 if (optarg[0] == '-')
414 else if (optarg[0] == '+')
423 ret = set_time(&t, xtime, optarg);
431 param.maxdepth = strtol(optarg, 0, 0);
435 param.pattern = (char *)optarg;
436 param.exclude_pattern = !!neg_opt;
439 char *buf, *token, *next, *p;
442 len = strlen((char *)optarg);
446 strcpy(buf, (char *)optarg);
448 if (param.num_alloc_obds == 0) {
449 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
450 sizeof(struct obd_uuid));
451 if (param.obduuid == NULL)
453 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
456 for (token = buf; token && *token; token = next) {
457 p = strchr(token, ',');
463 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
487 param.exclude_type = !!neg_opt;
489 case 'b': param.type = S_IFBLK; break;
490 case 'c': param.type = S_IFCHR; break;
491 case 'd': param.type = S_IFDIR; break;
492 case 'f': param.type = S_IFREG; break;
493 case 'l': param.type = S_IFLNK; break;
494 case 'p': param.type = S_IFIFO; break;
495 case 's': param.type = S_IFSOCK; break;
496 #ifdef S_IFDOOR /* Solaris only */
497 case 'D': param.type = S_IFDOOR; break;
499 default: fprintf(stderr, "error: %s: bad type '%s'\n",
506 if (optarg[0] == '-')
508 else if (optarg[0] == '+')
517 if (optarg[0] == '+')
518 param.size_sign = -1;
519 else if (optarg[0] == '-')
520 param.size_sign = +1;
524 ret = parse_size(optarg, ¶m.size,¶m.size_units);
526 fprintf(stderr,"error: bad size '%s'\n",
539 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
540 argv[0], argv[optind - 1]);
545 if (pathstart == -1) {
546 fprintf(stderr, "error: %s: no filename|pathname\n",
549 } else if (pathend == -1) {
557 static int deprecated_warning;
558 if (!deprecated_warning) {
559 fprintf(stderr, "lfs find: -q, -r, -v options "
560 "deprecated. Use 'lfs getstripe' instead.\n");
561 deprecated_warning = 1;
563 if (!param.recursive && param.maxdepth == -1)
569 ret = llapi_find(argv[pathstart], ¶m);
571 ret = llapi_getstripe(argv[pathstart], ¶m);
572 } while (++pathstart < pathend && !ret);
575 fprintf(stderr, "error: %s failed for %s.\n",
576 argv[0], argv[optind - 1]);
578 if (param.obduuid && param.num_alloc_obds)
584 static int lfs_getstripe(int argc, char **argv)
586 struct option long_opts[] = {
588 {"quiet", 0, 0, 'q'},
589 {"recursive", 0, 0, 'r'},
590 {"verbose", 0, 0, 'v'},
593 char short_opts[] = "hO:qrv";
595 struct find_param param = { 0 };
598 while ((c = getopt_long(argc, argv, short_opts,
599 long_opts, NULL)) != -1) {
604 "error: %s: only one obduuid allowed",
608 param.obduuid = (struct obd_uuid *)optarg;
624 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
625 argv[0], argv[optind - 1]);
633 param.maxdepth = param.recursive ? -1 : 1;
636 rc = llapi_getstripe(argv[optind], ¶m);
637 } while (++optind < argc && !rc);
640 fprintf(stderr, "error: %s failed for %s.\n",
641 argv[0], argv[optind - 1]);
645 static int lfs_osts(int argc, char **argv)
648 struct mntent *mnt = NULL;
649 struct obd_uuid *obduuid = NULL;
650 struct find_param param;
656 fp = setmntent(MOUNTED, "r");
659 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
663 memset(¶m, 0, sizeof(param));
664 param.obduuid = obduuid;
665 while (feof(fp) == 0 && ferror(fp) ==0) {
666 if (llapi_is_lustre_mnt(mnt)) {
667 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
670 "error: %s: failed on %s\n",
671 argv[0], mnt->mnt_dir);
681 #define COOK(value) \
684 while (value > 1024) { \
698 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
700 char rpath[PATH_MAX] = {'\0'};
702 int rc, len, out_len = 0;
704 if (!realpath(path, rpath)) {
706 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
707 path, strerror(-rc));
713 while (feof(fp) == 0 && ferror(fp) == 0) {
714 if (llapi_is_lustre_mnt(mnt)) {
715 len = strlen(mnt->mnt_dir);
717 !strncmp(rpath, mnt->mnt_dir, len)) {
719 memset(mntdir, 0, dir_len);
720 strncpy(mntdir, mnt->mnt_dir, dir_len);
729 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
733 static int showdf(char *mntdir, struct obd_statfs *stat,
734 char *uuid, int ishow, int cooked,
735 char *type, int index, int rc)
737 long long avail, used, total;
739 char *suffix = "KMGTPEZY";
740 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
741 char tbuf[20], ubuf[20], abuf[20], rbuf[20];
749 avail = stat->os_ffree;
750 used = stat->os_files - stat->os_ffree;
751 total = stat->os_files;
753 int shift = cooked ? 0 : 10;
755 avail = (stat->os_bavail * stat->os_bsize) >> shift;
756 used = ((stat->os_blocks - stat->os_bfree) *
757 stat->os_bsize) >> shift;
758 total = (stat->os_blocks * stat->os_bsize) >> shift;
762 ratio = (double)used / (double)total;
768 cook_val = (double)total;
771 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
773 sprintf(tbuf, CDF, total);
775 cook_val = (double)used;
778 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
780 sprintf(ubuf, CDF, used);
782 cook_val = (double)avail;
785 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
787 sprintf(abuf, CDF, avail);
789 sprintf(tbuf, CDF, total);
790 sprintf(ubuf, CDF, used);
791 sprintf(abuf, CDF, avail);
794 sprintf(rbuf, RDF, (int)(ratio * 100));
795 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
796 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
798 printf("[%s:%d]\n", type, index);
804 printf(UUF": inactive device\n", uuid);
807 printf(UUF": %s\n", uuid, strerror(-rc));
814 static int mntdf(char *mntdir, int ishow, int cooked)
816 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
817 struct obd_uuid uuid_buf;
822 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
823 "UUID", "Inodes", "IUsed", "IFree",
824 "IUse%", "Mounted on");
826 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
827 "UUID", cooked ? "bytes" : "1K-blocks",
828 "Used", "Available", "Use%", "Mounted on");
830 for (index = 0; ; index++) {
831 memset(&stat_buf, 0, sizeof(struct obd_statfs));
832 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
833 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
834 &stat_buf, &uuid_buf);
841 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
842 rc == -ENODATA || rc == 0) {
843 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
847 "error: llapi_obd_statfs(%s): %s (%d)\n",
848 uuid_buf.uuid, strerror(-rc), rc);
852 sum.os_ffree += stat_buf.os_ffree;
853 sum.os_files += stat_buf.os_files;
857 for (index = 0; ; index++) {
858 memset(&stat_buf, 0, sizeof(struct obd_statfs));
859 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
860 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
861 &stat_buf, &uuid_buf);
868 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
869 rc == -ENODATA || rc == 0) {
870 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
874 "error: llapi_obd_statfs failed: %s (%d)\n",
879 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
880 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
881 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
886 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
891 static int lfs_df(int argc, char **argv)
895 struct mntent *mnt = NULL;
896 char mntdir[PATH_MAX] = {'\0'};
897 int ishow = 0, cooked = 0;
901 while ((c = getopt(argc, argv, "ih")) != -1) {
916 fp = setmntent(MOUNTED, "r");
919 fprintf(stderr, "error: %s: open %s failed( %s )\n",
920 argv[0], MOUNTED, strerror(errno));
924 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
930 rc = mntdf(mntdir, ishow, cooked);
935 while (feof(fp) == 0 && ferror(fp) == 0) {
936 if (llapi_is_lustre_mnt(mnt)) {
937 rc = mntdf(mnt->mnt_dir, ishow, cooked);
950 static int lfs_check(int argc, char **argv)
954 struct mntent *mnt = NULL;
963 obd_types[0] = obd_type1;
964 obd_types[1] = obd_type2;
966 if (strcmp(argv[1], "osts") == 0) {
967 strcpy(obd_types[0], "osc");
968 } else if (strcmp(argv[1], "mds") == 0) {
969 strcpy(obd_types[0], "mdc");
970 } else if (strcmp(argv[1], "servers") == 0) {
972 strcpy(obd_types[0], "osc");
973 strcpy(obd_types[1], "mdc");
975 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
980 fp = setmntent(MOUNTED, "r");
982 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
986 while (feof(fp) == 0 && ferror(fp) ==0) {
987 if (llapi_is_lustre_mnt(mnt))
995 fprintf(stderr, "No suitable Lustre mount found\n");
999 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
1002 fprintf(stderr, "error: %s: %s status failed\n",
1009 static int lfs_catinfo(int argc, char **argv)
1012 struct mntent *mnt = NULL;
1015 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1018 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1021 fp = setmntent(MOUNTED, "r");
1023 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1026 mnt = getmntent(fp);
1027 while (feof(fp) == 0 && ferror(fp) == 0) {
1028 if (llapi_is_lustre_mnt(mnt))
1030 mnt = getmntent(fp);
1037 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1039 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1041 fprintf(stderr, "no lustre_lite mounted.\n");
1048 int lfs_join(int argc, char **argv)
1050 char *name_head, *name_tail;
1056 name_head = argv[1];
1057 fd = open(name_head, O_WRONLY);
1059 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1063 size = lseek(fd, 0, SEEK_END);
1064 if (size % JOIN_FILE_ALIGN) {
1065 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1066 name_head, (long long)size, JOIN_FILE_ALIGN);
1070 name_tail = argv[2];
1071 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1075 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1081 #ifdef HAVE_QUOTA_SUPPORT
1082 static int lfs_quotachown(int argc, char **argv)
1089 while ((c = getopt(argc, argv, "i")) != -1) {
1095 fprintf(stderr, "error: %s: option '-%c' "
1096 "unrecognized\n", argv[0], c);
1102 rc = llapi_quotachown(argv[optind], flag);
1104 fprintf(stderr,"error: change file owner/group failed.\n");
1109 static int lfs_quotacheck(int argc, char **argv)
1111 int c, check_type = 0;
1113 struct if_quotacheck qchk;
1114 struct if_quotactl qctl;
1115 char *obd_type = qchk.obd_type;
1116 char *obd_uuid = qchk.obd_uuid.uuid;
1119 memset(&qchk, 0, sizeof(qchk));
1122 while ((c = getopt(argc, argv, "ug")) != -1) {
1131 fprintf(stderr, "error: %s: option '-%c' "
1132 "unrecognized\n", argv[0], c);
1139 else /* do quotacheck for both user & group quota by default */
1147 memset(&qctl, 0, sizeof(qctl));
1148 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1149 qctl.qc_id = QFMT_LDISKFS;
1150 qctl.qc_type = check_type;
1151 rc = llapi_quotactl(mnt, &qctl);
1153 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1157 rc = llapi_quotacheck(mnt, check_type);
1159 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1163 rc = llapi_poll_quotacheck(mnt, &qchk);
1166 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1167 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1171 memset(&qctl, 0, sizeof(qctl));
1172 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1173 qctl.qc_id = QFMT_LDISKFS;
1174 qctl.qc_type = check_type;
1175 rc = llapi_quotactl(mnt, &qctl);
1178 fprintf(stderr, "%s %s ",
1179 qctl.obd_type, qctl.obd_uuid.uuid);
1180 fprintf(stderr, "%s turn on quota failed: %s\n",
1181 argv[0], strerror(errno));
1188 static int lfs_quotaon(int argc, char **argv)
1192 struct if_quotactl qctl;
1193 char *obd_type = qctl.obd_type;
1194 char *obd_uuid = qctl.obd_uuid.uuid;
1197 memset(&qctl, 0, sizeof(qctl));
1198 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1199 qctl.qc_id = QFMT_LDISKFS;
1202 while ((c = getopt(argc, argv, "ugf")) != -1) {
1205 qctl.qc_type |= 0x01;
1208 qctl.qc_type |= 0x02;
1211 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1214 fprintf(stderr, "error: %s: option '-%c' "
1215 "unrecognized\n", argv[0], c);
1222 else /* by default, enable quota for both user & group */
1223 qctl.qc_type = 0x02;
1230 rc = llapi_quotactl(mnt, &qctl);
1233 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1234 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1241 static int lfs_quotaoff(int argc, char **argv)
1245 struct if_quotactl qctl;
1246 char *obd_type = qctl.obd_type;
1247 char *obd_uuid = qctl.obd_uuid.uuid;
1250 memset(&qctl, 0, sizeof(qctl));
1251 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1254 while ((c = getopt(argc, argv, "ug")) != -1) {
1257 qctl.qc_type |= 0x01;
1260 qctl.qc_type |= 0x02;
1263 fprintf(stderr, "error: %s: option '-%c' "
1264 "unrecognized\n", argv[0], c);
1271 else /* by default, disable quota for both user & group */
1272 qctl.qc_type = 0x02;
1279 rc = llapi_quotactl(mnt, &qctl);
1282 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1283 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1290 static int name2id(unsigned int *id, char *name, int type)
1292 if (type == USRQUOTA) {
1293 struct passwd *entry;
1295 if (!(entry = getpwnam(name))) {
1301 *id = entry->pw_uid;
1303 struct group *entry;
1305 if (!(entry = getgrnam(name))) {
1311 *id = entry->gr_gid;
1317 static int id2name(char **name, unsigned int id, int type)
1319 if (type == USRQUOTA) {
1320 struct passwd *entry;
1322 if (!(entry = getpwuid(id))) {
1328 *name = entry->pw_name;
1330 struct group *entry;
1332 if (!(entry = getgrgid(id))) {
1338 *name = entry->gr_name;
1344 #define ARG2INT(nr, str, msg) \
1347 nr = strtol(str, &endp, 0); \
1349 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1354 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1356 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1357 * returns the value or ULONG_MAX on integer overflow or incorrect format
1359 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1360 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1361 * 3. empty integer value is interpreted as 0
1364 static unsigned long str2sec(const char* timestr) {
1365 const char spec[] = "smhdw";
1366 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1367 unsigned long val = 0;
1370 if (strpbrk(timestr, spec) == NULL) {
1371 /* no specifiers inside the time string,
1372 should treat it as an integer value */
1373 val = strtoul(timestr, &tail, 10);
1374 return *tail ? ULONG_MAX : val;
1377 /* format string is XXwXXdXXhXXmXXs */
1383 v = strtoul(timestr, &tail, 10);
1384 if (v == ULONG_MAX || *tail == '\0')
1385 /* value too large (ULONG_MAX or more)
1386 or missing specifier */
1389 ptr = strchr(spec, *tail);
1391 /* unknown specifier */
1396 /* check if product will overflow the type */
1397 if (!(v < ULONG_MAX / mult[ind]))
1400 ADD_OVERFLOW(val, mult[ind] * v);
1401 if (val == ULONG_MAX)
1413 int lfs_setquota(int argc, char **argv)
1417 struct if_quotactl qctl;
1418 char *obd_type = qctl.obd_type;
1419 char *obd_uuid = qctl.obd_uuid.uuid;
1422 memset(&qctl, 0, sizeof(qctl));
1423 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1426 while ((c = getopt(argc, argv, "ugt")) != -1) {
1429 qctl.qc_type |= 0x01;
1432 qctl.qc_type |= 0x02;
1435 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1438 fprintf(stderr, "error: %s: option '-%c' "
1439 "unrecognized\n", argv[0], c);
1447 if (qctl.qc_type == UGQUOTA) {
1448 fprintf(stderr, "error: user and group quotas can't be set "
1453 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1454 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1456 if (optind + 6 != argc)
1459 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1461 fprintf(stderr, "error: find id for name %s failed: %s\n",
1462 argv[optind - 1], strerror(errno));
1466 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1467 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1468 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1469 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1471 dqb->dqb_valid = QIF_LIMITS;
1473 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1475 if (optind + 3 != argc)
1478 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1479 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1482 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1483 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1490 rc = llapi_quotactl(mnt, &qctl);
1493 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1494 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1501 static inline char *type2name(int check_type)
1503 if (check_type == USRQUOTA)
1505 else if (check_type == GRPQUOTA)
1512 /* Converts seconds value into format string
1513 * result is returned in buf
1515 * 1. result is in descenting order: 1w2d3h4m5s
1516 * 2. zero fields are not filled (except for p. 3): 5d1s
1517 * 3. zero seconds value is presented as "0s"
1519 static void sec2str(time_t seconds, char *buf)
1521 const char spec[] = "smhdw";
1522 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1527 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1528 c = seconds / mult[i];
1530 if (c > 0 || (i == 0 && buf == tail))
1531 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1538 static void diff2str(time_t seconds, char *buf, time_t now)
1544 if (seconds <= now) {
1545 strcpy(buf, "none");
1548 sec2str(seconds - now, buf);
1551 static void print_quota_title(char *name, struct if_quotactl *qctl)
1553 printf("Disk quotas for %s %s (%cid %u):\n",
1554 type2name(qctl->qc_type), name,
1555 *type2name(qctl->qc_type), qctl->qc_id);
1556 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1558 "kbytes", "quota", "limit", "grace",
1559 "files", "quota", "limit", "grace");
1562 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1568 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1569 int bover = 0, iover = 0;
1570 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1572 if (dqb->dqb_bhardlimit &&
1573 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1575 } else if (dqb->dqb_bsoftlimit &&
1576 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1577 if (dqb->dqb_btime > now) {
1584 if (dqb->dqb_ihardlimit &&
1585 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1587 } else if (dqb->dqb_isoftlimit &&
1588 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1589 if (dqb->dqb_btime > now) {
1596 #if 0 /* XXX: always print quotas even when no usages */
1597 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1603 if (strlen(mnt) > 15)
1604 printf("%s\n%15s", mnt, "");
1606 printf("%15s", mnt);
1609 diff2str(dqb->dqb_btime, timebuf, now);
1611 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1612 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1613 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1614 printf(" %7s%c %6s %7s %7s",
1615 numbuf[0], bover ? '*' : ' ', numbuf[1],
1616 numbuf[2], bover > 1 ? timebuf : "");
1619 diff2str(dqb->dqb_itime, timebuf, now);
1621 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1622 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1623 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1625 printf(" %7s%c %6s %7s %7s",
1626 numbuf[0], iover ? '*' : ' ', numbuf[1],
1627 numbuf[2], iover > 1 ? timebuf : "");
1630 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1631 qctl->qc_cmd == Q_GETOINFO) {
1635 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1636 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1637 printf("Block grace time: %s; Inode grace time: %s\n",
1638 bgtimebuf, igtimebuf);
1642 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1646 /* XXX: this is a flag to mark that only mds quota is wanted */
1647 qctl->qc_dqblk.dqb_valid = 1;
1648 rc = llapi_quotactl(mnt, qctl);
1650 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1653 qctl->qc_dqblk.dqb_valid = 0;
1655 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1658 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1661 struct obd_uuid *uuids = NULL, *uuidp;
1662 int obdcount = 1024;
1667 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1671 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1672 sizeof(struct obd_uuid));
1677 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1679 struct obd_uuid *uuids_temp;
1681 if (rc == -EOVERFLOW) {
1682 uuids_temp = realloc(uuids, obdcount *
1683 sizeof(struct obd_uuid));
1684 if (uuids_temp != NULL)
1685 goto retry_get_uuids;
1690 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1694 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1695 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1697 /* XXX clear this flag to get quota from osts */
1698 qctl->qc_dqblk.dqb_valid = 0;
1699 rc = llapi_quotactl(mnt, qctl);
1701 fprintf(stderr, "%s quotactl failed: %s\n",
1702 uuidp->uuid, strerror(errno));
1706 print_quota(uuidp->uuid, qctl, 1);
1714 static int lfs_quota(int argc, char **argv)
1717 char *name = NULL, *mnt;
1718 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1720 char *obd_type = qctl.obd_type;
1721 char *obd_uuid = qctl.obd_uuid.uuid;
1725 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1728 qctl.qc_type = 0x01;
1731 qctl.qc_type = 0x02;
1734 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1737 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1740 fprintf(stderr, "error: %s: option '-%c' "
1741 "unrecognized\n", argv[0], c);
1750 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1751 if (optind + 2 != argc) {
1752 fprintf(stderr, "error: missing quota argument(s)\n");
1756 name = argv[optind++];
1757 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1759 fprintf(stderr,"error: can't find id for name %s: %s\n",
1760 name, strerror(errno));
1763 print_quota_title(name, &qctl);
1764 } else if (optind + 1 != argc) {
1765 fprintf(stderr, "error: missing quota info argument(s)\n");
1771 rc = llapi_quotactl(mnt, &qctl);
1774 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1775 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1780 rc = id2name(&name, getuid(), qctl.qc_type);
1787 print_quota(mnt, &qctl, 0);
1789 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1790 print_mds_quota(mnt, &qctl);
1791 print_lov_quota(mnt, &qctl);
1796 #endif /* HAVE_QUOTA_SUPPORT */
1798 static int flushctx_ioctl(char *mp)
1802 fd = open(mp, O_RDONLY);
1804 fprintf(stderr, "flushctx: error open %s: %s\n",
1805 mp, strerror(errno));
1809 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1811 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1812 mp, strerror(errno));
1818 static int lfs_flushctx(int argc, char **argv)
1820 int kdestroy = 0, c;
1822 char procline[PATH_MAX], *line;
1826 while ((c = getopt(argc, argv, "k")) != -1) {
1832 fprintf(stderr, "error: %s: option '-%c' "
1833 "unrecognized\n", argv[0], c);
1839 system("kdestroy > /dev/null");
1841 if (optind >= argc) {
1842 /* flush for all mounted lustre fs. */
1843 proc = fopen("/proc/mounts", "r");
1845 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1850 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1855 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1856 fprintf(stderr, "%s: unexpected format in "
1862 if (strcmp(fs, "lustre") != 0)
1864 /* we use '@' to determine it's a client. are there
1865 * any other better way?
1867 if (strchr(dev, '@') == NULL)
1870 if (flushctx_ioctl(mp))
1874 /* flush fs as specified */
1875 while (optind < argc) {
1876 if (flushctx_ioctl(argv[optind++]))
1884 static int lfs_lsetfacl(int argc, char **argv)
1887 return(llapi_lsetfacl(argc, argv));
1890 static int lfs_lgetfacl(int argc, char **argv)
1893 return(llapi_lgetfacl(argc, argv));
1896 static int lfs_rsetfacl(int argc, char **argv)
1899 return(llapi_rsetfacl(argc, argv));
1902 static int lfs_rgetfacl(int argc, char **argv)
1905 return(llapi_rgetfacl(argc, argv));
1908 static int lfs_cp(int argc, char **argv)
1910 return(llapi_cp(argc, argv));
1913 static int lfs_ls(int argc, char **argv)
1915 return(llapi_ls(argc, argv));
1918 int main(int argc, char **argv)
1924 ptl_initialize(argc, argv);
1925 if (obd_initialize(argc, argv) < 0)
1927 if (dbg_initialize(argc, argv) < 0)
1930 Parser_init("lfs > ", cmdlist);
1933 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1935 rc = Parser_commands();
1938 obd_finalize(argc, argv);