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.
35 #include <sys/types.h>
42 #include <lnet/api-support.h>
43 #include <lnet/lnetctl.h>
45 #include <liblustre.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre/liblustreapi.h>
48 #include <lustre/lustre_user.h>
53 unsigned int libcfs_subsystem_debug = 0;
56 static int lfs_setstripe(int argc, char **argv);
57 static int lfs_find(int argc, char **argv);
58 static int lfs_getstripe(int argc, char **argv);
59 static int lfs_osts(int argc, char **argv);
60 static int lfs_df(int argc, char **argv);
61 static int lfs_check(int argc, char **argv);
62 static int lfs_catinfo(int argc, char **argv);
63 #ifdef HAVE_QUOTA_SUPPORT
64 static int lfs_quotachown(int argc, char **argv);
65 static int lfs_quotacheck(int argc, char **argv);
66 static int lfs_quotaon(int argc, char **argv);
67 static int lfs_quotaoff(int argc, char **argv);
68 static int lfs_setquota(int argc, char **argv);
69 static int lfs_quota(int argc, char **argv);
71 static int lfs_flushctx(int argc, char **argv);
72 static int lfs_join(int argc, char **argv);
73 static int lfs_getfacl(int argc, char **argv);
74 static int lfs_setfacl(int argc, char **argv);
76 /* all avaialable commands */
77 command_t cmdlist[] = {
78 {"setstripe", lfs_setstripe, 0,
79 "Create a new file with a specific striping pattern or\n"
80 "set the default striping pattern on an existing directory or\n"
81 "delete the default striping pattern from an existing directory\n"
82 "usage: setstripe <filename|dirname> <stripe_size> <stripe_index> <stripe_count>\n"
84 " setstripe <filename|dirname> [--size|-s stripe_size]\n"
85 " [--index|-i stripe_index]\n"
86 " [--count|-c stripe_count]\n"
88 " setstripe -d <dirname> (to delete default striping)\n"
89 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"
90 "\tstripe_index: OST index of first stripe (-1 filesystem default)\n"
91 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)"},
92 {"getstripe", lfs_getstripe, 0,
93 "To list the striping info for a given filename or files in a\n"
94 "directory or recursively for all files in a directory tree.\n"
95 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
96 " [--recursive | -r] <dir|file> ..."},
98 "To find files that match given parameters recursively in a directory tree.\n"
99 "usage: find <dir/file> ... \n"
100 " [[!] --atime|-A N] [[!] --mtime|-M N] [[!] --ctime|-C N] [--maxdepth|-D N]\n"
101 " [[!] --name|-n <pattern>] [--print0|-P] [--print|-p] [--obd|-O <uuid>]\n"
102 "\t !: used before --atime, --mtime, --ctime specifies the negative value\n"
103 "\t !: used before --name means find exclude the regular expression pattern\n"
104 "If one of the options below is provided, find works the same as 'getstripe':\n"
105 "To list the striping info for a given filename or files in a directory or\n"
107 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
108 " [--recursive | -r] <dir|file> ..."},
109 {"check", lfs_check, 0,
110 "Display the status of MDS or OSTs (as specified in the command)\n"
111 "or all the servers (MDS and OSTs).\n"
112 "usage: check <osts|mds|servers>"},
113 {"catinfo", lfs_catinfo, 0,
114 "Show information of specified type logs.\n"
115 "usage: catinfo {keyword} [node name]\n"
116 "\tkeywords are one of followings: config, deletions.\n"
117 "\tnode name must be provided when use keyword config."},
118 {"join", lfs_join, 0,
119 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
120 "usage: join <filename_A> <filename_B>\n"},
121 {"osts", lfs_osts, 0, "osts"},
123 "report filesystem disk space usage or inodes usage"
125 "Usage: df [-i] [-h] [path]"},
126 #ifdef HAVE_QUOTA_SUPPORT
127 {"quotachown",lfs_quotachown, 0,
128 "Change files' owner or group on the specified filesystem.\n"
129 "usage: quotachown [-i] <filesystem>\n"
130 "\t-i: ignore error if file is not exist\n"},
131 {"quotacheck", lfs_quotacheck, 0,
132 "Scan the specified filesystem for disk usage, and create,\n"
133 "or update quota files.\n"
134 "usage: quotacheck [ -ug ] <filesystem>"},
135 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
136 "usage: quotaon [ -ugf ] <filesystem>"},
137 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
138 "usage: quotaoff [ -ug ] <filesystem>"},
139 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
140 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
141 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
142 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
143 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
145 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
146 "usage: flushctx [-k] [mountpoint...]"},
147 {"getfacl", lfs_getfacl, 0,
148 "Get file access control list in remote client.\n"
149 "usage: getfacl [-dRLPvh] file"},
150 {"setfacl", lfs_setfacl, 0,
151 "Set file access control list in remote client.\n"
152 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
153 {"help", Parser_help, 0, "help"},
154 {"exit", Parser_quit, 0, "quit"},
155 {"quit", Parser_quit, 0, "quit"},
160 static int lfs_setstripe(int argc, char **argv)
165 int st_offset, st_count;
169 char *stripe_size_arg = NULL;
170 char *stripe_off_arg = NULL;
171 char *stripe_count_arg = NULL;
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 st_size = strtoul(stripe_size_arg, &end, 0);
256 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
257 argv[0], stripe_size_arg);
262 /* get the stripe offset */
263 if (stripe_off_arg != NULL) {
264 st_offset = strtoul(stripe_off_arg, &end, 0);
266 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
267 argv[0], stripe_off_arg);
271 /* get the stripe count */
272 if (stripe_count_arg != NULL) {
273 st_count = strtoul(stripe_count_arg, &end, 0);
275 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
276 argv[0], stripe_count_arg);
281 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
283 fprintf(stderr, "error: %s: create stripe file failed\n",
289 static int set_time(time_t *time, time_t *set, char *str)
296 else if (str[0] == '-')
302 t = strtol(str, NULL, 0);
303 if (*time < t * 24 * 60 * 60) {
306 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
310 *set = *time - t * 24 * 60 * 60;
314 static int lfs_find(int argc, char **argv)
321 int quiet, verbose, recursive;
322 struct find_param param;
323 struct obd_uuid *obduuid = NULL;
325 struct option long_opts[] = {
326 /* New find options. */
327 {"atime", required_argument, 0, 'A'},
328 {"ctime", required_argument, 0, 'C'},
329 {"mtime", required_argument, 0, 'M'},
330 {"maxdepth", required_argument, 0, 'D'},
331 {"name", required_argument, 0, 'n'},
332 /* --obd is considered as a new option. */
333 {"obd", required_argument, 0, 'O'},
334 {"ost", required_argument, 0, 'O'},
335 {"print", no_argument, 0, 'P'},
336 {"print0", no_argument, 0, 'p'},
337 /* Old find options. */
338 {"quiet", no_argument, 0, 'q'},
339 {"recursive", no_argument, 0, 'r'},
340 {"verbose", no_argument, 0, 'v'},
353 quiet = verbose = recursive = 0;
355 memset(¶m, 0, sizeof(param));
357 while ((c = getopt_long_only(argc, argv, "-A:C:D:M:n:PpOqrv",
358 long_opts, NULL)) >= 0)
364 /* '!' is part of option */
365 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
366 if (!isoption && pathend != -1) {
367 fprintf(stderr, "err: %s: filename|dirname must either "
368 "precede options or follow options\n",
372 if (!isoption && pathstart == -1)
373 pathstart = optind - 1;
374 if (isoption && pathstart != -1 && pathend == -1) {
375 pathend = optind - 2;
376 if ((c == 1 && strcmp(optarg, "!") == 0) ||
377 c == 'P' || c == 'p' || c == 'O' ||
378 c == 'q' || c == 'r' || c == 'v')
379 pathend = optind - 1;
386 if (strcmp(optarg, "!") == 0)
390 xtime = ¶m.atime;
391 xsign = ¶m.asign;
394 xtime = ¶m.ctime;
395 xsign = ¶m.csign;
399 xtime = ¶m.mtime;
400 xsign = ¶m.msign;
404 if (optarg[0] == '-')
406 else if (optarg[0] == '+')
411 strcat(timestr, optarg);
415 ret = set_time(&t, xtime, optarg);
422 depth = strtol(optarg, 0, 0);
426 param.pattern = (char *)optarg;
428 param.exclude_pattern = 1;
430 param.exclude_pattern = 0;
435 "error: %s: only one obduuid allowed",
439 obduuid = (struct obd_uuid *)optarg;
463 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
464 argv[0], argv[optind - 1]);
469 if (pathstart == -1) {
470 fprintf(stderr, "error: %s: no filename|pathname\n",
473 } else if (pathend == -1) {
478 param.obduuid = obduuid;
480 param.maxdepth = depth;
481 param.zeroend = zeroend;
484 param.recursive = recursive;
485 param.verbose = verbose;
487 param.maxdepth = recursive ? -1 : 1;
492 ret = llapi_find(argv[pathstart], ¶m);
494 ret = llapi_getstripe(argv[pathstart], ¶m);
495 } while (++pathstart < pathend && !ret);
498 fprintf(stderr, "error: %s failed for %s.\n",
499 argv[0], argv[optind - 1]);
503 static int lfs_getstripe(int argc, char **argv)
505 struct option long_opts[] = {
507 {"quiet", 0, 0, 'q'},
508 {"recursive", 0, 0, 'r'},
509 {"verbose", 0, 0, 'v'},
512 char short_opts[] = "hO:qrv";
513 int quiet, verbose, recursive, c, rc;
514 struct obd_uuid *obduuid = NULL;
515 struct find_param param;
518 quiet = verbose = recursive = 0;
519 while ((c = getopt_long(argc, argv, short_opts,
520 long_opts, NULL)) != -1) {
525 "error: %s: only one obduuid allowed",
529 obduuid = (struct obd_uuid *)optarg;
545 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
546 argv[0], argv[optind - 1]);
554 memset(¶m, 0, sizeof(param));
555 param.recursive = recursive;
556 param.verbose = verbose;
558 param.obduuid = obduuid;
559 param.maxdepth = recursive ? -1 : 1;
562 rc = llapi_getstripe(argv[optind], ¶m);
563 } while (++optind < argc && !rc);
566 fprintf(stderr, "error: %s failed for %s.\n",
567 argv[0], argv[optind - 1]);
571 static int lfs_osts(int argc, char **argv)
574 struct mntent *mnt = NULL;
575 struct obd_uuid *obduuid = NULL;
576 struct find_param param;
582 fp = setmntent(MOUNTED, "r");
585 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
589 memset(¶m, 0, sizeof(param));
590 param.obduuid = obduuid;
591 while (feof(fp) == 0 && ferror(fp) ==0) {
592 if (llapi_is_lustre_mnt(mnt)) {
593 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
596 "error: %s: failed on %s\n",
597 argv[0], mnt->mnt_dir);
607 #define COOK(value) \
610 while (value > 1024) { \
624 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
626 char rpath[PATH_MAX] = {'\0'};
628 int rc, len, out_len = 0;
630 if (!realpath(path, rpath)) {
632 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
633 path, strerror(-rc));
639 while (feof(fp) == 0 && ferror(fp) == 0) {
640 if (llapi_is_lustre_mnt(mnt)) {
641 len = strlen(mnt->mnt_dir);
643 !strncmp(rpath, mnt->mnt_dir, len)) {
645 memset(mntdir, 0, dir_len);
646 strncpy(mntdir, mnt->mnt_dir, dir_len);
655 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
659 static int showdf(char *mntdir, struct obd_statfs *stat,
660 char *uuid, int ishow, int cooked,
661 char *type, int index, int rc)
663 long long avail, used, total;
665 char *suffix = "KMGTPEZY";
666 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
674 avail = stat->os_ffree;
675 used = stat->os_files - stat->os_ffree;
676 total = stat->os_files;
678 int shift = cooked ? 0 : 10;
680 avail = (stat->os_bavail * stat->os_bsize) >> shift;
681 used = stat->os_blocks - stat->os_bavail;
682 used = (used * stat->os_bsize) >> shift;
683 total = (stat->os_blocks * stat->os_bsize) >> shift;
687 ratio = (double)used / (double)total;
693 cook_val = (double)total;
696 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
698 sprintf(tbuf, CDF, total);
700 cook_val = (double)used;
703 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
705 sprintf(ubuf, CDF, used);
707 cook_val = (double)avail;
710 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
712 sprintf(abuf, CDF, avail);
714 sprintf(tbuf, CDF, total);
715 sprintf(ubuf, CDF, used);
716 sprintf(abuf, CDF, avail);
719 sprintf(rbuf, RDF, (int)(ratio * 100));
720 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
721 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
723 printf("[%s:%d]\n", type, index);
729 printf(UUF": inactive device\n", uuid);
732 printf(UUF": %s\n", uuid, strerror(-rc));
739 static int mntdf(char *mntdir, int ishow, int cooked)
741 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
742 struct obd_uuid uuid_buf;
747 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
748 "UUID", "Inodes", "IUsed", "IFree",
749 "IUse%", "Mounted on");
751 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
752 "UUID", cooked ? "bytes" : "1K-blocks",
753 "Used", "Available", "Use%", "Mounted on");
755 for (index = 0; ; index++) {
756 memset(&stat_buf, 0, sizeof(struct obd_statfs));
757 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
758 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
759 &stat_buf, &uuid_buf);
766 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
767 rc == -ENODATA || rc == 0) {
768 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
772 "error: llapi_obd_statfs(%s): %s (%d)\n",
773 uuid_buf.uuid, strerror(-rc), rc);
777 sum.os_ffree += stat_buf.os_ffree;
778 sum.os_files += stat_buf.os_files;
782 for (index = 0; ; index++) {
783 memset(&stat_buf, 0, sizeof(struct obd_statfs));
784 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
785 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
786 &stat_buf, &uuid_buf);
793 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
794 rc == -ENODATA || rc == 0) {
795 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
799 "error: llapi_obd_statfs failed: %s (%d)\n",
804 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
805 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
806 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
811 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
816 static int lfs_df(int argc, char **argv)
820 struct mntent *mnt = NULL;
821 char mntdir[PATH_MAX] = {'\0'};
822 int ishow = 0, cooked = 0;
826 while ((c = getopt(argc, argv, "ih")) != -1) {
841 fp = setmntent(MOUNTED, "r");
844 fprintf(stderr, "error: %s: open %s failed( %s )\n",
845 argv[0], MOUNTED, strerror(errno));
849 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
855 rc = mntdf(mntdir, ishow, cooked);
860 while (feof(fp) == 0 && ferror(fp) == 0) {
861 if (llapi_is_lustre_mnt(mnt)) {
862 rc = mntdf(mnt->mnt_dir, ishow, cooked);
875 static int lfs_check(int argc, char **argv)
879 struct mntent *mnt = NULL;
888 obd_types[0] = obd_type1;
889 obd_types[1] = obd_type2;
891 if (strcmp(argv[1], "osts") == 0) {
892 strcpy(obd_types[0], "osc");
893 } else if (strcmp(argv[1], "mds") == 0) {
894 strcpy(obd_types[0], "mdc");
895 } else if (strcmp(argv[1], "servers") == 0) {
897 strcpy(obd_types[0], "osc");
898 strcpy(obd_types[1], "mdc");
900 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
905 fp = setmntent(MOUNTED, "r");
907 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
911 while (feof(fp) == 0 && ferror(fp) ==0) {
912 if (llapi_is_lustre_mnt(mnt))
920 fprintf(stderr, "No suitable Lustre mount found\n");
924 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
927 fprintf(stderr, "error: %s: %s status failed\n",
934 static int lfs_catinfo(int argc, char **argv)
937 struct mntent *mnt = NULL;
940 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
943 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
946 fp = setmntent(MOUNTED, "r");
948 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
952 while (feof(fp) == 0 && ferror(fp) == 0) {
953 if (llapi_is_lustre_mnt(mnt))
962 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
964 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
966 fprintf(stderr, "no lustre_lite mounted.\n");
973 int lfs_join(int argc, char **argv)
975 char *name_head, *name_tail;
982 fd = open(name_head, O_WRONLY);
984 fprintf(stderr, "Can not open name_head %s rc=%d\n",
988 size = lseek(fd, 0, SEEK_END);
989 if (size % JOIN_FILE_ALIGN) {
990 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
991 name_head, (long long)size, JOIN_FILE_ALIGN);
996 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
1000 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
1006 #ifdef HAVE_QUOTA_SUPPORT
1007 static int lfs_quotachown(int argc, char **argv)
1013 while ((c = getopt(argc, argv, "i")) != -1) {
1019 fprintf(stderr, "error: %s: option '-%c' "
1020 "unrecognized\n", argv[0], c);
1026 rc = llapi_quotachown(argv[optind], flag);
1028 fprintf(stderr,"error: change file owner/group failed.\n");
1033 static int lfs_quotacheck(int argc, char **argv)
1035 int c, check_type = 0;
1037 struct if_quotacheck qchk;
1038 struct if_quotactl qctl;
1039 char *obd_type = qchk.obd_type;
1040 char *obd_uuid = qchk.obd_uuid.uuid;
1043 memset(&qchk, 0, sizeof(qchk));
1046 while ((c = getopt(argc, argv, "ug")) != -1) {
1055 fprintf(stderr, "error: %s: option '-%c' "
1056 "unrecognized\n", argv[0], c);
1063 else /* do quotacheck for both user & group quota by default */
1071 memset(&qctl, 0, sizeof(qctl));
1072 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1073 qctl.qc_id = QFMT_LDISKFS;
1074 qctl.qc_type = check_type;
1075 rc = llapi_quotactl(mnt, &qctl);
1077 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
1081 rc = llapi_quotacheck(mnt, check_type);
1083 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
1087 rc = llapi_poll_quotacheck(mnt, &qchk);
1090 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1091 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
1095 memset(&qctl, 0, sizeof(qctl));
1096 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1097 qctl.qc_id = QFMT_LDISKFS;
1098 qctl.qc_type = check_type;
1099 rc = llapi_quotactl(mnt, &qctl);
1102 fprintf(stderr, "%s %s ",
1103 qctl.obd_type, qctl.obd_uuid.uuid);
1104 fprintf(stderr, "%s turn on quota failed: %s\n",
1105 argv[0], strerror(errno));
1112 static int lfs_quotaon(int argc, char **argv)
1116 struct if_quotactl qctl;
1117 char *obd_type = qctl.obd_type;
1118 char *obd_uuid = qctl.obd_uuid.uuid;
1121 memset(&qctl, 0, sizeof(qctl));
1122 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
1123 qctl.qc_id = QFMT_LDISKFS;
1126 while ((c = getopt(argc, argv, "ugf")) != -1) {
1129 qctl.qc_type |= 0x01;
1132 qctl.qc_type |= 0x02;
1135 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1138 fprintf(stderr, "error: %s: option '-%c' "
1139 "unrecognized\n", argv[0], c);
1152 rc = llapi_quotactl(mnt, &qctl);
1155 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1156 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1163 static int lfs_quotaoff(int argc, char **argv)
1167 struct if_quotactl qctl;
1168 char *obd_type = qctl.obd_type;
1169 char *obd_uuid = qctl.obd_uuid.uuid;
1172 memset(&qctl, 0, sizeof(qctl));
1173 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1176 while ((c = getopt(argc, argv, "ug")) != -1) {
1179 qctl.qc_type |= 0x01;
1182 qctl.qc_type |= 0x02;
1185 fprintf(stderr, "error: %s: option '-%c' "
1186 "unrecognized\n", argv[0], c);
1199 rc = llapi_quotactl(mnt, &qctl);
1202 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1203 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1210 static int name2id(unsigned int *id, char *name, int type)
1212 if (type == USRQUOTA) {
1213 struct passwd *entry;
1215 if (!(entry = getpwnam(name))) {
1221 *id = entry->pw_uid;
1223 struct group *entry;
1225 if (!(entry = getgrnam(name))) {
1231 *id = entry->gr_gid;
1237 static int id2name(char **name, unsigned int id, int type)
1239 if (type == USRQUOTA) {
1240 struct passwd *entry;
1242 if (!(entry = getpwuid(id))) {
1248 *name = entry->pw_name;
1250 struct group *entry;
1252 if (!(entry = getgrgid(id))) {
1258 *name = entry->gr_name;
1264 #define ARG2INT(nr, str, msg) \
1267 nr = strtol(str, &endp, 0); \
1269 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1274 int lfs_setquota(int argc, char **argv)
1278 struct if_quotactl qctl;
1279 char *obd_type = qctl.obd_type;
1280 char *obd_uuid = qctl.obd_uuid.uuid;
1283 memset(&qctl, 0, sizeof(qctl));
1284 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1287 while ((c = getopt(argc, argv, "ugt")) != -1) {
1290 qctl.qc_type |= 0x01;
1293 qctl.qc_type |= 0x02;
1296 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1299 fprintf(stderr, "error: %s: option '-%c' "
1300 "unrecognized\n", argv[0], c);
1308 if (qctl.qc_type == UGQUOTA) {
1309 fprintf(stderr, "error: user and group quotas can't be set "
1314 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1315 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1317 if (optind + 6 != argc)
1320 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1322 fprintf(stderr, "error: find id for name %s failed: %s\n",
1323 argv[optind - 1], strerror(errno));
1327 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1328 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1329 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1330 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1332 dqb->dqb_valid = QIF_LIMITS;
1334 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1336 if (optind + 3 != argc)
1339 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1340 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1345 rc = llapi_quotactl(mnt, &qctl);
1348 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1349 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1356 static inline char *type2name(int check_type)
1358 if (check_type == USRQUOTA)
1360 else if (check_type == GRPQUOTA)
1367 static void grace2str(time_t seconds,char *buf)
1369 uint minutes, hours, days;
1371 minutes = (seconds + 30) / 60;
1372 hours = minutes / 60;
1377 snprintf(buf, 40, "%ddays", days);
1379 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1383 static void diff2str(time_t seconds, char *buf, time_t now)
1389 if (seconds <= now) {
1390 strcpy(buf, "none");
1393 grace2str(seconds - now, buf);
1396 static void print_quota_title(char *name, struct if_quotactl *qctl)
1398 printf("Disk quotas for %s %s (%cid %u):\n",
1399 type2name(qctl->qc_type), name,
1400 *type2name(qctl->qc_type), qctl->qc_id);
1401 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1403 "blocks", "quota", "limit", "grace",
1404 "files", "quota", "limit", "grace");
1407 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1413 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1414 int bover = 0, iover = 0;
1415 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1417 if (dqb->dqb_bhardlimit &&
1418 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1420 } else if (dqb->dqb_bsoftlimit &&
1421 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1422 if (dqb->dqb_btime > now) {
1429 if (dqb->dqb_ihardlimit &&
1430 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1432 } else if (dqb->dqb_isoftlimit &&
1433 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1434 if (dqb->dqb_btime > now) {
1441 #if 0 /* XXX: always print quotas even when no usages */
1442 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1448 if (strlen(mnt) > 15)
1449 printf("%s\n%15s", mnt, "");
1451 printf("%15s", mnt);
1454 diff2str(dqb->dqb_btime, timebuf, now);
1456 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1457 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1458 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1459 printf(" %7s%c %6s %7s %7s",
1460 numbuf[0], bover ? '*' : ' ', numbuf[1],
1461 numbuf[2], bover > 1 ? timebuf : "");
1464 diff2str(dqb->dqb_itime, timebuf, now);
1466 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1467 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1468 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1470 printf(" %7s%c %6s %7s %7s",
1471 numbuf[0], iover ? '*' : ' ', numbuf[1],
1472 numbuf[2], iover > 1 ? timebuf : "");
1475 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1476 qctl->qc_cmd == Q_GETOINFO) {
1480 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1481 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1482 printf("Block grace time: %s; Inode grace time: %s\n",
1483 bgtimebuf, igtimebuf);
1487 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1491 /* XXX: this is a flag to mark that only mds quota is wanted */
1492 qctl->qc_dqblk.dqb_valid = 1;
1493 rc = llapi_quotactl(mnt, qctl);
1495 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1498 qctl->qc_dqblk.dqb_valid = 0;
1500 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1503 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1506 struct obd_uuid uuids[1024], *uuidp;
1507 int obdcount = 1024;
1512 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1516 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1518 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1522 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1523 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1525 /* XXX clear this flag to get quota from osts */
1526 qctl->qc_dqblk.dqb_valid = 0;
1527 rc = llapi_quotactl(mnt, qctl);
1529 fprintf(stderr, "%s quotactl failed: %s\n",
1530 uuidp->uuid, strerror(errno));
1534 print_quota(uuidp->uuid, qctl, 1);
1542 static int lfs_quota(int argc, char **argv)
1545 char *name = NULL, *mnt;
1546 struct if_quotactl qctl;
1547 char *obd_type = qctl.obd_type;
1548 char *obd_uuid = qctl.obd_uuid.uuid;
1551 memset(&qctl, 0, sizeof(qctl));
1552 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1555 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1558 qctl.qc_type |= 0x01;
1561 qctl.qc_type |= 0x02;
1564 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1567 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1570 fprintf(stderr, "error: %s: option '-%c' "
1571 "unrecognized\n", argv[0], c);
1579 if (qctl.qc_type == UGQUOTA) {
1580 fprintf(stderr, "error: user or group can't be specified"
1585 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1586 if (optind + 2 != argc)
1589 name = argv[optind++];
1590 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1592 fprintf(stderr, "error: find id for name %s failed: %s\n",
1593 name, strerror(errno));
1596 print_quota_title(name, &qctl);
1597 } else if (optind + 1 != argc) {
1603 rc = llapi_quotactl(mnt, &qctl);
1606 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1607 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1612 rc = id2name(&name, getuid(), qctl.qc_type);
1619 print_quota(mnt, &qctl, 0);
1621 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1622 print_mds_quota(mnt, &qctl);
1623 print_lov_quota(mnt, &qctl);
1628 #endif /* HAVE_QUOTA_SUPPORT */
1630 static int flushctx_ioctl(char *mp)
1634 fd = open(mp, O_RDONLY);
1636 fprintf(stderr, "flushctx: error open %s: %s\n",
1637 mp, strerror(errno));
1641 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1643 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1644 mp, strerror(errno));
1650 static int lfs_flushctx(int argc, char **argv)
1652 int kdestroy = 0, c;
1654 char procline[PATH_MAX], *line;
1658 while ((c = getopt(argc, argv, "k")) != -1) {
1664 fprintf(stderr, "error: %s: option '-%c' "
1665 "unrecognized\n", argv[0], c);
1671 system("kdestroy > /dev/null");
1673 if (optind >= argc) {
1674 /* flush for all mounted lustre fs. */
1675 proc = fopen("/proc/mounts", "r");
1677 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1682 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1687 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1688 fprintf(stderr, "%s: unexpected format in "
1694 if (strcmp(fs, "lustre") != 0)
1696 /* we use '@' to determine it's a client. are there
1697 * any other better way?
1699 if (strchr(dev, '@') == NULL)
1702 if (flushctx_ioctl(mp))
1706 /* flush fs as specified */
1707 while (optind < argc) {
1708 if (flushctx_ioctl(argv[optind++]))
1717 * We assume one and only one filename is supplied as the
1720 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1722 char *dname, *rpath = NULL;
1723 char path[PATH_MAX], cwd[PATH_MAX];
1731 /* FIXME the premise is there is no sub-mounted filesystems under this
1732 * mounted lustre tree. */
1733 strncpy(fname, argv[argc - 1], PATH_MAX);
1735 /* get path prefix */
1736 dname = dirname(fname);
1738 /* try to resolve the pathname into relative to the root of the mounted
1739 * lustre filesystem.
1741 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1742 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1746 if (chdir(dname) == -1) {
1747 fprintf(stderr, "chdir to %s failed: %s\n",
1748 dname, strerror(errno));
1752 if (getcwd(path, sizeof(path)) == NULL) {
1753 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1757 if (chdir(cwd) == -1) {
1758 fprintf(stderr, "chdir back to %s: %s\n",
1759 cwd, strerror(errno));
1763 strncat(path, "/", PATH_MAX);
1764 strncpy(fname, argv[argc - 1], PATH_MAX);
1765 strncat(path, basename(fname), PATH_MAX);
1767 fp = setmntent(MOUNTED, "r");
1769 fprintf(stderr, "setmntent %s failed: %s\n",
1770 MOUNTED, strerror(errno));
1775 mnt = getmntent(fp);
1779 if (!llapi_is_lustre_mnttype(mnt))
1782 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1783 rpath = path + strlen(mnt->mnt_dir);
1789 /* remove char '/' from rpath to be a relative path */
1790 while (rpath && *rpath == '/') rpath++;
1794 "%s: file %s doesn't belong to a lustre file system!\n",
1795 argv[0], argv[argc - 1]);
1799 for (i = 0; i < argc - 1; i++) {
1800 strncat(cmd, argv[i], PATH_MAX);
1801 strncat(cmd, " ", PATH_MAX);
1803 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1804 strncpy(fname, argv[argc - 1], sizeof(fname));
1809 static int lfs_getfacl(int argc, char **argv)
1811 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1813 if (acl_cmd_parse(argc, argv, fname, cmd))
1816 return llapi_getfacl(fname, cmd);
1819 static int lfs_setfacl(int argc, char **argv)
1821 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1823 if (acl_cmd_parse(argc, argv, fname, cmd))
1826 return llapi_setfacl(fname, cmd);
1829 int main(int argc, char **argv)
1835 ptl_initialize(argc, argv);
1836 if (obd_initialize(argc, argv) < 0)
1838 if (dbg_initialize(argc, argv) < 0)
1841 Parser_init("lfs > ", cmdlist);
1844 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1846 rc = Parser_commands();
1849 obd_finalize(argc, argv);