Whamcloud - gitweb
LU-8232 obdclass: remove dead code
[fs/lustre-release.git] / lustre / obdclass / lprocfs_status.c
index 0cae0eb..7d7299c 100644 (file)
@@ -47,8 +47,8 @@
 #ifdef CONFIG_PROC_FS
 
 static int lprocfs_no_percpu_stats = 0;
-CFS_MODULE_PARM(lprocfs_no_percpu_stats, "i", int, 0644,
-                "Do not alloc percpu data for lprocfs stats");
+module_param(lprocfs_no_percpu_stats, int, 0644);
+MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
 
 #define MAX_STRING_SIZE 128
 
@@ -321,9 +321,10 @@ ssize_t lprocfs_uint_seq_write(struct file *file, const char __user *buffer,
                               size_t count, loff_t *off)
 {
        int *data = ((struct seq_file *)file->private_data)->private;
-       int val = 0, rc;
+       int rc;
+       __s64 val = 0;
 
-       rc = lprocfs_write_helper(buffer, count, &val);
+       rc = lprocfs_str_to_s64(buffer, count, &val);
        if (rc < 0)
                return rc;
 
@@ -353,14 +354,14 @@ lprocfs_atomic_seq_write(struct file *file, const char __user *buffer,
                        size_t count, loff_t *off)
 {
        atomic_t *atm = ((struct seq_file *)file->private_data)->private;
-       int val = 0;
+       __s64 val = 0;
        int rc;
 
-       rc = lprocfs_write_helper(buffer, count, &val);
+       rc = lprocfs_str_to_s64(buffer, count, &val);
        if (rc < 0)
                return rc;
 
-       if (val <= 0)
+       if (val <= 0 || val > INT_MAX)
                return -ERANGE;
 
        atomic_set(atm, val);
@@ -598,6 +599,7 @@ static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
 #undef flag2str
 
 static const char *obd_connect_names[] = {
+       /* flags names  */
        "read_only",
        "lov_index",
        "connect_from_mds",
@@ -657,46 +659,86 @@ static const char *obd_connect_names[] = {
        "unlink_close",
        "multi_mod_rpcs",
        "dir_stripe",
-       "fileset_mount",
+       "subtree",
        "lock_ahead",
        "bulk_mbits",
        "compact_obdo",
        "second_flags",
+       /* flags2 names */
+       "file_secctx",
        NULL
 };
 
-static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
+static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags,
+                                     __u64 flags2, const char *sep)
 {
        bool first = true;
-       __u64 mask = 1;
+       __u64 mask;
        int i;
 
-       for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
+       for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
                if (flags & mask) {
                        seq_printf(m, "%s%s",
                                   first ? "" : sep, obd_connect_names[i]);
                        first = false;
                }
        }
-       if (flags & ~(mask - 1))
+
+       if (flags & ~(mask - 1)) {
                seq_printf(m, "%sunknown_"LPX64,
                           first ? "" : sep, flags & ~(mask - 1));
+               first = false;
+       }
+
+       if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
+               return;
+
+       for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
+               if (flags2 & mask) {
+                       seq_printf(m, "%s%s",
+                                  first ? "" : sep, obd_connect_names[i]);
+                       first = false;
+               }
+       }
+
+       if (flags2 & ~(mask - 1)) {
+               seq_printf(m, "%sunknown2_"LPX64,
+                          first ? "" : sep, flags2 & ~(mask - 1));
+               first = false;
+       }
 }
 
-int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
+int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
+                         const char *sep)
 {
-       __u64 mask = 1;
+       __u64 mask;
        int i, ret = 0;
 
-       for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
+       for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
                if (flags & mask)
                        ret += snprintf(page + ret, count - ret, "%s%s",
                                        ret ? sep : "", obd_connect_names[i]);
        }
+
        if (flags & ~(mask - 1))
                ret += snprintf(page + ret, count - ret,
                                "%sunknown_"LPX64,
                                ret ? sep : "", flags & ~(mask - 1));
+
+       if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
+               return ret;
+
+       for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
+               if (flags2 & mask)
+                       ret += snprintf(page + ret, count - ret, "%s%s",
+                                       ret ? sep : "", obd_connect_names[i]);
+       }
+
+       if (flags2 & ~(mask - 1))
+               ret += snprintf(page + ret, count - ret,
+                               "%sunknown2_"LPX64,
+                               ret ? sep : "", flags2 & ~(mask - 1));
+
        return ret;
 }
 EXPORT_SYMBOL(obd_connect_flags2str);
@@ -783,6 +825,7 @@ int lprocfs_import_seq_show(struct seq_file *m, void *data)
                   obd2cli_tgt(obd),
                   ptlrpc_import_state_name(imp->imp_state));
        obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
