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 start> <stripe count>\n"
84 " setstripe -d <dirname> (to delete default striping)\n"
85 "\tstripe size: Number of bytes on each OST (0 filesystem default)\n"
86 "\tstripe start: OST index of first stripe (-1 filesystem default)\n"
87 "\tstripe count: Number of OSTs to stripe over (0 default, -1 all)"},
88 {"getstripe", lfs_getstripe, 0,
89 "To list the striping info for a given filename or files in a\n"
90 "directory or recursively for all files in a directory tree.\n"
91 "usage: getstripe [--obd|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
92 " [--recursive | -r] <dir|file> ..."},
94 "To find files that match given parameters recursively in a directory tree.\n"
95 "usage: find [--atime|-A N] [--mtime|-M N] [--ctime|-C N] [--maxdepth|-D N]\n"
96 " [--print0|-P] [--print|-p] [--obd|-O <uuid>] <dir/file> ...\n"
97 "If one of the options below is provided, find works the same as 'getstripe':\n"
98 "To list the striping info for a given filename or files in a directory or\n"
100 "OBSOLETE usage: find [--quiet | -q] [--verbose | -v]\n"
101 " [--recursive | -r] <dir|file> ..."},
102 {"check", lfs_check, 0,
103 "Display the status of MDS or OSTs (as specified in the command)\n"
104 "or all the servers (MDS and OSTs).\n"
105 "usage: check <osts|mds|servers>"},
106 {"catinfo", lfs_catinfo, 0,
107 "Show information of specified type logs.\n"
108 "usage: catinfo {keyword} [node name]\n"
109 "\tkeywords are one of followings: config, deletions.\n"
110 "\tnode name must be provided when use keyword config."},
111 {"join", lfs_join, 0,
112 "join two lustre files into one - join A, B, will be like cat B >> A & del B\n"
113 "usage: join <filename_A> <filename_B>\n"},
114 {"osts", lfs_osts, 0, "osts"},
116 "report filesystem disk space usage or inodes usage"
118 "Usage: df [-i] [-h] [path]"},
119 #ifdef HAVE_QUOTA_SUPPORT
120 {"quotachown",lfs_quotachown, 0,
121 "Change files' owner or group on the specified filesystem.\n"
122 "usage: quotachown [-i] <filesystem>\n"
123 "\t-i: ignore error if file is not exist\n"},
124 {"quotacheck", lfs_quotacheck, 0,
125 "Scan the specified filesystem for disk usage, and create,\n"
126 "or update quota files.\n"
127 "usage: quotacheck [ -ug ] <filesystem>"},
128 {"quotaon", lfs_quotaon, 0, "Turn filesystem quotas on.\n"
129 "usage: quotaon [ -ugf ] <filesystem>"},
130 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem quotas off.\n"
131 "usage: quotaoff [ -ug ] <filesystem>"},
132 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
133 "usage: setquota [ -u | -g ] <name> <block-softlimit> <block-hardlimit> <inode-softlimit> <inode-hardlimit> <filesystem>\n"
134 " setquota -t [ -u | -g ] <block-grace> <inode-grace> <filesystem>"},
135 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
136 "usage: quota [ -o obd_uuid ] [ -u | -g ] [name] <filesystem>"},
138 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
139 "usage: flushctx [-k] [mountpoint...]"},
140 {"getfacl", lfs_getfacl, 0,
141 "Get file access control list in remote client.\n"
142 "usage: getfacl [-dRLPvh] file"},
143 {"setfacl", lfs_setfacl, 0,
144 "Set file access control list in remote client.\n"
145 "usage: setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file"},
146 {"help", Parser_help, 0, "help"},
147 {"exit", Parser_quit, 0, "quit"},
148 {"quit", Parser_quit, 0, "quit"},
153 static int lfs_setstripe(int argc, char **argv)
158 int st_offset, st_count;
161 if (argc != 5 && argc != 3)
166 if (strcmp(argv[1], "-d") != 0)
176 /* get the stripe size */
177 st_size = strtoul(argv[2], &end, 0);
179 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
184 /* get the stripe offset */
185 st_offset = strtoul(argv[3], &end, 0);
187 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
191 /* get the stripe count */
192 st_count = strtoul(argv[4], &end, 0);
194 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
200 result = llapi_file_create(fname, st_size, st_offset, st_count, 0);
202 fprintf(stderr, "error: %s: create stripe file failed\n",
208 static int set_time(time_t *time, time_t *set, char *str)
215 else if (str[0] == '-')
221 t = strtol(str, NULL, 0);
222 if (*time < t * 24 * 60 * 60) {
225 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
229 *set = *time - t * 24 * 60 * 60;
233 static int lfs_find(int argc, char **argv)
240 time_t atime, ctime, mtime;
241 int asign, msign, csign;
242 int quiet, verbose, recursive;
243 struct find_param param;
244 struct obd_uuid *obduuid = NULL;
245 struct option long_opts[] = {
246 /* New find options. */
247 {"atime", required_argument, 0, 'A'},
248 {"ctime", required_argument, 0, 'C'},
249 {"mtime", required_argument, 0, 'M'},
250 {"maxdepth", required_argument, 0, 'D'},
251 /* --obd is considered as a new option. */
252 {"obd", required_argument, 0, 'O'},
253 {"print", no_argument, 0, 'P'},
254 {"print0", no_argument, 0, 'p'},
255 /* Old find options. */
256 {"quiet", no_argument, 0, 'q'},
257 {"recursive", no_argument, 0, 'r'},
258 {"verbose", no_argument, 0, 'v'},
265 atime = ctime = mtime = 0;
266 asign = csign = msign = 0;
267 quiet = verbose = recursive = 0;
269 while ((c = getopt_long_only(argc, argv, "A:C:D:M:PpOqrv",
270 long_opts, NULL)) >= 0)
277 ret = set_time(&t, &atime, optarg);
284 ret = set_time(&t, &ctime, optarg);
291 depth = strtol(optarg, 0, 0);
294 ret = set_time(&t, &mtime, optarg);
303 "error: %s: only one obduuid allowed",
307 obduuid = (struct obd_uuid *)optarg;
331 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
332 argv[0], argv[optind - 1]);
340 memset(¶m, 0, sizeof(param));
341 param.obduuid = obduuid;
343 param.maxdepth = depth;
350 param.zeroend = zeroend;
353 param.recursive = recursive;
354 param.verbose = verbose;
356 param.maxdepth = recursive ? -1 : 1;
361 ret = llapi_find(argv[optind], ¶m);
363 ret = llapi_getstripe(argv[optind], ¶m);
364 } while (++optind < argc && !ret);
367 fprintf(stderr, "error: %s failed for %s.\n",
368 argv[0], argv[optind - 1]);
372 static int lfs_getstripe(int argc, char **argv)
374 struct option long_opts[] = {
376 {"quiet", 0, 0, 'q'},
377 {"recursive", 0, 0, 'r'},
378 {"verbose", 0, 0, 'v'},
381 char short_opts[] = "hO:qrv";
382 int quiet, verbose, recursive, c, rc;
383 struct obd_uuid *obduuid = NULL;
384 struct find_param param;
387 quiet = verbose = recursive = 0;
388 while ((c = getopt_long(argc, argv, short_opts,
389 long_opts, NULL)) != -1) {
394 "error: %s: only one obduuid allowed",
398 obduuid = (struct obd_uuid *)optarg;
414 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
415 argv[0], argv[optind - 1]);
423 memset(¶m, 0, sizeof(param));
424 param.recursive = recursive;
425 param.verbose = verbose;
427 param.obduuid = obduuid;
428 param.maxdepth = recursive ? -1 : 1;
431 rc = llapi_getstripe(argv[optind], ¶m);
432 } while (++optind < argc && !rc);
435 fprintf(stderr, "error: %s failed for %s.\n",
436 argv[0], argv[optind - 1]);
440 static int lfs_osts(int argc, char **argv)
443 struct mntent *mnt = NULL;
444 struct obd_uuid *obduuid = NULL;
445 struct find_param param;
451 fp = setmntent(MOUNTED, "r");
454 fprintf(stderr, "%s: setmntent(%s): %s:", argv[0], MOUNTED,
458 memset(¶m, 0, sizeof(param));
459 param.obduuid = obduuid;
460 while (feof(fp) == 0 && ferror(fp) ==0) {
461 if (llapi_is_lustre_mnttype(mnt)) {
462 rc = llapi_getstripe(mnt->mnt_dir, ¶m);
465 "error: %s: failed on %s\n",
466 argv[0], mnt->mnt_dir);
476 #define COOK(value) \
479 while (value > 1024) { \
493 static int path2mnt(char *path, FILE *fp, char *mntdir, int dir_len)
495 char rpath[PATH_MAX] = {'\0'};
497 int rc, len, out_len = 0;
499 if (!realpath(path, rpath)) {
501 fprintf(stderr, "error: lfs df: invalid path '%s': %s\n",
502 path, strerror(-rc));
508 while (feof(fp) == 0 && ferror(fp) == 0) {
509 if (llapi_is_lustre_mnttype(mnt)) {
510 len = strlen(mnt->mnt_dir);
512 !strncmp(rpath, mnt->mnt_dir, len)) {
514 memset(mntdir, 0, dir_len);
515 strncpy(mntdir, mnt->mnt_dir, dir_len);
524 fprintf(stderr, "error: lfs df: %s isn't mounted on lustre\n", path);
528 static int showdf(char *mntdir, struct obd_statfs *stat,
529 char *uuid, int ishow, int cooked,
530 char *type, int index, int rc)
532 long long avail, used, total;
534 char *suffix = "KMGTPEZY";
535 char tbuf[10], ubuf[10], abuf[10], rbuf[10];
543 avail = stat->os_ffree;
544 used = stat->os_files - stat->os_ffree;
545 total = stat->os_files;
547 int shift = cooked ? 0 : 10;
549 avail = (stat->os_bavail * stat->os_bsize) >> shift;
550 used = stat->os_blocks - stat->os_bavail;
551 used = (used * stat->os_bsize) >> shift;
552 total = (stat->os_blocks * stat->os_bsize) >> shift;
556 ratio = (double)used / (double)total;
562 cook_val = (double)total;
565 sprintf(tbuf, HDF"%c", cook_val, suffix[i - 1]);
567 sprintf(tbuf, CDF, total);
569 cook_val = (double)used;
572 sprintf(ubuf, HDF"%c", cook_val, suffix[i - 1]);
574 sprintf(ubuf, CDF, used);
576 cook_val = (double)avail;
579 sprintf(abuf, HDF"%c", cook_val, suffix[i - 1]);
581 sprintf(abuf, CDF, avail);
583 sprintf(tbuf, CDF, total);
584 sprintf(ubuf, CDF, used);
585 sprintf(abuf, CDF, avail);
588 sprintf(rbuf, RDF, (int)(ratio * 100));
589 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
590 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
592 printf("[%s:%d]\n", type, index);
598 printf(UUF": inactive device\n", uuid);
601 printf(UUF": %s\n", uuid, strerror(-rc));
608 static int mntdf(char *mntdir, int ishow, int cooked)
610 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
611 struct obd_uuid uuid_buf;
616 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
617 "UUID", "Inodes", "IUsed", "IFree",
618 "IUse%", "Mounted on");
620 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
621 "UUID", cooked ? "bytes" : "1K-blocks",
622 "Used", "Available", "Use%", "Mounted on");
624 for (index = 0; ; index++) {
625 memset(&stat_buf, 0, sizeof(struct obd_statfs));
626 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
627 rc = llapi_obd_statfs(mntdir, LL_STATFS_MDC, index,
628 &stat_buf, &uuid_buf);
632 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
633 rc == -ENODATA || rc == 0) {
634 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
638 "error: llapi_obd_statfs(%s): %s (%d)\n",
639 uuid_buf.uuid, strerror(-rc), rc);
643 sum.os_ffree += stat_buf.os_ffree;
644 sum.os_files += stat_buf.os_files;
648 for (index = 0; ; index++) {
649 memset(&stat_buf, 0, sizeof(struct obd_statfs));
650 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
651 rc = llapi_obd_statfs(mntdir, LL_STATFS_LOV, index,
652 &stat_buf, &uuid_buf);
656 if (rc == -ENOTCONN || rc == -ETIMEDOUT || rc == -EIO ||
657 rc == -ENODATA || rc == 0) {
658 showdf(mntdir, &stat_buf, uuid_buf.uuid, ishow, cooked,
662 "error: llapi_obd_statfs failed: %s (%d)\n",
667 sum.os_blocks += stat_buf.os_blocks * stat_buf.os_bsize;
668 sum.os_bfree += stat_buf.os_bfree * stat_buf.os_bsize;
669 sum.os_bavail += stat_buf.os_bavail * stat_buf.os_bsize;
674 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0,0);
679 static int lfs_df(int argc, char **argv)
683 struct mntent *mnt = NULL;
684 char mntdir[PATH_MAX] = {'\0'};
685 int ishow = 0, cooked = 0;
689 while ((c = getopt(argc, argv, "ih")) != -1) {
704 fp = setmntent(MOUNTED, "r");
707 fprintf(stderr, "error: %s: open %s failed( %s )\n",
708 argv[0], MOUNTED, strerror(errno));
712 rc = path2mnt(path, fp, mntdir, sizeof(mntdir));
718 rc = mntdf(mntdir, ishow, cooked);
723 while (feof(fp) == 0 && ferror(fp) == 0) {
724 if (llapi_is_lustre_mnttype(mnt)) {
725 rc = mntdf(mnt->mnt_dir, ishow, cooked);
738 static int lfs_check(int argc, char **argv)
742 struct mntent *mnt = NULL;
751 obd_types[0] = obd_type1;
752 obd_types[1] = obd_type2;
754 if (strcmp(argv[1], "osts") == 0) {
755 strcpy(obd_types[0], "osc");
756 } else if (strcmp(argv[1], "mds") == 0) {
757 strcpy(obd_types[0], "mdc");
758 } else if (strcmp(argv[1], "servers") == 0) {
760 strcpy(obd_types[0], "osc");
761 strcpy(obd_types[1], "mdc");
763 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
768 fp = setmntent(MOUNTED, "r");
770 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
774 while (feof(fp) == 0 && ferror(fp) ==0) {
775 if (llapi_is_lustre_mnttype(mnt))
783 fprintf(stderr, "No suitable Lustre mount found\n");
787 rc = llapi_target_check(num_types, obd_types, mnt->mnt_dir);
790 fprintf(stderr, "error: %s: %s status failed\n",
797 static int lfs_catinfo(int argc, char **argv)
800 struct mntent *mnt = NULL;
803 if (argc < 2 || (!strcmp(argv[1],"config") && argc < 3))
806 if (strcmp(argv[1], "config") && strcmp(argv[1], "deletions"))
809 fp = setmntent(MOUNTED, "r");
811 fprintf(stderr, "setmntent(%s): %s:", MOUNTED,
815 while (feof(fp) == 0 && ferror(fp) == 0) {
816 if (llapi_is_lustre_mnttype(mnt))
825 rc = llapi_catinfo(mnt->mnt_dir, argv[1], argv[2]);
827 rc = llapi_catinfo(mnt->mnt_dir, argv[1], NULL);
829 fprintf(stderr, "no lustre_lite mounted.\n");
836 int lfs_join(int argc, char **argv)
838 char *name_head, *name_tail;
845 fd = open(name_head, O_WRONLY);
847 fprintf(stderr, "Can not open name_head %s rc=%d\n",
851 size = lseek(fd, 0, SEEK_END);
852 if (size % JOIN_FILE_ALIGN) {
853 fprintf(stderr,"head file %s size %llu must be mutiple of %d\n",
854 name_head, (long long)size, JOIN_FILE_ALIGN);
859 rc = ioctl(fd, LL_IOC_JOIN, name_tail);
863 fprintf(stderr, "Lustre joining files: %s, %s, failed\n",
869 #ifdef HAVE_QUOTA_SUPPORT
870 static int lfs_quotachown(int argc, char **argv)
876 while ((c = getopt(argc, argv, "i")) != -1) {
882 fprintf(stderr, "error: %s: option '-%c' "
883 "unrecognized\n", argv[0], c);
889 rc = llapi_quotachown(argv[optind], flag);
891 fprintf(stderr,"error: change file owner/group failed.\n");
896 static int lfs_quotacheck(int argc, char **argv)
898 int c, check_type = 0;
900 struct if_quotacheck qchk;
901 struct if_quotactl qctl;
902 char *obd_type = qchk.obd_type;
903 char *obd_uuid = qchk.obd_uuid.uuid;
906 memset(&qchk, 0, sizeof(qchk));
909 while ((c = getopt(argc, argv, "ug")) != -1) {
918 fprintf(stderr, "error: %s: option '-%c' "
919 "unrecognized\n", argv[0], c);
926 else /* do quotacheck for both user & group quota by default */
934 memset(&qctl, 0, sizeof(qctl));
935 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
936 qctl.qc_id = QFMT_LDISKFS;
937 qctl.qc_type = check_type;
938 rc = llapi_quotactl(mnt, &qctl);
940 fprintf(stderr, "quota off failed: %s\n", strerror(errno));
944 rc = llapi_quotacheck(mnt, check_type);
946 fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
950 rc = llapi_poll_quotacheck(mnt, &qchk);
953 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
954 fprintf(stderr, "quota check failed: %s\n", strerror(errno));
958 memset(&qctl, 0, sizeof(qctl));
959 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
960 qctl.qc_id = QFMT_LDISKFS;
961 qctl.qc_type = check_type;
962 rc = llapi_quotactl(mnt, &qctl);
965 fprintf(stderr, "%s %s ",
966 qctl.obd_type, qctl.obd_uuid.uuid);
967 fprintf(stderr, "%s turn on quota failed: %s\n",
968 argv[0], strerror(errno));
975 static int lfs_quotaon(int argc, char **argv)
979 struct if_quotactl qctl;
980 char *obd_type = qctl.obd_type;
981 char *obd_uuid = qctl.obd_uuid.uuid;
984 memset(&qctl, 0, sizeof(qctl));
985 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
986 qctl.qc_id = QFMT_LDISKFS;
989 while ((c = getopt(argc, argv, "ugf")) != -1) {
992 qctl.qc_type |= 0x01;
995 qctl.qc_type |= 0x02;
998 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1001 fprintf(stderr, "error: %s: option '-%c' "
1002 "unrecognized\n", argv[0], c);
1015 rc = llapi_quotactl(mnt, &qctl);
1018 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1019 fprintf(stderr, "%s failed: %s\n", argv[0], strerror(errno));
1026 static int lfs_quotaoff(int argc, char **argv)
1030 struct if_quotactl qctl;
1031 char *obd_type = qctl.obd_type;
1032 char *obd_uuid = qctl.obd_uuid.uuid;
1035 memset(&qctl, 0, sizeof(qctl));
1036 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
1039 while ((c = getopt(argc, argv, "ug")) != -1) {
1042 qctl.qc_type |= 0x01;
1045 qctl.qc_type |= 0x02;
1048 fprintf(stderr, "error: %s: option '-%c' "
1049 "unrecognized\n", argv[0], c);
1062 rc = llapi_quotactl(mnt, &qctl);
1065 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1066 fprintf(stderr, "quotaoff failed: %s\n", strerror(errno));
1073 static int name2id(unsigned int *id, char *name, int type)
1075 if (type == USRQUOTA) {
1076 struct passwd *entry;
1078 if (!(entry = getpwnam(name))) {
1084 *id = entry->pw_uid;
1086 struct group *entry;
1088 if (!(entry = getgrnam(name))) {
1094 *id = entry->gr_gid;
1100 static int id2name(char **name, unsigned int id, int type)
1102 if (type == USRQUOTA) {
1103 struct passwd *entry;
1105 if (!(entry = getpwuid(id))) {
1111 *name = entry->pw_name;
1113 struct group *entry;
1115 if (!(entry = getgrgid(id))) {
1121 *name = entry->gr_name;
1127 #define ARG2INT(nr, str, msg) \
1130 nr = strtol(str, &endp, 0); \
1132 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
1137 int lfs_setquota(int argc, char **argv)
1141 struct if_quotactl qctl;
1142 char *obd_type = qctl.obd_type;
1143 char *obd_uuid = qctl.obd_uuid.uuid;
1146 memset(&qctl, 0, sizeof(qctl));
1147 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
1150 while ((c = getopt(argc, argv, "ugt")) != -1) {
1153 qctl.qc_type |= 0x01;
1156 qctl.qc_type |= 0x02;
1159 qctl.qc_cmd = LUSTRE_Q_SETINFO;
1162 fprintf(stderr, "error: %s: option '-%c' "
1163 "unrecognized\n", argv[0], c);
1171 if (qctl.qc_type == UGQUOTA) {
1172 fprintf(stderr, "error: user and group quotas can't be set "
1177 if (qctl.qc_cmd == LUSTRE_Q_SETQUOTA) {
1178 struct obd_dqblk *dqb = &qctl.qc_dqblk;
1180 if (optind + 6 != argc)
1183 rc = name2id(&qctl.qc_id, argv[optind++], qctl.qc_type);
1185 fprintf(stderr, "error: find id for name %s failed: %s\n",
1186 argv[optind - 1], strerror(errno));
1190 ARG2INT(dqb->dqb_bsoftlimit, argv[optind++], "block-softlimit");
1191 ARG2INT(dqb->dqb_bhardlimit, argv[optind++], "block-hardlimit");
1192 ARG2INT(dqb->dqb_isoftlimit, argv[optind++], "inode-softlimit");
1193 ARG2INT(dqb->dqb_ihardlimit, argv[optind++], "inode-hardlimit");
1195 dqb->dqb_valid = QIF_LIMITS;
1197 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
1199 if (optind + 3 != argc)
1202 ARG2INT(dqi->dqi_bgrace, argv[optind++], "block-grace");
1203 ARG2INT(dqi->dqi_igrace, argv[optind++], "inode-grace");
1208 rc = llapi_quotactl(mnt, &qctl);
1211 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1212 fprintf(stderr, "setquota failed: %s\n", strerror(errno));
1219 static inline char *type2name(int check_type)
1221 if (check_type == USRQUOTA)
1223 else if (check_type == GRPQUOTA)
1230 static void grace2str(time_t seconds,char *buf)
1232 uint minutes, hours, days;
1234 minutes = (seconds + 30) / 60;
1235 hours = minutes / 60;
1240 snprintf(buf, 40, "%ddays", days);
1242 snprintf(buf, 40, "%02d:%02d", hours + days * 24, minutes);
1246 static void diff2str(time_t seconds, char *buf, time_t now)
1252 if (seconds <= now) {
1253 strcpy(buf, "none");
1256 grace2str(seconds - now, buf);
1259 static void print_quota_title(char *name, struct if_quotactl *qctl)
1261 printf("Disk quotas for %s %s (%cid %u):\n",
1262 type2name(qctl->qc_type), name,
1263 *type2name(qctl->qc_type), qctl->qc_id);
1264 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
1266 "blocks", "quota", "limit", "grace",
1267 "files", "quota", "limit", "grace");
1270 static void print_quota(char *mnt, struct if_quotactl *qctl, int ost_only)
1276 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
1277 int bover = 0, iover = 0;
1278 struct obd_dqblk *dqb = &qctl->qc_dqblk;
1280 if (dqb->dqb_bhardlimit &&
1281 toqb(dqb->dqb_curspace) > dqb->dqb_bhardlimit) {
1283 } else if (dqb->dqb_bsoftlimit &&
1284 toqb(dqb->dqb_curspace) > dqb->dqb_bsoftlimit) {
1285 if (dqb->dqb_btime > now) {
1292 if (dqb->dqb_ihardlimit &&
1293 dqb->dqb_curinodes > dqb->dqb_ihardlimit) {
1295 } else if (dqb->dqb_isoftlimit &&
1296 dqb->dqb_curinodes > dqb->dqb_isoftlimit) {
1297 if (dqb->dqb_btime > now) {
1304 #if 0 /* XXX: always print quotas even when no usages */
1305 if (dqb->dqb_curspace || dqb->dqb_curinodes)
1311 if (strlen(mnt) > 15)
1312 printf("%s\n%15s", mnt, "");
1314 printf("%15s", mnt);
1317 diff2str(dqb->dqb_btime, timebuf, now);
1319 sprintf(numbuf[0], LPU64, toqb(dqb->dqb_curspace));
1320 sprintf(numbuf[1], LPU64, dqb->dqb_bsoftlimit);
1321 sprintf(numbuf[2], LPU64, dqb->dqb_bhardlimit);
1322 printf(" %7s%c %6s %7s %7s",
1323 numbuf[0], bover ? '*' : ' ', numbuf[1],
1324 numbuf[2], bover > 1 ? timebuf : "");
1327 diff2str(dqb->dqb_itime, timebuf, now);
1329 sprintf(numbuf[0], LPU64, dqb->dqb_curinodes);
1330 sprintf(numbuf[1], LPU64, dqb->dqb_isoftlimit);
1331 sprintf(numbuf[2], LPU64, dqb->dqb_ihardlimit);
1333 printf(" %7s%c %6s %7s %7s",
1334 numbuf[0], iover ? '*' : ' ', numbuf[1],
1335 numbuf[2], iover > 1 ? timebuf : "");
1338 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
1339 qctl->qc_cmd == Q_GETOINFO) {
1343 grace2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf);
1344 grace2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf);
1345 printf("Block grace time: %s; Inode grace time: %s\n",
1346 bgtimebuf, igtimebuf);
1350 static void print_mds_quota(char *mnt, struct if_quotactl *qctl)
1354 /* XXX: this is a flag to mark that only mds quota is wanted */
1355 qctl->qc_dqblk.dqb_valid = 1;
1356 rc = llapi_quotactl(mnt, qctl);
1358 fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
1361 qctl->qc_dqblk.dqb_valid = 0;
1363 print_quota(qctl->obd_uuid.uuid, qctl, 0);
1366 static void print_lov_quota(char *mnt, struct if_quotactl *qctl)
1369 struct obd_uuid uuids[1024], *uuidp;
1370 int obdcount = 1024;
1375 fprintf(stderr, "open %s failed: %s\n", mnt, strerror(errno));
1379 rc = llapi_lov_get_uuids(dirfd(dir), uuids, &obdcount);
1381 fprintf(stderr, "get ost uuid failed: %s\n", strerror(errno));
1385 for (i = 0, uuidp = uuids; i < obdcount; i++, uuidp++) {
1386 memcpy(&qctl->obd_uuid, uuidp, sizeof(*uuidp));
1388 /* XXX clear this flag to get quota from osts */
1389 qctl->qc_dqblk.dqb_valid = 0;
1390 rc = llapi_quotactl(mnt, qctl);
1392 fprintf(stderr, "%s quotactl failed: %s\n",
1393 uuidp->uuid, strerror(errno));
1397 print_quota(uuidp->uuid, qctl, 1);
1405 static int lfs_quota(int argc, char **argv)
1408 char *name = NULL, *mnt;
1409 struct if_quotactl qctl;
1410 char *obd_type = qctl.obd_type;
1411 char *obd_uuid = qctl.obd_uuid.uuid;
1414 memset(&qctl, 0, sizeof(qctl));
1415 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
1418 while ((c = getopt(argc, argv, "ugto:")) != -1) {
1421 qctl.qc_type |= 0x01;
1424 qctl.qc_type |= 0x02;
1427 qctl.qc_cmd = LUSTRE_Q_GETINFO;
1430 strncpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
1433 fprintf(stderr, "error: %s: option '-%c' "
1434 "unrecognized\n", argv[0], c);
1442 if (qctl.qc_type == UGQUOTA) {
1443 fprintf(stderr, "error: user or group can't be specified"
1448 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
1449 if (optind + 2 != argc)
1452 name = argv[optind++];
1453 rc = name2id(&qctl.qc_id, name, qctl.qc_type);
1455 fprintf(stderr, "error: find id for name %s failed: %s\n",
1456 name, strerror(errno));
1459 print_quota_title(name, &qctl);
1460 } else if (optind + 1 != argc) {
1466 rc = llapi_quotactl(mnt, &qctl);
1469 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
1470 fprintf(stderr, "quota failed: %s\n", strerror(errno));
1475 rc = id2name(&name, getuid(), qctl.qc_type);
1482 print_quota(mnt, &qctl, 0);
1484 if (!*obd_uuid && qctl.qc_cmd != LUSTRE_Q_GETINFO) {
1485 print_mds_quota(mnt, &qctl);
1486 print_lov_quota(mnt, &qctl);
1491 #endif /* HAVE_QUOTA_SUPPORT */
1493 static int flushctx_ioctl(char *mp)
1497 fd = open(mp, O_RDONLY);
1499 fprintf(stderr, "flushctx: error open %s: %s\n",
1500 mp, strerror(errno));
1504 rc = ioctl(fd, LL_IOC_FLUSHCTX);
1506 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
1507 mp, strerror(errno));
1513 static int lfs_flushctx(int argc, char **argv)
1515 int kdestroy = 0, c;
1517 char procline[PATH_MAX], *line;
1521 while ((c = getopt(argc, argv, "k")) != -1) {
1527 fprintf(stderr, "error: %s: option '-%c' "
1528 "unrecognized\n", argv[0], c);
1534 system("kdestroy > /dev/null");
1536 if (optind >= argc) {
1537 /* flush for all mounted lustre fs. */
1538 proc = fopen("/proc/mounts", "r");
1540 fprintf(stderr, "error: %s: can't open /proc/mounts\n",
1545 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
1550 if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
1551 fprintf(stderr, "%s: unexpected format in "
1557 if (strcmp(fs, "lustre") != 0)
1559 /* we use '@' to determine it's a client. are there
1560 * any other better way?
1562 if (strchr(dev, '@') == NULL)
1565 if (flushctx_ioctl(mp))
1569 /* flush fs as specified */
1570 while (optind < argc) {
1571 if (flushctx_ioctl(argv[optind++]))
1580 * We assume one and only one filename is supplied as the
1583 static int acl_cmd_parse(int argc, char **argv, char *fname, char *cmd)
1585 char *dname, *rpath = NULL;
1586 char path[PATH_MAX], cwd[PATH_MAX];
1594 /* FIXME the premise is there is no sub-mounted filesystems under this
1595 * mounted lustre tree. */
1596 strncpy(fname, argv[argc - 1], PATH_MAX);
1598 /* get path prefix */
1599 dname = dirname(fname);
1601 /* try to resolve the pathname into relative to the root of the mounted
1602 * lustre filesystem.
1604 if (getcwd(cwd, sizeof(cwd)) == NULL) {
1605 fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
1609 if (chdir(dname) == -1) {
1610 fprintf(stderr, "chdir to %s failed: %s\n",
1611 dname, strerror(errno));
1615 if (getcwd(path, sizeof(path)) == NULL) {
1616 fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
1620 if (chdir(cwd) == -1) {
1621 fprintf(stderr, "chdir back to %s: %s\n",
1622 cwd, strerror(errno));
1626 strncat(path, "/", PATH_MAX);
1627 strncpy(fname, argv[argc - 1], PATH_MAX);
1628 strncat(path, basename(fname), PATH_MAX);
1630 fp = setmntent(MOUNTED, "r");
1632 fprintf(stderr, "setmntent %s failed: %s\n",
1633 MOUNTED, strerror(errno));
1638 mnt = getmntent(fp);
1642 if (!llapi_is_lustre_mnttype(mnt))
1645 if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
1646 rpath = path + strlen(mnt->mnt_dir);
1652 /* remove char '/' from rpath to be a relative path */
1653 while (rpath && *rpath == '/') rpath++;
1657 "%s: file %s doesn't belong to a lustre file system!\n",
1658 argv[0], argv[argc - 1]);
1662 for (i = 0; i < argc - 1; i++) {
1663 strncat(cmd, argv[i], PATH_MAX);
1664 strncat(cmd, " ", PATH_MAX);
1666 strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
1667 strncpy(fname, argv[argc - 1], sizeof(fname));
1672 static int lfs_getfacl(int argc, char **argv)
1674 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1676 if (acl_cmd_parse(argc, argv, fname, cmd))
1679 return llapi_getfacl(fname, cmd);
1682 static int lfs_setfacl(int argc, char **argv)
1684 char fname[PATH_MAX] = "", cmd[PATH_MAX] = "";
1686 if (acl_cmd_parse(argc, argv, fname, cmd))
1689 return llapi_setfacl(fname, cmd);
1692 int main(int argc, char **argv)
1698 ptl_initialize(argc, argv);
1699 if (obd_initialize(argc, argv) < 0)
1701 if (dbg_initialize(argc, argv) < 0)
1704 Parser_init("lfs > ", cmdlist);
1707 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1709 rc = Parser_commands();
1712 obd_finalize(argc, argv);