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_getfacl(int argc, char **argv);
75 static int lfs_setfacl(int argc, char **argv);
77 /* all avaialable commands */
78 command_t cmdlist[] = {
79 {"setstripe", lfs_setstripe, 0,
80 "Create a new file with a specific striping pattern or\n"
81 "set the default striping pattern on an existing directory or\n"
82 "delete the default striping pattern from an existing directory\n"
83 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
85 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
86 " [--index|-i stripe_index]\n"
87 " [--count|-c stripe_count]\n"
89 " setstripe -d <dirname> (to delete default striping)\n"
90 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
91 "\t Can be specified with k, m or g (in KB, MB and GB respectively)\n"
92 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
93 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
94 {"getstripe", lfs_getstripe, 0,
95 "To list the striping info for a given filename or files in a\n"
96 "directory or recursively for all files in a directory tree.\n"
97 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
98 " [--recursive | -r] <dir|file> ..."},
100 "To find files that match given parameters recursively in a directory tree.\n"
101 "usage: find <dir/file> ... \n"
102 " [[!] --atime|-A [+-]N] [[!] --mtime|-M [+-]N] [[!] --ctime|-C [+-]N]\n"
103 " [--maxdepth|-D N] [[!] --name|-n <pattern>] [--print0|-P]\n"
104 " [--print|-p] [--obd|-O <uuid>] [[!] --type|-t <filetype>]\n"
105 "\t !: used before an option indicates 'NOT' the requested attribute\n"
106 "\t -: used before an value indicates 'AT MOST' the requested value\n"
107 "\t +: used before an option indicates 'AT LEAST' the requested value\n"},
108 {"check", lfs_check, 0,
109 "Display the status of MDS or OSTs (as specified in the command)\n"
110 "or all the servers (MDS and OSTs).\n"
111 "usage: check <osts|mds|servers>"},
112 {"catinfo", lfs_catinfo, 0,
113 "Show information of specified type logs.\n"
114 "usage: catinfo {keyword} [node name]\n"
115 "\tkeywords are one of followings: config, deletions.\n"
116 "\tnode name must be provided when use keyword config."},
117 {"join", lfs_join, 0,
118 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
119 "usage: join <filename_A> <filename_B>\n"},
120 {"osts", lfs_osts, 0, "osts"},
122 "report filesystem disk space usage or inodes usage"
124 "Usage: df [-i] [-h] [path]"},
125 #ifdef HAVE_QUOTA_SUPPORT
126 {"quotachown",lfs_quotachown, 0,
127 "Change files' owner or group on the specified filesystem.\n"
128 "usage: quotachown [-i] <filesystem>\n"
129 "\t-i: ignore error if file is not exist\n"},
130 {"quotacheck", lfs_quotacheck, 0,
131 "Scan the specified filesystem for disk usage, and create,\n"
132 "or update quota files.\n"
133 "usage: quotacheck [ -ug ] <filesystem>"},
134 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
135 "usage: quotaon [ -ugf ] <filesystem>"},
136 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
137 "usage: quotaoff [ -ug ] <filesystem>"},
138 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
139 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
140 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
141 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
142 "usage: quota [ -o obd_uuid ] [{-u|-g <name>}|-t] <filesystem>"},
144 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
145 "usage: flushctx [-k] [mountpoint...]"},
146 {"getfacl", lfs_getfacl, 0,
147 "Get file access control list in remote client.\n"
148 "usage: getfacl [-dRLPvh] file"},
149 {"setfacl", lfs_setfacl, 0,
150 "Set file access control list in remote client.\n"
151 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
152 {"help", Parser_help, 0, "help"},
153 {"exit", Parser_quit, 0, "quit"},
154 {"quit", Parser_quit, 0, "quit"},
159 static int lfs_setstripe(int argc, char **argv)
163 unsigned long long st_size;
164 int st_offset, st_count;
168 char *stripe_size_arg = NULL;
169 char *stripe_off_arg = NULL;
170 char *stripe_count_arg = NULL;
171 unsigned long long size_units;
173 struct option long_opts[] = {
174 {"size", required_argument, 0, 's'},
175 {"count", required_argument, 0, 'c'},
176 {"index", required_argument, 0, 'i'},
177 {"delete", no_argument, 0, 'd'},
184 if (argc == 3 && strcmp(argv[1], "-d") == 0) {
185 /* for compatibility with the existing positional parameter
189 } else if (argc == 5 &&
190 (argv[2][0] != '-' || isdigit(argv[2][1])) &&
191 (argv[3][0] != '-' || isdigit(argv[3][1])) &&
192 (argv[4][0] != '-' || isdigit(argv[4][1])) ) {
193 /* for compatibility with the existing positional parameter
196 stripe_size_arg = argv[2];
197 stripe_off_arg = argv[3];
198 stripe_count_arg = argv[4];
201 while ((c = getopt_long(argc, argv, "c:di:s:",
202 long_opts, NULL)) >= 0)
209 stripe_count_arg = optarg;
212 /* delete the default striping pattern */
216 stripe_off_arg = optarg;
219 stripe_size_arg = optarg;
224 fprintf(stderr, "error: %s: option '%s' "
226 argv[0], argv[optind - 1]);
231 fname = argv[optind];
237 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
238 stripe_count_arg != NULL)) {
239 fprintf(stderr, "error: %s: cannot specify -d with "
240 "-s, -c or -i options\n",
245 if (optind != argc - 1) {
246 fprintf(stderr, "error: %s: only 1 filename|dirname can be "
248 argv[0], argv[argc - 1]);
252 /* get the stripe size */
253 if (stripe_size_arg != NULL) {
254 result = parse_size(stripe_size_arg, &st_size, &size_units);
256 fprintf(stderr,"error: bad size '%s'\n",
261 /* get the stripe offset */
262 if (stripe_off_arg != NULL) {
263 st_offset = strtoul(stripe_off_arg, &end, 0);
265 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
266 argv[0], stripe_off_arg);
270 /* get the stripe count */
271 if (stripe_count_arg != NULL) {
272 st_count = strtoul(stripe_count_arg, &end, 0);
274 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
275 argv[0], stripe_count_arg);
280 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
282 fprintf(stderr, "error: %s: create stripe file failed\n",
288 static int set_time(time_t *time, time_t *set, char *str)
295 else if (str[0] == '-')
301 t = strtol(str, NULL, 0);
302 if (*time < t * 24 * 60 * 60) {
305 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
309 *set = *time - t * 24 * 60 * 60;
313 static int lfs_find(int argc, char **argv)
318 struct find_param param = { .maxdepth = -1 };
320 struct option long_opts[] = {
321 /* New find options. */
322 {"atime", required_argument, 0, 'A'},
323 {"ctime", required_argument, 0, 'C'},
324 {"mtime", required_argument, 0, 'M'},
325 {"maxdepth", required_argument, 0, 'D'},
326 {"name", required_argument, 0, 'n'},
327 /* --obd is considered as a new option. */
328 {"obd", required_argument, 0, 'O'},
329 {"ost", required_argument, 0, 'O'},
330 {"print", no_argument, 0, 'P'},
331 {"print0", no_argument, 0, 'p'},
332 /* Old find options. */
333 {"quiet", no_argument, 0, 'q'},
334 {"recursive", no_argument, 0, 'r'},
335 {"size", required_argument, 0, 's'},
336 {"type", required_argument, 0, 't'},
337 {"verbose", no_argument, 0, 'v'},
349 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpO:qrs:t:v",
350 long_opts, NULL)) >= 0) {
355 /* '!' is part of option */
356 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
357 if (!isoption && pathend != -1) {
358 fprintf(stderr, "err: %s: filename|dirname must either "
359 "precede options or follow options\n",
363 if (!isoption && pathstart == -1)
364 pathstart = optind - 1;
365 if (isoption && pathstart != -1 && pathend == -1) {
366 pathend = optind - 2;
367 if ((c == 1 && strcmp(optarg, "!") == 0) ||
368 c == 'P' || c == 'p' || c == 'O' ||
369 c == 'q' || c == 'r' || c == 'v')
370 pathend = optind - 1;
377 if (strcmp(optarg, "!") == 0)
381 xtime = ¶m.atime;
382 xsign = ¶m.asign;
385 xtime = ¶m.ctime;
386 xsign = ¶m.csign;
390 xtime = ¶m.mtime;
391 xsign = ¶m.msign;
395 if (optarg[0] == '-')
397 else if (optarg[0] == '+')
406 ret = set_time(&t, xtime, optarg);
414 param.maxdepth = strtol(optarg, 0, 0);
418 param.pattern = (char *)optarg;
419 param.exclude_pattern = !!neg_opt;
422 char *buf, *token, *next, *p;
425 len = strlen((char *)optarg);
429 strcpy(buf, (char *)optarg);
431 if (param.num_alloc_obds == 0) {
432 param.obduuid = (struct obd_uuid *)malloc(FIND_MAX_OSTS *
433 sizeof(struct obd_uuid));
434 if (param.obduuid == NULL)
436 param.num_alloc_obds = INIT_ALLOC_NUM_OSTS;
439 for (token = buf; token && *token; token = next) {
440 p = strchr(token, ',');
446 strcpy((char *)¶m.obduuid[param.num_obds++].uuid,
470 param.exclude_type = !!neg_opt;
472 case 'b': param.type = S_IFBLK; break;
473 case 'c': param.type = S_IFCHR; break;
474 case 'd': param.type = S_IFDIR; break;
475 case 'f': param.type = S_IFREG; break;
476 case 'l': param.type = S_IFLNK; break;
477 case 'p': param.type = S_IFIFO; break;
478 case 's': param.type = S_IFSOCK; break;
479 #ifdef S_IFDOOR /* Solaris only */
480 case 'D': param.type = S_IFDOOR; break;
482 default: fprintf(stderr, "error: %s: bad type '%s'\n",
489 if (optarg[0] == '-')
491 else if (optarg[0] == '+')
500 if (optarg[0] == '+')
501 param.size_sign = -1;
502 else if (optarg[0] == '-')
503 param.size_sign = +1;
507 ret = parse_size(optarg, ¶m.size,¶m.size_units);
509 fprintf(stderr,"error: bad size '%s'\n",
522 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
523 argv[0], argv[optind - 1]);
528 if (pathstart == -1) {
529 fprintf(stderr, "error: %s: no filename|pathname\n",
532 } else if (pathend == -1) {
540 static int deprecated_warning;
541 if (!deprecated_warning) {
542 fprintf(stderr, "lfs find: -q, -r, -v options "
543 "deprecated. Use 'lfs getstripe' instead.\n");
544 deprecated_warning = 1;
546 if (!param.recursive && param.maxdepth == -1)
552 ret = llapi_find(argv[pathstart], ¶m);
554 ret = llapi_getstripe(argv[pathstart], ¶m);
555 } while (++pathstart < pathend && !ret);
558 fprintf(stderr, "error: %s failed for %s.\n",
559 argv[0], argv[optind - 1]);
561 if (param.obduuid && param.num_alloc_obds)
567 static int lfs_getstripe(int argc, char **argv)
569 struct option long_opts[] = {
571 {"quiet", 0, 0, 'q'},
572 {"recursive", 0, 0, 'r'},
573 {"verbose", 0, 0, 'v'},
576 char short_opts[] = "hO:qrv";
578 struct find_param param = { 0 };
581 while ((c = getopt_long(argc, argv, short_opts,
582 long_opts, NULL)) != -1) {
587 "error: %s: only one obduuid allowed",
591 param.obduuid = (struct obd_uuid *)optarg;
607 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
608 argv[0], argv[optind - 1]);
616 param.maxdepth = param.recursive ? -1 : 1;
619 rc = llapi_getstripe(argv[optind], ¶m);
620 } while (++optind < argc && !rc);
623 fprintf(stderr, "error: %s failed for %s.\n",
624 argv[0], argv[optind - 1]);
628 static int lfs_osts(int argc, char **argv)
631 struct mntent *mnt = NULL;
632 struct obd_uuid *obduuid = NULL;
633 struct find_param param;
639 fp = setmntent(MOUNTED, "r");
642 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
646 memset(¶m, 0, sizeof(param));
647 param.obduuid = obduuid;
648 while (feof(fp) == 0 && ferror(fp) ==0) {
649 if (llapi_is_lustre_mnt(mnt)) {
650 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
653 "error: %s: failed on %s\n",
654 argv[0], mnt->mnt_dir);
664 #define COOK(value) \
667 while (value > 1024) { \
681 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
683 char rpath[PATH_MAX] = {'\0'};
685 int rc, len, out_len = 0;
687 if (!realpath(path, rpath)) {
689 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
690 path, strerror(-rc));
696 while (feof(fp) == 0 && ferror(fp) == 0) {
697 if (llapi_is_lustre_mnt(mnt)) {
698 len = strlen(mnt->mnt_dir);
700 !strncmp(rpath, mnt->mnt_dir, len)) {
702 memset(mntdir, 0, dir_len);
703 strncpy(mntdir, mnt->mnt_dir, dir_len);
712 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
716 static int showdf(char *mntdir, struct obd_statfs *stat,
717 char *uuid, int ishow, int cooked,
718 char *type, int index, int rc)
720 long long avail, used, total;
722 char *suffix = "KMGTPEZY";
723 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
731 avail = stat->os_ffree;
732 used = stat->os_files - stat->os_ffree;
733 total = stat->os_files;
735 int shift = cooked ? 0 : 10;
737 avail = (stat->os_bavail * stat->os_bsize) >> shift;
738 used = stat->os_blocks - stat->os_bavail;
739 used = (used * stat->os_bsize) >> shift;
740 total = (stat->os_blocks * stat->os_bsize) >> shift;
744 ratio = (double)used / (double)total;
750 cook_val = (double)total;
753 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
755 sprintf(tbuf, CDF, total);
757 cook_val = (double)used;
760 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
762 sprintf(ubuf, CDF, used);
764 cook_val = (double)avail;
767 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
769 sprintf(abuf, CDF, avail);
771 sprintf(tbuf, CDF, total);
772 sprintf(ubuf, CDF, used);
773 sprintf(abuf, CDF, avail);
776 sprintf(rbuf, RDF, (int)(ratio * 100));
777 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
778 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
780 printf("[%s:%d]\n", type, index);
786 printf(UUF": inactive device\n", uuid);
789 printf(UUF": %s\n", uuid, strerror(-rc));
796 static int mntdf(char *mntdir, int ishow, int cooked)
798 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
799 struct obd_uuid uuid_buf;
804 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
805 "UUID", "Inodes", "IUsed", "IFree",
806 "IUse%", "Mounted on");
808 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
809 "UUID", cooked ? "bytes" : "1K-blocks",
810 "Used", "Available", "Use%", "Mounted on");
812 for (index = 0; ; index++) {
813 memset(&stat_buf, 0, sizeof(struct obd_statfs));
814 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
815 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
816 &stat_buf, &uuid_buf);
823 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
824 rc == -ENODATA || rc == 0) {
825 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
829 "error: llapi_obd_statfs(%s): %s (%d)\n",
830 uuid_buf.uuid, strerror(-rc), rc);
834 sum.os_ffree += stat_buf.os_ffree;
835 sum.os_files += stat_buf.os_files;
839 for (index = 0; ; index++) {
840 memset(&stat_buf, 0, sizeof(struct obd_statfs));
841 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
842 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
843 &stat_buf, &uuid_buf);
850 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
851 rc == -ENODATA || rc == 0) {
852 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
856 "error: llapi_obd_statfs failed: %s (%d)\n",
861 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
862 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
863 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
868 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
873 static int lfs_df(int argc, char **argv)
877 struct mntent *mnt = NULL;
878 char mntdir[PATH_MAX] = {'\0'};
879 int ishow = 0, cooked = 0;
883 while ((c = getopt(argc, argv, "ih")) != -1) {
898 fp = setmntent(MOUNTED, "r");
901 fprintf(stderr, "error: %s: open %s failed( %s )\n",
902 argv[0], MOUNTED, strerror(errno));
906 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
912 rc = mntdf(mntdir, ishow, cooked);
917 while (feof(fp) == 0 && ferror(fp) == 0) {
918 if (llapi_is_lustre_mnt(mnt)) {
919 rc = mntdf(mnt->mnt_dir, ishow, cooked);
932 static int lfs_check(int argc, char **argv)
936 struct mntent *mnt = NULL;
945 obd_types[0] = obd_type1;
946 obd_types[1] = obd_type2;
948 if (strcmp(argv[1], "osts") == 0) {
949 strcpy(obd_types[0], "osc");
950 } else if (strcmp(argv[1], "mds") == 0) {
951 strcpy(obd_types[0], "mdc");
952 } else if (strcmp(argv[1], "servers") == 0) {
954 strcpy(obd_types[0], "osc");
955 strcpy(obd_types[1], "mdc");
957 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
962 fp = setmntent(MOUNTED, "r");
964 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
968 while (feof(fp) == 0 && ferror(fp) ==0) {
969 if (llapi_is_lustre_mnt(mnt))
977 fprintf(stderr, "No suitable Lustre mount found\n");
981 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
984 fprintf(stderr, "error: %s: %s status failed\n",
991 static int lfs_catinfo(int argc, char **argv)
994 struct mntent *mnt = NULL;
997 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
1000 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
1003 fp = setmntent(MOUNTED, "r");
1005 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
1008 mnt = getmntent(fp);
1009 while (feof(fp) == 0 && ferror(fp) == 0) {
1010 if (llapi_is_lustre_mnt(mnt))
1012 mnt = getmntent(fp);
1019 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
1021 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
1023 fprintf(stderr, "no lustre_lite mounted.\n");
1030 int lfs_join(int argc, char **argv)
1032 char *name_head, *name_tail;
1038 name_head = argv[1];
1039 fd = open(name_head, O_WRONLY);
1041 fprintf(stderr, "Can not open name_head %s rc=%d\n",
1045 size = lseek(fd, 0, SEEK_END);
1046 if (size % JOIN_FILE_ALIGN) {
1047 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
1048 name_head, (long long)size, JOIN_FILE_ALIGN);
1052 name_tail = argv[2];
1053 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1057 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1063 #ifdef HAVE_QUOTA_SUPPORT
1064 static int lfs_quotachown(int argc, char **argv)
1070 while ((c = getopt(argc, argv, "i")) != -1) {
1076 fprintf(stderr, "error: %s: option '-%c' "
1077 "unrecognized\n", argv[0], c);
1083 rc = llapi_quotachown(argv[optind], flag);
1085 fprintf(stderr,"error: change file owner/group failed.\n");
1090 static int lfs_quotacheck(int argc, char **argv)
1092 int c, check_type = 0;
1094 struct if_quotacheck qchk;
1095 struct if_quotactl qctl;
1096 char *obd_type = qchk.obd_type;
1097 char *obd_uuid = qchk.obd_uuid.uuid;
1100 memset(&qchk, 0, sizeof(qchk));
1103 while ((c = getopt(argc, argv, "ug")) != -1) {
1112 fprintf(stderr, "error: %s: option '-%c' "
1113 "unrecognized\n", argv[0], c);
1120 else /* do quotacheck for both user & group quota by default */
1128 memset(&qctl, 0, sizeof(qctl));
1129 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1130 qctl.qc_id = QFMT_LDISKFS;
1131 qctl.qc_type = check_type;
1132 rc = llapi_quotactl(mnt, &qctl);
1134 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1138 rc = llapi_quotacheck(mnt, check_type);
1140 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1144 rc = llapi_poll_quotacheck(mnt, &qchk);
1147 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1148 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1152 memset(&qctl, 0, sizeof(qctl));
1153 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1154 qctl.qc_id = QFMT_LDISKFS;
1155 qctl.qc_type = check_type;
1156 rc = llapi_quotactl(mnt, &qctl);
1159 fprintf(stderr, "%s %s ",
1160 qctl.obd_type, qctl.obd_uuid.uuid);
1161 fprintf(stderr, "%s turn on quota failed: %s\n",
1162 argv[0], strerror(errno));
1169 static int lfs_quotaon(int argc, char **argv)
1173 struct if_quotactl qctl;
1174 char *obd_type = qctl.obd_type;
1175 char *obd_uuid = qctl.obd_uuid.uuid;
1178 memset(&qctl, 0, sizeof(qctl));
1179 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1180 qctl.qc_id = QFMT_LDISKFS;
1183 while ((c = getopt(argc, argv, "ugf")) != -1) {
1186 qctl.qc_type |= 0x01;
1189 qctl.qc_type |= 0x02;
1192 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1195 fprintf(stderr, "error: %s: option '-%c' "
1196 "unrecognized\n", argv[0], c);
1203 else /* by default, enable quota for both user & group */
1204 qctl.qc_type = 0x02;
1211 rc = llapi_quotactl(mnt, &qctl);
1214 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1215 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1222 static int lfs_quotaoff(int argc, char **argv)
1226 struct if_quotactl qctl;
1227 char *obd_type = qctl.obd_type;
1228 char *obd_uuid = qctl.obd_uuid.uuid;
1231 memset(&qctl, 0, sizeof(qctl));
1232 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1235 while ((c = getopt(argc, argv, "ug")) != -1) {
1238 qctl.qc_type |= 0x01;
1241 qctl.qc_type |= 0x02;
1244 fprintf(stderr, "error: %s: option '-%c' "
1245 "unrecognized\n", argv[0], c);
1252 else /* by default, disable quota for both user & group */
1253 qctl.qc_type = 0x02;
1260 rc = llapi_quotactl(mnt, &qctl);
1263 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1264 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1271 static int name2id(unsigned int *id, char *name, int type)
1273 if (type == USRQUOTA) {
1274 struct passwd *entry;
1276 if (!(entry = getpwnam(name))) {
1282 *id = entry->pw_uid;
1284 struct group *entry;
1286 if (!(entry = getgrnam(name))) {
1292 *id = entry->gr_gid;
1298 static int id2name(char **name, unsigned int id, int type)
1300 if (type == USRQUOTA) {
1301 struct passwd *entry;
1303 if (!(entry = getpwuid(id))) {
1309 *name = entry->pw_name;
1311 struct group *entry;
1313 if (!(entry = getgrgid(id))) {
1319 *name = entry->gr_name;
1325 #define ARG2INT(nr, str, msg) \
1328 nr = strtol(str, &endp, 0); \
1330 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1335 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
1337 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
1338 * returns the value or ULONG_MAX on integer overflow or incorrect format
1340 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
1341 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
1342 * 3. empty integer value is interpreted as 0
1345 static unsigned long str2sec(const char* timestr) {
1346 const char spec[] = "smhdw";
1347 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1348 unsigned long val = 0;
1351 if (strpbrk(timestr, spec) == NULL) {
1352 /* no specifiers inside the time string,
1353 should treat it as an integer value */
1354 val = strtoul(timestr, &tail, 10);
1355 return *tail ? ULONG_MAX : val;
1358 /* format string is XXwXXdXXhXXmXXs */
1364 v = strtoul(timestr, &tail, 10);
1365 if (v == ULONG_MAX || *tail == '\0')
1366 /* value too large (ULONG_MAX or more)
1367 or missing specifier */
1370 ptr = strchr(spec, *tail);
1372 /* unknown specifier */
1377 /* check if product will overflow the type */
1378 if (!(v < ULONG_MAX / mult[ind]))
1381 ADD_OVERFLOW(val, mult[ind] * v);
1382 if (val == ULONG_MAX)
1394 int lfs_setquota(int argc, char **argv)
1398 struct if_quotactl qctl;
1399 char *obd_type = qctl.obd_type;
1400 char *obd_uuid = qctl.obd_uuid.uuid;
1403 memset(&qctl, 0, sizeof(qctl));
1404 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1407 while ((c = getopt(argc, argv, "ugt")) != -1) {
1410 qctl.qc_type |= 0x01;
1413 qctl.qc_type |= 0x02;
1416 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1419 fprintf(stderr, "error: %s: option '-%c' "
1420 "unrecognized\n", argv[0], c);
1428 if (qctl.qc_type == UGQUOTA) {
1429 fprintf(stderr, "error: user and group quotas can't be set "
1434 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1435 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1437 if (optind + 6 != argc)
1440 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1442 fprintf(stderr, "error: find id for name %s failed: %s\n",
1443 argv[optind - 1], strerror(errno));
1447 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1448 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1449 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1450 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1452 dqb->dqb_valid = QIF_LIMITS;
1454 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1456 if (optind + 3 != argc)
1459 if ((dqi->dqi_bgrace = str2sec(argv[optind++])) == ULONG_MAX) {
1460 fprintf(stderr, "error: bad %s: %s\n", "block-grace", argv[optind - 1]);
1463 if ((dqi->dqi_igrace = str2sec(argv[optind++])) == ULONG_MAX) {
1464 fprintf(stderr, "error: bad %s: %s\n", "inode-grace", argv[optind - 1]);
1471 rc = llapi_quotactl(mnt, &qctl);
1474 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1475 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1482 static inline char *type2name(int check_type)
1484 if (check_type == USRQUOTA)
1486 else if (check_type == GRPQUOTA)
1493 /* Converts seconds value into format string
1494 * result is returned in buf
1496 * 1. result is in descenting order: 1w2d3h4m5s
1497 * 2. zero fields are not filled (except for p. 3): 5d1s
1498 * 3. zero seconds value is presented as "0s"
1500 static void sec2str(time_t seconds, char *buf)
1502 const char spec[] = "smhdw";
1503 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
1508 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
1509 c = seconds / mult[i];
1511 if (c > 0 || (i == 0 && buf == tail))
1512 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
1519 static void diff2str(time_t seconds, char *buf, time_t now)
1525 if (seconds <= now) {
1526 strcpy(buf, "none");
1529 sec2str(seconds - now, buf);
1532 static void print_quota_title(char *name, struct if_quotactl *qctl)
1534 printf("Disk quotas for %s %s (%cid %u):\n",
1535 type2name(qctl->qc_type), name,
1536 *type2name(qctl->qc_type), qctl->qc_id);
1537 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1539 "kbytes", "quota", "limit", "grace",
1540 "files", "quota", "limit", "grace");
1543 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1549 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1550 int bover = 0, iover = 0;
1551 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1553 if (dqb->dqb_bhardlimit &&
1554 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1556 } else if (dqb->dqb_bsoftlimit &&
1557 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1558 if (dqb->dqb_btime > now) {
1565 if (dqb->dqb_ihardlimit &&
1566 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1568 } else if (dqb->dqb_isoftlimit &&
1569 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1570 if (dqb->dqb_btime > now) {
1577 #if 0 /* XXX: always print quotas even when no usages */
1578 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1584 if (strlen(mnt) > 15)
1585 printf("%s\n%15s", mnt, "");
1587 printf("%15s", mnt);
1590 diff2str(dqb->dqb_btime, timebuf, now);
1592 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1593 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1594 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1595 printf(" %7s%c %6s %7s %7s",
1596 numbuf[0], bover ? '*' : ' ', numbuf[1],
1597 numbuf[2], bover > 1 ? timebuf : "");
1600 diff2str(dqb->dqb_itime, timebuf, now);
1602 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1603 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1604 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1606 printf(" %7s%c %6s %7s %7s",
1607 numbuf[0], iover ? '*' : ' ', numbuf[1],
1608 numbuf[2], iover > 1 ? timebuf : "");
1611 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1612 qctl->qc_cmd == Q_GETOINFO) {
1616 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1617 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1618 printf("Block grace time: %s; Inode grace time: %s\n",
1619 bgtimebuf, igtimebuf);
1623 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1627 /* XXX: this is a flag to mark that only mds quota is wanted */
1628 qctl->qc_dqblk.dqb_valid = 1;
1629 rc = llapi_quotactl(mnt, qctl);
1631 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1634 qctl->qc_dqblk.dqb_valid = 0;
1636 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1639 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1642 struct obd_uuid *uuids = NULL, *uuidp;
1643 int obdcount = 1024;
1648 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1652 uuids = (struct obd_uuid *)malloc(INIT_ALLOC_NUM_OSTS *
1653 sizeof(struct obd_uuid));
1658 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1660 struct obd_uuid *uuids_temp;
1662 if (rc == -EOVERFLOW) {
1663 uuids_temp = realloc(uuids, obdcount *
1664 sizeof(struct obd_uuid));
1665 if (uuids_temp != NULL)
1666 goto retry_get_uuids;
1671 fprintf(stderr, "get ost uuid failed: %s\n", strerror(rc));
1675 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1676 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1678 /* XXX clear this flag to get quota from osts */
1679 qctl->qc_dqblk.dqb_valid = 0;
1680 rc = llapi_quotactl(mnt, qctl);
1682 fprintf(stderr, "%s quotactl failed: %s\n",
1683 uuidp->uuid, strerror(errno));
1687 print_quota(uuidp->uuid, qctl, 1);
1695 static int lfs_quota(int argc, char **argv)
1698 char *name = NULL, *mnt;
1699 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
1701 char *obd_type = qctl.obd_type;
1702 char *obd_uuid = qctl.obd_uuid.uuid;
1706 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1709 qctl.qc_type = 0x01;
1712 qctl.qc_type = 0x02;
1715 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1718 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1721 fprintf(stderr, "error: %s: option '-%c' "
1722 "unrecognized\n", argv[0], c);
1731 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1732 if (optind + 2 != argc) {
1733 fprintf(stderr, "error: missing quota argument(s)\n");
1737 name = argv[optind++];
1738 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1740 fprintf(stderr,"error: can't find id for name %s: %s\n",
1741 name, strerror(errno));
1744 print_quota_title(name, &qctl);
1745 } else if (optind + 1 != argc) {
1746 fprintf(stderr, "error: missing quota info argument(s)\n");
1752 rc = llapi_quotactl(mnt, &qctl);
1755 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1756 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1761 rc = id2name(&name, getuid(), qctl.qc_type);
1768 print_quota(mnt, &qctl, 0);
1770 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1771 print_mds_quota(mnt, &qctl);
1772 print_lov_quota(mnt, &qctl);
1777 #endif /* HAVE_QUOTA_SUPPORT */
1779 static int flushctx_ioctl(char *mp)
1783 fd = open(mp, O_RDONLY);
1785 fprintf(stderr, "flushctx: error open %s: %s\n",
1786 mp, strerror(errno));
1790 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1792 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1793 mp, strerror(errno));
1799 static int lfs_flushctx(int argc, char **argv)
1801 int kdestroy = 0, c;
1803 char procline[PATH_MAX], *line;
1807 while ((c = getopt(argc, argv, "k")) != -1) {
1813 fprintf(stderr, "error: %s: option '-%c' "
1814 "unrecognized\n", argv[0], c);
1820 system("kdestroy > /dev/null");
1822 if (optind >= argc) {
1823 /* flush for all mounted lustre fs. */
1824 proc = fopen("/proc/mounts", "r");
1826 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1831 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1836 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1837 fprintf(stderr, "%s: unexpected format in "
1843 if (strcmp(fs, "lustre") != 0)
1845 /* we use '@' to determine it's a client. are there
1846 * any other better way?
1848 if (strchr(dev, '@') == NULL)
1851 if (flushctx_ioctl(mp))
1855 /* flush fs as specified */
1856 while (optind < argc) {
1857 if (flushctx_ioctl(argv[optind++]))
1866 * We assume one and only one filename is supplied as the
1869 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1871 char *dname, *rpath = NULL;
1872 char path[PATH_MAX], cwd[PATH_MAX];
1880 /* FIXME the premise is there is no sub-mounted filesystems under this
1881 * mounted lustre tree. */
1882 strncpy(fname, argv[argc - 1], PATH_MAX);
1884 /* get path prefix */
1885 dname = dirname(fname);
1887 /* try to resolve the pathname into relative to the root of the mounted
1888 * lustre filesystem.
1890 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1891 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1895 if (chdir(dname) == -1) {
1896 fprintf(stderr, "chdir to %s failed: %s\n",
1897 dname, strerror(errno));
1901 if (getcwd(path, sizeof(path)) == NULL) {
1902 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1906 if (chdir(cwd) == -1) {
1907 fprintf(stderr, "chdir back to %s: %s\n",
1908 cwd, strerror(errno));
1912 strncat(path, "/", PATH_MAX);
1913 strncpy(fname, argv[argc - 1], PATH_MAX);
1914 strncat(path, basename(fname), PATH_MAX);
1916 fp = setmntent(MOUNTED, "r");
1918 fprintf(stderr, "setmntent %s failed: %s\n",
1919 MOUNTED, strerror(errno));
1924 mnt = getmntent(fp);
1928 if (!llapi_is_lustre_mnttype(mnt->mnt_type))
1931 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1932 rpath = path + strlen(mnt->mnt_dir);
1938 /* remove char '/' from rpath to be a relative path */
1939 while (rpath && *rpath == '/') rpath++;
1943 "%s: file %s doesn't belong to a lustre file system!\n",
1944 argv[0], argv[argc - 1]);
1948 for (i = 0; i < argc - 1; i++) {
1949 strncat(cmd, argv[i], PATH_MAX);
1950 strncat(cmd, " ", PATH_MAX);
1952 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1953 strncpy(fname, argv[argc - 1], sizeof(fname));
1958 static int lfs_getfacl(int argc, char **argv)
1960 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1962 if (acl_cmd_parse(argc, argv, fname, cmd))
1965 return llapi_getfacl(fname, cmd);
1968 static int lfs_setfacl(int argc, char **argv)
1970 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1972 if (acl_cmd_parse(argc, argv, fname, cmd))
1975 return llapi_setfacl(fname, cmd);
1978 int main(int argc, char **argv)
1984 ptl_initialize(argc, argv);
1985 if (obd_initialize(argc, argv) < 0)
1987 if (dbg_initialize(argc, argv) < 0)
1990 Parser_init("lfs > ", cmdlist);
1993 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1995 rc = Parser_commands();
1998 obd_finalize(argc, argv);