+                                 imp->imp_connect_data.ocd_connect_flags2,
                                  ", ");
        seq_printf(m, " ]\n");
        obd_connect_data_seqprint(m, ocd);
@@ -988,11 +1031,14 @@ int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
 {
        struct obd_device *obd = data;
        __u64 flags;
+       __u64 flags2;
 
        LPROCFS_CLIMP_CHECK(obd);
        flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
+       flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
        seq_printf(m, "flags="LPX64"\n", flags);
-       obd_connect_seq_flags2str(m, flags, "\n");
+       seq_printf(m, "flags2="LPX64"\n", flags2);
+       obd_connect_seq_flags2str(m, flags, flags2, "\n");
        seq_printf(m, "\n");
        LPROCFS_CLIMP_EXIT(obd);
        return 0;
@@ -1224,7 +1270,6 @@ static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
        struct lprocfs_counter_header   *hdr;
        struct lprocfs_counter           ctr;
        int                              idx    = *(loff_t *)v;
-       int                              rc     = 0;
 
        if (idx == 0) {
                struct timeval now;
@@ -1232,34 +1277,25 @@ static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
                do_gettimeofday(&now);
                seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
                           "snapshot_time", now.tv_sec, now.tv_usec);
-               if (rc < 0)
-                       return rc;
        }
 
        hdr = &stats->ls_cnt_header[idx];
        lprocfs_stats_collect(stats, idx, &ctr);
 
        if (ctr.lc_count == 0)
-               goto out;
+               return 0;
 
        seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
                   ctr.lc_count, hdr->lc_units);
-       if (rc < 0)
-               goto out;
 
        if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
                seq_printf(p, " "LPD64" "LPD64" "LPD64,
                           ctr.lc_min, ctr.lc_max, ctr.lc_sum);
-               if (rc < 0)
-                       goto out;
                if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
                        seq_printf(p, " "LPD64, ctr.lc_sumsquare);
-               if (rc < 0)
-                       goto out;
        }
        seq_putc(p, '\n');
-out:
-       return (rc < 0) ? rc : 0;
+       return 0;
 }
 
 static const struct seq_operations lprocfs_stats_seq_sops = {
@@ -1360,9 +1396,8 @@ EXPORT_SYMBOL(lprocfs_counter_init);
 
 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
 {
-        LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
+       LPROCFS_MD_OP_INIT(num_private_stats, stats, get_root);
         LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
-        LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
@@ -1518,56 +1553,6 @@ __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
 }
 EXPORT_SYMBOL(lprocfs_read_helper);
 
-int lprocfs_write_helper(const char __user *buffer, unsigned long count,
-                         int *val)
-{
-        return lprocfs_write_frac_helper(buffer, count, val, 1);
-}
-EXPORT_SYMBOL(lprocfs_write_helper);
-
-int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
-                              int *val, int mult)
-{
-        char kernbuf[20], *end, *pbuf;
-
-        if (count > (sizeof(kernbuf) - 1))
-                return -EINVAL;
-
-       if (copy_from_user(kernbuf, buffer, count))
-                return -EFAULT;
-
-        kernbuf[count] = '\0';
-        pbuf = kernbuf;
-        if (*pbuf == '-') {
-                mult = -mult;
-                pbuf++;
-        }
-
-        *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
-        if (pbuf == end)
-                return -EINVAL;
-
-        if (end != NULL && *end == '.') {
-                int temp_val, pow = 1;
-                int i;
-
-                pbuf = end + 1;
-                if (strlen(pbuf) > 5)
-                        pbuf[5] = '\0'; /*only allow 5bits fractional*/
-
-                temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
-
-                if (pbuf < end) {
-                        for (i = 0; i < (end - pbuf); i++)
-                                pow *= 10;
-
-                        *val += temp_val / pow;
-                }
-        }
-        return 0;
-}
-EXPORT_SYMBOL(lprocfs_write_frac_helper);
-
 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
                              int mult)
 {
@@ -1647,77 +1632,309 @@ int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
 }
 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
 
