+ while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
+ /* Check if we have a mount point */
+ if (mntdir[0] == '\0')
+ continue;
+
+ rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
+ if (rc || path[0] != '\0')
+ break;
+ fsname[0] = '\0'; /* avoid matching in next loop */
+ mntdir[0] = '\0'; /* avoid matching in next loop */
+ }
+
+ return rc;
+}
+
+static int lfs_getname(int argc, char **argv)
+{
+ char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
+ int rc = 0, index = 0, c;
+ char buf[sizeof(struct obd_uuid)];
+
+ while ((c = getopt(argc, argv, "h")) != -1)
+ return CMD_HELP;
+
+ if (optind == argc) { /* no paths specified, get all paths. */
+ while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
+ rc = llapi_getname(mntdir, buf, sizeof(buf));
+ if (rc < 0) {
+ fprintf(stderr,
+ "cannot get name for `%s': %s\n",
+ mntdir, strerror(-rc));
+ break;
+ }
+
+ printf("%s %s\n", buf, mntdir);
+
+ path[0] = fsname[0] = mntdir[0] = 0;
+ }
+ } else { /* paths specified, only attempt to search these. */
+ for (; optind < argc; optind++) {
+ rc = llapi_getname(argv[optind], buf, sizeof(buf));
+ if (rc < 0) {
+ fprintf(stderr,
+ "cannot get name for `%s': %s\n",
+ argv[optind], strerror(-rc));
+ break;
+ }
+
+ printf("%s %s\n", buf, argv[optind]);
+ }
+ }
+ return rc;
+}
+
+static int lfs_check(int argc, char **argv)
+{
+ int rc;
+ char mntdir[PATH_MAX] = {'\0'};
+ int num_types = 1;
+ char *obd_types[2];
+ char obd_type1[4];
+ char obd_type2[4];
+
+ if (argc != 2) {
+ fprintf(stderr, "%s check: server type must be specified\n",
+ progname);
+ return CMD_HELP;
+ }
+
+ obd_types[0] = obd_type1;
+ obd_types[1] = obd_type2;
+
+ if (strcmp(argv[1], "osts") == 0) {
+ strcpy(obd_types[0], "osc");
+ } else if (strcmp(argv[1], "mds") == 0) {
+ strcpy(obd_types[0], "mdc");
+ } else if (strcmp(argv[1], "servers") == 0) {
+ num_types = 2;
+ strcpy(obd_types[0], "osc");
+ strcpy(obd_types[1], "mdc");
+ } else {
+ fprintf(stderr, "%s check: unrecognized option '%s'\n",
+ progname, argv[1]);
+ return CMD_HELP;
+ }
+
+ rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
+ if (rc < 0 || mntdir[0] == '\0') {
+ fprintf(stderr,
+ "%s check: cannot find mounted Lustre filesystem: %s\n",
+ progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
+ return rc;
+ }
+
+ rc = llapi_target_check(num_types, obd_types, mntdir);
+ if (rc)
+ fprintf(stderr, "%s check: cannot check target '%s': %s\n",
+ progname, argv[1], strerror(-rc));
+
+ return rc;
+
+}
+
+#ifdef HAVE_SYS_QUOTA_H
+#define ARG2INT(nr, str, msg) \
+do { \
+ char *endp; \
+ nr = strtol(str, &endp, 0); \
+ if (*endp != '\0') { \
+ fprintf(stderr, "%s: bad %s '%s'\n", \
+ progname, msg, str); \
+ return CMD_HELP; \
+ } \
+} while (0)
+
+#define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
+
+/* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
+ * returns the value or ULONG_MAX on integer overflow or incorrect format
+ * Notes:
+ * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
+ * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
+ * 3. empty integer value is interpreted as 0
+ */
+static unsigned long str2sec(const char* timestr)
+{
+ const char spec[] = "smhdw";
+ const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
+ unsigned long val = 0;
+ char *tail;
+
+ if (strpbrk(timestr, spec) == NULL) {
+ /* no specifiers inside the time string,
+ should treat it as an integer value */
+ val = strtoul(timestr, &tail, 10);
+ return *tail ? ULONG_MAX : val;
+ }
+
+ /* format string is XXwXXdXXhXXmXXs */
+ while (*timestr) {
+ unsigned long v;
+ int ind;
+ char* ptr;
+
+ v = strtoul(timestr, &tail, 10);
+ if (v == ULONG_MAX || *tail == '\0')
+ /* value too large (ULONG_MAX or more)
+ or missing specifier */
+ goto error;
+
+ ptr = strchr(spec, *tail);
+ if (ptr == NULL)
+ /* unknown specifier */
+ goto error;
+
+ ind = ptr - spec;
+
+ /* check if product will overflow the type */
+ if (!(v < ULONG_MAX / mult[ind]))
+ goto error;
+
+ ADD_OVERFLOW(val, mult[ind] * v);
+ if (val == ULONG_MAX)
+ goto error;
+
+ timestr = tail + 1;
+ }
+
+ return val;
+
+error:
+ return ULONG_MAX;
+}
+
+#define ARG2ULL(nr, str, def_units) \
+do { \
+ unsigned long long limit, units = def_units; \
+ int rc; \
+ \
+ rc = llapi_parse_size(str, &limit, &units, 1); \
+ if (rc < 0) { \
+ fprintf(stderr, "%s: invalid limit '%s'\n", \
+ progname, str); \
+ return CMD_HELP; \
+ } \
+ nr = limit; \
+} while (0)
+
+static inline int has_times_option(int argc, char **argv)
+{
+ int i;
+
+ for (i = 1; i < argc; i++)
+ if (!strcmp(argv[i], "-t"))
+ return 1;
+
+ return 0;
+}
+
+int lfs_setquota_times(int argc, char **argv)
+{
+ int c, rc;
+ struct if_quotactl qctl;
+ char *mnt, *obd_type = (char *)qctl.obd_type;
+ struct obd_dqblk *dqb = &qctl.qc_dqblk;
+ struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
+ struct option long_opts[] = {
+ { .val = 'b', .name = "block-grace", .has_arg = required_argument },
+ { .val = 'g', .name = "group", .has_arg = no_argument },
+ { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
+ { .val = 'p', .name = "projid", .has_arg = no_argument },
+ { .val = 't', .name = "times", .has_arg = no_argument },
+ { .val = 'u', .name = "user", .has_arg = no_argument },
+ { .name = NULL } };
+ int qtype;
+
+ memset(&qctl, 0, sizeof(qctl));
+ qctl.qc_cmd = LUSTRE_Q_SETINFO;
+ qctl.qc_type = ALLQUOTA;
+
+ while ((c = getopt_long(argc, argv, "b:gi:ptu",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'u':
+ qtype = USRQUOTA;
+ goto quota_type;
+ case 'g':
+ qtype = GRPQUOTA;
+ goto quota_type;
+ case 'p':
+ qtype = PRJQUOTA;
+quota_type:
+ if (qctl.qc_type != ALLQUOTA) {
+ fprintf(stderr, "error: -u/g/p can't be used "
+ "more than once\n");
+ return CMD_HELP;
+ }
+ qctl.qc_type = qtype;
+ break;
+ case 'b':
+ if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
+ fprintf(stderr, "error: bad block-grace: %s\n",
+ optarg);
+ return CMD_HELP;
+ }
+ dqb->dqb_valid |= QIF_BTIME;
+ break;
+ case 'i':
+ if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
+ fprintf(stderr, "error: bad inode-grace: %s\n",
+ optarg);
+ return CMD_HELP;
+ }
+ dqb->dqb_valid |= QIF_ITIME;
+ break;
+ case 't': /* Yes, of course! */
+ break;
+ default: /* getopt prints error message for us when opterr != 0 */
+ return CMD_HELP;
+ }
+ }
+
+ if (qctl.qc_type == ALLQUOTA) {
+ fprintf(stderr, "error: neither -u, -g nor -p specified\n");
+ return CMD_HELP;
+ }
+
+ if (optind != argc - 1) {
+ fprintf(stderr, "error: unexpected parameters encountered\n");
+ return CMD_HELP;
+ }
+
+ if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
+ (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
+ fprintf(stderr, "error: grace time is too large\n");
+ return CMD_HELP;
+ }
+
+ mnt = argv[optind];
+ rc = llapi_quotactl(mnt, &qctl);
+ if (rc) {
+ if (*obd_type)
+ fprintf(stderr, "%s %s ", obd_type,
+ obd_uuid2str(&qctl.obd_uuid));
+ fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
+ return rc;
+ }
+
+ return 0;
+}
+
+#define BSLIMIT (1 << 0)
+#define BHLIMIT (1 << 1)
+#define ISLIMIT (1 << 2)
+#define IHLIMIT (1 << 3)
+
+int lfs_setquota(int argc, char **argv)
+{
+ int c, rc = 0;
+ struct if_quotactl qctl;
+ char *mnt, *obd_type = (char *)qctl.obd_type;
+ struct obd_dqblk *dqb = &qctl.qc_dqblk;
+ struct option long_opts[] = {
+ { .val = 'b', .name = "block-softlimit",
+ .has_arg = required_argument },
+ { .val = 'B', .name = "block-hardlimit",
+ .has_arg = required_argument },
+ { .val = 'd', .name = "default", .has_arg = no_argument },
+ { .val = 'g', .name = "group", .has_arg = required_argument },
+ { .val = 'G', .name = "default-grp", .has_arg = no_argument },
+ { .val = 'i', .name = "inode-softlimit",
+ .has_arg = required_argument },
+ { .val = 'I', .name = "inode-hardlimit",
+ .has_arg = required_argument },
+ { .val = 'p', .name = "projid", .has_arg = required_argument },
+ { .val = 'P', .name = "default-prj", .has_arg = no_argument },
+ { .val = 'u', .name = "user", .has_arg = required_argument },
+ { .val = 'U', .name = "default-usr", .has_arg = no_argument },
+ { .name = NULL } };
+ unsigned limit_mask = 0;
+ char *endptr;
+ bool use_default = false;
+ int qtype;
+
+ if (has_times_option(argc, argv))
+ return lfs_setquota_times(argc, argv);
+
+ memset(&qctl, 0, sizeof(qctl));
+ qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
+ qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
+ * so it can be used as a marker that qc_type
+ * isn't reinitialized from command line */
+
+ while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'U':
+ qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
+ qtype = USRQUOTA;
+ qctl.qc_id = 0;
+ goto quota_type_def;
+ case 'u':
+ qtype = USRQUOTA;
+ rc = name2uid(&qctl.qc_id, optarg);
+ goto quota_type;
+ case 'G':
+ qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
+ qtype = GRPQUOTA;
+ qctl.qc_id = 0;
+ goto quota_type_def;
+ case 'g':
+ qtype = GRPQUOTA;
+ rc = name2gid(&qctl.qc_id, optarg);
+ goto quota_type;
+ case 'P':
+ qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
+ qtype = PRJQUOTA;
+ qctl.qc_id = 0;
+ goto quota_type_def;
+ case 'p':
+ qtype = PRJQUOTA;
+ rc = name2projid(&qctl.qc_id, optarg);
+quota_type:
+ if (rc) {
+ qctl.qc_id = strtoul(optarg, &endptr, 10);
+ if (*endptr != '\0') {
+ fprintf(stderr, "%s setquota: invalid"
+ " id '%s'\n", progname, optarg);
+ return -1;
+ }
+ }
+
+ if (qctl.qc_id == 0) {
+ fprintf(stderr, "%s setquota: can't set quota"
+ " for root usr/group/project.\n",
+ progname);
+ return -1;
+ }
+
+quota_type_def:
+ if (qctl.qc_type != ALLQUOTA) {
+ fprintf(stderr,
+ "%s setquota: only one of -u, -U, -g,"
+ " -G, -p or -P may be specified\n",
+ progname);
+ return CMD_HELP;
+ }
+ qctl.qc_type = qtype;
+ break;
+ case 'd':
+ qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
+ use_default = true;
+ break;
+ case 'b':
+ ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
+ dqb->dqb_bsoftlimit >>= 10;
+ limit_mask |= BSLIMIT;
+ if (dqb->dqb_bsoftlimit &&
+ dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
+ fprintf(stderr,
+ "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
+ "See '%s help setquota' or Lustre manual for details\n",
+ progname, dqb->dqb_bsoftlimit,
+ progname);
+ break;
+ case 'B':
+ ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
+ dqb->dqb_bhardlimit >>= 10;
+ limit_mask |= BHLIMIT;
+ if (dqb->dqb_bhardlimit &&
+ dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
+ fprintf(stderr,
+ "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
+ "See '%s help setquota' or Lustre manual for details\n",
+ progname, dqb->dqb_bhardlimit,
+ progname);
+ break;
+ case 'i':
+ ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
+ limit_mask |= ISLIMIT;
+ if (dqb->dqb_isoftlimit &&
+ dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
+ fprintf(stderr,
+ "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
+ "See '%s help setquota' or Lustre manual for details\n",
+ progname, dqb->dqb_isoftlimit,
+ progname);
+ break;
+ case 'I':
+ ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
+ limit_mask |= IHLIMIT;
+ if (dqb->dqb_ihardlimit &&
+ dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
+ fprintf(stderr,
+ "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
+ "See '%s help setquota' or Lustre manual for details\n",
+ progname, dqb->dqb_ihardlimit,
+ progname);
+ break;
+ default:
+ fprintf(stderr,
+ "%s setquota: unrecognized option '%s'\n",
+ progname, argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ if (qctl.qc_type == ALLQUOTA) {
+ fprintf(stderr,
+ "%s setquota: either -u or -g must be specified\n",
+ progname);
+ return CMD_HELP;
+ }
+
+ if (!use_default && limit_mask == 0) {
+ fprintf(stderr,
+ "%s setquota: at least one limit must be specified\n",
+ progname);
+ return CMD_HELP;
+ }
+
+ if (use_default && limit_mask != 0) {
+ fprintf(stderr,
+ "%s setquota: limits should not be specified when"
+ " using default quota\n",
+ progname);
+ return CMD_HELP;
+ }