Whamcloud - gitweb
LU-13314 utils: fix lfs find time calculation margin
[fs/lustre-release.git] / lustre / utils / lfs.c
index 82fa2c5..64d5f88 100644 (file)
@@ -4137,20 +4137,21 @@ static int lfs_poollist(int argc, char **argv)
        return llapi_poollist(argv[1]);
 }
 
+#define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
                       char *str)
 {
        long long t = 0;
-       int res = 0;
+       int sign = 0;
        char *endptr = "AD";
        char *timebuf;
 
        if (str[0] == '+')
-               res = 1;
+               sign = 1;
        else if (str[0] == '-')
-               res = -1;
+               sign = -1;
 
-       if (res)
+       if (sign)
                str++;
 
        for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
@@ -4162,9 +4163,12 @@ static time_t set_time(struct find_param *param, time_t *time, time_t *set,
                        unit *= 52; /* 52 weeks + 1 day below */
                case  'w':      /* fallthrough */
                        unit *= 7;
+                       if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
+                               param->fp_time_margin *= (1 + unit / 52);
+                       unit += (*endptr == 'y'); /* +1 day for 365 days/year */
                case '\0': /* days are default unit if none used */
                case  'd':      /* fallthrough */
-                       unit = (unit + (*endptr == 'y')) * 24;
+                       unit *= 24;
                case  'h':      /* fallthrough */
                        unit *= 60;
                case  'm':      /* fallthrough */
@@ -4186,7 +4190,7 @@ static time_t set_time(struct find_param *param, time_t *time, time_t *set,
                t += val * unit;
        }
        if (*time < t) {
-               if (res != 0)
+               if (sign != 0)
                        str--;
                fprintf(stderr, "%s find: bad time '%s': too large\n",
                        progname, str);
@@ -4195,7 +4199,7 @@ static time_t set_time(struct find_param *param, time_t *time, time_t *set,
 
        *set = *time - t;
 
-       return res;
+       return sign;
 }
 
 static int str2quotaid(__u32 *id, const char *arg)
@@ -4298,7 +4302,7 @@ static int lfs_find(int argc, char **argv)
        struct find_param param = {
                .fp_max_depth = -1,
                .fp_quiet = 1,
-               .fp_time_margin = 24 * 60 * 60,
+               .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
        };
        struct option long_opts[] = {
        { .val = 'A',   .name = "atime",        .has_arg = required_argument },
@@ -7324,16 +7328,39 @@ static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
        }
 }
 
+static int tgt_name2index(const char *tgtname, unsigned int *idx)
+{
+       char *dash, *endp;
+
+       /* format is "lustre-OST0001" */
+       dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
+       if (!dash) {
+               fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
+               return -EINVAL;
+       }
+       dash += 4;
+
+       *idx = strtoul(dash, &endp, 16);
+       if (*idx > 0xffff) {
+               fprintf(stderr, "wrong index %s\n", tgtname);
+               return -ERANGE;
+       }
+
+       return 0;
+}
+
 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
                           bool h, __u64 *total)
 {
-       int rc = 0, rc1 = 0, count = 0;
+       int rc = 0, rc1 = 0, count = 0, i = 0;
+       char **list = NULL, *buffer = NULL;
        __u32 valid = qctl->qc_valid;
 
-       /*
-        * TODO: for commands LUSTRE_Q_"S\|G"ETQUOTAPOOL we need
-        * to go only through OSTs that belong to requested pool.
-        */
+       if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
+               return 0;
+
+       /* Is it correct for the case OST0000, OST0002, OST0003 -
+        * we will ask OST0001 that is absent and won't ask OST0003? */
        rc = llapi_get_obd_count(mnt, &count, is_mdt);
        if (rc) {
                fprintf(stderr, "can not get %s count: %s\n",
@@ -7341,7 +7368,39 @@ static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
                return rc;
        }
 
-       for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
+       if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
+               char fname[PATH_MAX];
+               char fsname[LUSTRE_MAXFSNAME + 1];
+               int bufsize = sizeof(struct obd_uuid) * count;
+
+               rc = llapi_search_fsname(mnt, fsname);
+               if (rc) {
+                       fprintf(stderr, "cannot get fsname for mountpoint %s\n",
+                               mnt);
+                       goto out;
+               }
+               buffer = malloc(bufsize + sizeof(*list) * count);
+               if (!buffer)
+                       return -ENOMEM;
+               list = (char **)(buffer + bufsize);
+               snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
+               count = llapi_get_poolmembers(fname, list, count,
+                                             buffer, bufsize);
+               if (count <= 0)
+                       goto out;
+       }
+
+       for (i = 0; i < count; i++) {
+               if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
+                       unsigned int index;
+
+                       if (tgt_name2index(list[i], &index))
+                               continue;
+                       qctl->qc_idx = index;
+               } else {
+                       qctl->qc_idx = i;
+               }
+
                qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
                rc = llapi_quotactl(mnt, qctl);
                if (rc) {
@@ -7364,6 +7423,8 @@ static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
                                   qctl->qc_dqblk.dqb_bhardlimit;
        }
 out:
+       if (buffer)
+               free(buffer);
        qctl->qc_valid = valid;
        return rc ? : rc1;
 }