-int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
-                            __u64 *val)
+/* Obtains the conversion factor for the unit specified */
+static int get_mult(char unit, __u64 *mult)
+{
+       __u64 units = 1;
+
+       switch (unit) {
+       /* peta, tera, giga, mega, and kilo */
+       case 'p':
+       case 'P':
+               units <<= 10;
+       case 't':
+       case 'T':
+               units <<= 10;
+       case 'g':
+       case 'G':
+               units <<= 10;
+       case 'm':
+       case 'M':
+               units <<= 10;
+       case 'k':
+       case 'K':
+               units <<= 10;
+               break;
+       /* some tests expect % to be accepted */
+       case '%':
+               units = 1;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       *mult = units;
+
+       return 0;
+}
+
+/*
+ * Ensures the numeric string is valid. The function provides the final
+ * multiplier in the case a unit exists at the end of the string. It also
+ * locates the start of the whole and fractional parts (if any). This
+ * function modifies the string so kstrtoull can be used to parse both
+ * the whole and fraction portions. This function also figures out
+ * the base of the number.
+ */
+static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
+                                 bool allow_units, char **whole, char **frac,
+                                 unsigned int *base)
 {
-        return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
+       bool hit_decimal = false;
+       bool hit_unit = false;
+       int rc = 0;
+       char *start;
+       *mult = def_mult;
+       *whole = NULL;
+       *frac = NULL;
+       *base = 10;
+
+       /* a hex string if it starts with "0x" */
+       if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
+               *base = 16;
+               buffer += 2;
+       }
+
+       start = buffer;
+
+       while (*buffer) {
+               /* allow for a single new line before the null terminator */
+               if (*buffer == '\n') {
+                       *buffer = '\0';
+                       buffer++;
+
+                       if (*buffer)
+                               return -EINVAL;
+
+                       break;
+               }
+
+               /* any chars after our unit indicates a malformed string */
+               if (hit_unit)
+                       return -EINVAL;
+
+               /* ensure we only hit one decimal */
+               if (*buffer == '.') {
+                       if (hit_decimal)
+                               return -EINVAL;
+
+                       /* if past start, there's a whole part */
+                       if (start != buffer)
+                               *whole = start;
+
+                       *buffer = '\0';
+                       start = buffer + 1;
+                       hit_decimal = true;
+               } else if (!isdigit(*buffer) &&
+                          !(*base == 16 && isxdigit(*buffer))) {
+                       if (allow_units) {
+                               /* if we allow units, attempt to get mult */
+                               hit_unit = true;
+                               rc = get_mult(*buffer, mult);
+                               if (rc)
+                                       return rc;
+
+                               /* string stops here, but keep processing */
+                               *buffer = '\0';
+                       } else {
+                               /* bad string */
+                               return -EINVAL;
+                       }
+               }
+
+               buffer++;
+       }
+
+       if (hit_decimal) {
+               /* hit a decimal, make sure there's a fractional part */
+               if (!*start)
+                       return -EINVAL;
+
+               *frac = start;
+       } else {
+               /* didn't hit a decimal, but may have a whole part */
+               if (start != buffer && *start)
+                       *whole = start;
+       }
+
+       /* malformed string if we didn't get anything */
+       if (!*frac && !*whole)
+               return -EINVAL;
+
+       return 0;
 }
-EXPORT_SYMBOL(lprocfs_write_u64_helper);
 
-int lprocfs_write_frac_u64_helper(const char __user *buffer,
-                                 unsigned long count,
-                                 __u64 *val, int mult)
+/*
+ * Parses a numeric string which can contain a whole and fraction portion
+ * into a __u64. Accepts a multiplier to apply to the value parsed. Also
+ * allows the string to have a unit at the end. The function handles
+ * wrapping of the final unsigned value.
+ */
+static int str_to_u64_parse(char *buffer, unsigned long count,
+                           __u64 *val, __u64 def_mult, bool allow_units)
 {
-        char kernbuf[22], *end, *pbuf;
-        __u64 whole, frac = 0, units;
-        unsigned frac_d = 1;
+       __u64 whole = 0;
+       __u64 frac = 0;
+       unsigned int frac_d = 1;
+       __u64 wrap_indicator = ULLONG_MAX;
+       int rc = 0;
+       __u64 mult;
+       char *strwhole;
+       char *strfrac;
+       unsigned int base = 10;
 
-        if (count > (sizeof(kernbuf) - 1))
-                return -EINVAL;
+       rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
+                                   &strwhole, &strfrac, &base);
 
-       if (copy_from_user(kernbuf, buffer, count))
-                return -EFAULT;
+       if (rc)
+               return rc;
 
-        kernbuf[count] = '\0';
-        pbuf = kernbuf;
-        if (*pbuf == '-') {
-                mult = -mult;
-                pbuf++;
-        }
+       if (mult == 0) {
+               *val = 0;
+               return 0;
+       }
 
-        whole = simple_strtoull(pbuf, &end, 10);
-        if (pbuf == end)
-                return -EINVAL;
+       /* the multiplier limits how large the value can be */
+       wrap_indicator /=  mult;
 
-        if (end != NULL && *end == '.') {
-                int i;
-                pbuf = end + 1;
+       if (strwhole) {
+               rc = kstrtoull(strwhole, base, &whole);
+               if (rc)
+                       return rc;
 
-                /* need to limit frac_d to a __u32 */
-                if (strlen(pbuf) > 10)
-                        pbuf[10] = '\0';
+               if (whole > wrap_indicator)
+                       return -ERANGE;
 
-                frac = simple_strtoull(pbuf, &end, 10);
-                /* count decimal places */
-                for (i = 0; i < (end - pbuf); i++)
-                        frac_d *= 10;
-        }
+               whole *= mult;
+       }
 
-        units = 1;
-       if (end != NULL) {
-               switch (*end) {
-               case 'p': case 'P':
-                       units <<= 10;
-               case 't': case 'T':
-                       units <<= 10;
-               case 'g': case 'G':
-                       units <<= 10;
-               case 'm': case 'M':
-                       units <<= 10;
-               case 'k': case 'K':
-                       units <<= 10;
+       if (strfrac) {
+               if (strlen(strfrac) > 10)
+                       strfrac[10] = '\0';
+
+               rc = kstrtoull(strfrac, base, &frac);
+               if (rc)
+                       return rc;
+
+               /* determine power of fractional portion */
+               while (*strfrac) {
+                       frac_d *= base;
+                       strfrac++;
                }
+
+               /* fractional portion is too large to perform calculation */
+               if (frac > wrap_indicator)
+                       return -ERANGE;
+
+               frac *= mult;
+               do_div(frac, frac_d);
        }
-        /* Specified units override the multiplier */
-       if (units > 1)
-                mult = mult < 0 ? -units : units;
 
-        frac *= mult;
-        do_div(frac, frac_d);
-        *val = whole * mult + frac;
-        return 0;
+       /* check that the sum of whole and fraction fits in u64 */
+       if (whole > (ULLONG_MAX - frac))
+               return -ERANGE;
+
+       *val = whole + frac;
+
+       return 0;
+}
+
+/*
+ * This function parses numeric/hex strings into __s64. It accepts a multiplier
+ * which will apply to the value parsed. It also can allow the string to
+ * have a unit as the last character. The function handles overflow/underflow
+ * of the signed integer.
+ */
+static int str_to_s64_internal(const char __user *buffer, unsigned long count,
+                              __s64 *val, __u64 def_mult, bool allow_units)
+{
+       char kernbuf[22];
+       __u64 tmp;
+       unsigned int offset = 0;
+       int signed sign = 1;
+       __u64 max = LLONG_MAX;
+       int rc = 0;
+
+       if (count > (sizeof(kernbuf) - 1))
+               return -EINVAL;
+
+       if (copy_from_user(kernbuf, buffer, count))
+               return -EFAULT;
+
+       kernbuf[count] = '\0';
+
+       /* keep track of our sign */
+       if (*kernbuf == '-') {
+               sign = -1;
+               offset++;
+               /* equivalent to max = -LLONG_MIN, avoids overflow */
+               max++;
+       }
+
+       rc = str_to_u64_parse(kernbuf + offset, count - offset,
+                             &tmp, def_mult, allow_units);
+       if (rc)
+               return rc;
+
+       /* check for overflow/underflow */
+       if (max < tmp)
+               return -ERANGE;
+
+       *val = (__s64)tmp * sign;
+
+       return 0;
+}
+
+/**
+ * Convert a user string into a signed 64 bit number. This function produces
+ * an error when the value parsed from the string underflows or
+ * overflows. This function accepts strings which contain digits and
+ * optionally a decimal or hex strings which are prefixed with "0x".
+ *
+ * \param[in] buffer   string consisting of numbers and optionally a decimal
+ * \param[in] count    buffer length
+ * \param[in] val      if successful, the value represented by the string
+ *
+ * \retval             0 on success
+ * \retval             negative number on error
+ */
+int lprocfs_str_to_s64(const char __user *buffer, unsigned long count,
+                      __s64 *val)
+{
+       return str_to_s64_internal(buffer, count, val, 1, false);
+}
+EXPORT_SYMBOL(lprocfs_str_to_s64);
+
+/**
+ * Convert a user string into a signed 64 bit number. This function produces
+ * an error when the value parsed from the string times multiplier underflows or
+ * overflows. This function only accepts strings that contains digits, an
+ * optional decimal, and a char representing a unit at the end. If a unit is
+ * specified in the string, the multiplier provided by the caller is ignored.
+ * This function can also accept hexadecimal strings which are prefixed with
+ * "0x".
+ *
+ * \param[in] buffer   string consisting of numbers, a decimal, and a unit
+ * \param[in] count    buffer length
+ * \param[in] val      if successful, the value represented by the string
+ * \param[in] defunit  default unit if string doesn't contain one
+ *
+ * \retval             0 on success
+ * \retval             negative number on error
+ */
+int lprocfs_str_with_units_to_s64(const char __user *buffer,
+                                 unsigned long count, __s64 *val, char defunit)
+{
+       __u64 mult;
+       int rc;
+
+       rc = get_mult(defunit, &mult);
+       if (rc)
+               return rc;
+
+       return str_to_s64_internal(buffer, count, val, mult, true);
 }
-EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
+EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
 
 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
 {