linux/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
<<
>>
Prefs
   1/*
   2 * GPL HEADER START
   3 *
   4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 only,
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but
  11 * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * General Public License version 2 for more details (a copy is included
  14 * in the LICENSE file that accompanied this code).
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * version 2 along with this program; If not, see
  18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  19 *
  20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21 * CA 95054 USA or visit www.sun.com if you need additional information or
  22 * have any questions.
  23 *
  24 * GPL HEADER END
  25 */
  26/*
  27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2011, 2015, Intel Corporation.
  31 */
  32/*
  33 * This file is part of Lustre, http://www.lustre.org/
  34 * Lustre is a trademark of Sun Microsystems, Inc.
  35 *
  36 * lustre/obdclass/lprocfs_status.c
  37 *
  38 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
  39 */
  40
  41#define DEBUG_SUBSYSTEM S_CLASS
  42
  43#include "../include/obd_class.h"
  44#include "../include/lprocfs_status.h"
  45#include "../include/lustre/lustre_idl.h"
  46#include <linux/seq_file.h>
  47#include <linux/ctype.h>
  48
  49static const char * const obd_connect_names[] = {
  50        "read_only",
  51        "lov_index",
  52        "unused",
  53        "write_grant",
  54        "server_lock",
  55        "version",
  56        "request_portal",
  57        "acl",
  58        "xattr",
  59        "create_on_write",
  60        "truncate_lock",
  61        "initial_transno",
  62        "inode_bit_locks",
  63        "join_file(obsolete)",
  64        "getattr_by_fid",
  65        "no_oh_for_devices",
  66        "remote_client",
  67        "remote_client_by_force",
  68        "max_byte_per_rpc",
  69        "64bit_qdata",
  70        "mds_capability",
  71        "oss_capability",
  72        "early_lock_cancel",
  73        "som",
  74        "adaptive_timeouts",
  75        "lru_resize",
  76        "mds_mds_connection",
  77        "real_conn",
  78        "change_qunit_size",
  79        "alt_checksum_algorithm",
  80        "fid_is_enabled",
  81        "version_recovery",
  82        "pools",
  83        "grant_shrink",
  84        "skip_orphan",
  85        "large_ea",
  86        "full20",
  87        "layout_lock",
  88        "64bithash",
  89        "object_max_bytes",
  90        "imp_recov",
  91        "jobstats",
  92        "umask",
  93        "einprogress",
  94        "grant_param",
  95        "flock_owner",
  96        "lvb_type",
  97        "nanoseconds_times",
  98        "lightweight_conn",
  99        "short_io",
 100        "pingless",
 101        "flock_deadlock",
 102        "disp_stripe",
 103        "unknown",
 104        NULL
 105};
 106
 107int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
 108{
 109        __u64 mask = 1;
 110        int i, ret = 0;
 111
 112        for (i = 0; obd_connect_names[i]; i++, mask <<= 1) {
 113                if (flags & mask)
 114                        ret += snprintf(page + ret, count - ret, "%s%s",
 115                                        ret ? sep : "", obd_connect_names[i]);
 116        }
 117        if (flags & ~(mask - 1))
 118                ret += snprintf(page + ret, count - ret,
 119                                "%sunknown flags %#llx",
 120                                ret ? sep : "", flags & ~(mask - 1));
 121        return ret;
 122}
 123EXPORT_SYMBOL(obd_connect_flags2str);
 124
 125int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
 126                             int mult)
 127{
 128        long decimal_val, frac_val;
 129        int prtn;
 130
 131        if (count < 10)
 132                return -EINVAL;
 133
 134        decimal_val = val / mult;
 135        prtn = snprintf(buffer, count, "%ld", decimal_val);
 136        frac_val = val % mult;
 137
 138        if (prtn < (count - 4) && frac_val > 0) {
 139                long temp_frac;
 140                int i, temp_mult = 1, frac_bits = 0;
 141
 142                temp_frac = frac_val * 10;
 143                buffer[prtn++] = '.';
 144                while (frac_bits < 2 && (temp_frac / mult) < 1) {
 145                        /* only reserved 2 bits fraction */
 146                        buffer[prtn++] = '0';
 147                        temp_frac *= 10;
 148                        frac_bits++;
 149                }
 150                /*
 151                 * Need to think these cases :
 152                 *      1. #echo x.00 > /sys/xxx       output result : x
 153                 *      2. #echo x.0x > /sys/xxx       output result : x.0x
 154                 *      3. #echo x.x0 > /sys/xxx       output result : x.x
 155                 *      4. #echo x.xx > /sys/xxx       output result : x.xx
 156                 *      Only reserved 2 bits fraction.
 157                 */
 158                for (i = 0; i < (5 - prtn); i++)
 159                        temp_mult *= 10;
 160
 161                frac_bits = min((int)count - prtn, 3 - frac_bits);
 162                prtn += snprintf(buffer + prtn, frac_bits, "%ld",
 163                                 frac_val * temp_mult / mult);
 164
 165                prtn--;
 166                while (buffer[prtn] < '1' || buffer[prtn] > '9') {
 167                        prtn--;
 168                        if (buffer[prtn] == '.') {
 169                                prtn--;
 170                                break;
 171                        }
 172                }
 173                prtn++;
 174        }
 175        buffer[prtn++] = '\n';
 176        return prtn;
 177}
 178EXPORT_SYMBOL(lprocfs_read_frac_helper);
 179
 180int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
 181                              int *val, int mult)
 182{
 183        char kernbuf[20], *end, *pbuf;
 184
 185        if (count > (sizeof(kernbuf) - 1))
 186                return -EINVAL;
 187
 188        if (copy_from_user(kernbuf, buffer, count))
 189                return -EFAULT;
 190
 191        kernbuf[count] = '\0';
 192        pbuf = kernbuf;
 193        if (*pbuf == '-') {
 194                mult = -mult;
 195                pbuf++;
 196        }
 197
 198        *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
 199        if (pbuf == end)
 200                return -EINVAL;
 201
 202        if (end && *end == '.') {
 203                int temp_val, pow = 1;
 204                int i;
 205
 206                pbuf = end + 1;
 207                if (strlen(pbuf) > 5)
 208                        pbuf[5] = '\0'; /*only allow 5bits fractional*/
 209
 210                temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
 211
 212                if (pbuf < end) {
 213                        for (i = 0; i < (end - pbuf); i++)
 214                                pow *= 10;
 215
 216                        *val += temp_val / pow;
 217                }
 218        }
 219        return 0;
 220}
 221EXPORT_SYMBOL(lprocfs_write_frac_helper);
 222
 223static int lprocfs_no_percpu_stats;
 224module_param(lprocfs_no_percpu_stats, int, 0644);
 225MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
 226
 227#define MAX_STRING_SIZE 128
 228
 229int lprocfs_single_release(struct inode *inode, struct file *file)
 230{
 231        return single_release(inode, file);
 232}
 233EXPORT_SYMBOL(lprocfs_single_release);
 234
 235int lprocfs_seq_release(struct inode *inode, struct file *file)
 236{
 237        return seq_release(inode, file);
 238}
 239EXPORT_SYMBOL(lprocfs_seq_release);
 240
 241/* lprocfs API calls */
 242
 243struct dentry *ldebugfs_add_simple(struct dentry *root,
 244                                   char *name, void *data,
 245                                   struct file_operations *fops)
 246{
 247        struct dentry *entry;
 248        umode_t mode = 0;
 249
 250        if (!root || !name || !fops)
 251                return ERR_PTR(-EINVAL);
 252
 253        if (fops->read)
 254                mode = 0444;
 255        if (fops->write)
 256                mode |= 0200;
 257        entry = debugfs_create_file(name, mode, root, data, fops);
 258        if (IS_ERR_OR_NULL(entry)) {
 259                CERROR("LprocFS: No memory to create <debugfs> entry %s\n", name);
 260                return entry ?: ERR_PTR(-ENOMEM);
 261        }
 262        return entry;
 263}
 264EXPORT_SYMBOL_GPL(ldebugfs_add_simple);
 265
 266static struct file_operations lprocfs_generic_fops = { };
 267
 268int ldebugfs_add_vars(struct dentry *parent,
 269                      struct lprocfs_vars *list,
 270                      void *data)
 271{
 272        if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
 273                return -EINVAL;
 274
 275        while (list->name) {
 276                struct dentry *entry;
 277                umode_t mode = 0;
 278
 279                if (list->proc_mode != 0000) {
 280                        mode = list->proc_mode;
 281                } else if (list->fops) {
 282                        if (list->fops->read)
 283                                mode = 0444;
 284                        if (list->fops->write)
 285                                mode |= 0200;
 286                }
 287                entry = debugfs_create_file(list->name, mode, parent,
 288                                            list->data ?: data,
 289                                            list->fops ?: &lprocfs_generic_fops
 290                                           );
 291                if (IS_ERR_OR_NULL(entry))
 292                        return entry ? PTR_ERR(entry) : -ENOMEM;
 293                list++;
 294        }
 295        return 0;
 296}
 297EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
 298
 299void ldebugfs_remove(struct dentry **entryp)
 300{
 301        debugfs_remove_recursive(*entryp);
 302        *entryp = NULL;
 303}
 304EXPORT_SYMBOL_GPL(ldebugfs_remove);
 305
 306struct dentry *ldebugfs_register(const char *name,
 307                                 struct dentry *parent,
 308                                 struct lprocfs_vars *list, void *data)
 309{
 310        struct dentry *entry;
 311
 312        entry = debugfs_create_dir(name, parent);
 313        if (IS_ERR_OR_NULL(entry)) {
 314                entry = entry ?: ERR_PTR(-ENOMEM);
 315                goto out;
 316        }
 317
 318        if (!IS_ERR_OR_NULL(list)) {
 319                int rc;
 320
 321                rc = ldebugfs_add_vars(entry, list, data);
 322                if (rc != 0) {
 323                        debugfs_remove(entry);
 324                        entry = ERR_PTR(rc);
 325                }
 326        }
 327out:
 328        return entry;
 329}
 330EXPORT_SYMBOL_GPL(ldebugfs_register);
 331
 332/* Generic callbacks */
 333int lprocfs_rd_uint(struct seq_file *m, void *data)
 334{
 335        seq_printf(m, "%u\n", *(unsigned int *)data);
 336        return 0;
 337}
 338EXPORT_SYMBOL(lprocfs_rd_uint);
 339
 340int lprocfs_wr_uint(struct file *file, const char __user *buffer,
 341                    unsigned long count, void *data)
 342{
 343        unsigned *p = data;
 344        char dummy[MAX_STRING_SIZE + 1], *end;
 345        unsigned long tmp;
 346
 347        dummy[MAX_STRING_SIZE] = '\0';
 348        if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
 349                return -EFAULT;
 350
 351        tmp = simple_strtoul(dummy, &end, 0);
 352        if (dummy == end)
 353                return -EINVAL;
 354
 355        *p = (unsigned int)tmp;
 356        return count;
 357}
 358EXPORT_SYMBOL(lprocfs_wr_uint);
 359
 360static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
 361                         char *buf)
 362{
 363        struct obd_device *obd = container_of(kobj, struct obd_device,
 364                                              obd_kobj);
 365
 366        return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
 367}
 368LUSTRE_RO_ATTR(uuid);
 369
 370static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
 371                              char *buf)
 372{
 373        struct obd_device *obd = container_of(kobj, struct obd_device,
 374                                              obd_kobj);
 375        struct obd_statfs  osfs;
 376        int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
 377                            cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 378                            OBD_STATFS_NODELAY);
 379        if (!rc)
 380                return sprintf(buf, "%u\n", osfs.os_bsize);
 381
 382        return rc;
 383}
 384LUSTRE_RO_ATTR(blocksize);
 385
 386static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
 387                                char *buf)
 388{
 389        struct obd_device *obd = container_of(kobj, struct obd_device,
 390                                              obd_kobj);
 391        struct obd_statfs  osfs;
 392        int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
 393                            cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 394                            OBD_STATFS_NODELAY);
 395        if (!rc) {
 396                __u32 blk_size = osfs.os_bsize >> 10;
 397                __u64 result = osfs.os_blocks;
 398
 399                while (blk_size >>= 1)
 400                        result <<= 1;
 401
 402                return sprintf(buf, "%llu\n", result);
 403        }
 404
 405        return rc;
 406}
 407LUSTRE_RO_ATTR(kbytestotal);
 408
 409static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
 410                               char *buf)
 411{
 412        struct obd_device *obd = container_of(kobj, struct obd_device,
 413                                              obd_kobj);
 414        struct obd_statfs  osfs;
 415        int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
 416                            cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 417                            OBD_STATFS_NODELAY);
 418        if (!rc) {
 419                __u32 blk_size = osfs.os_bsize >> 10;
 420                __u64 result = osfs.os_bfree;
 421
 422                while (blk_size >>= 1)
 423                        result <<= 1;
 424
 425                return sprintf(buf, "%llu\n", result);
 426        }
 427
 428        return rc;
 429}
 430LUSTRE_RO_ATTR(kbytesfree);
 431
 432static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
 433                                char *buf)
 434{
 435        struct obd_device *obd = container_of(kobj, struct obd_device,
 436                                              obd_kobj);
 437        struct obd_statfs  osfs;
 438        int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
 439                            cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 440                            OBD_STATFS_NODELAY);
 441        if (!rc) {
 442                __u32 blk_size = osfs.os_bsize >> 10;
 443                __u64 result = osfs.os_bavail;
 444
 445                while (blk_size >>= 1)
 446                        result <<= 1;
 447
 448                return sprintf(buf, "%llu\n", result);
 449        }
 450
 451        return rc;
 452}
 453LUSTRE_RO_ATTR(kbytesavail);
 454
 455static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
 456                               char *buf)
 457{
 458        struct obd_device *obd = container_of(kobj, struct obd_device,
 459                                              obd_kobj);
 460        struct obd_statfs  osfs;
 461        int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
 462                            cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 463                            OBD_STATFS_NODELAY);
 464        if (!rc)
 465                return sprintf(buf, "%llu\n", osfs.os_files);
 466
 467        return rc;
 468}
 469LUSTRE_RO_ATTR(filestotal);
 470
 471static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
 472                              char *buf)
 473{
 474        struct obd_device *obd = container_of(kobj, struct obd_device,
 475                                              obd_kobj);
 476        struct obd_statfs  osfs;
 477        int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
 478                            cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 479                            OBD_STATFS_NODELAY);
 480        if (!rc)
 481                return sprintf(buf, "%llu\n", osfs.os_ffree);
 482
 483        return rc;
 484}
 485LUSTRE_RO_ATTR(filesfree);
 486
 487int lprocfs_rd_server_uuid(struct seq_file *m, void *data)
 488{
 489        struct obd_device *obd = data;
 490        struct obd_import *imp;
 491        char *imp_state_name = NULL;
 492        int rc;
 493
 494        LASSERT(obd);
 495        rc = lprocfs_climp_check(obd);
 496        if (rc)
 497                return rc;
 498
 499        imp = obd->u.cli.cl_import;
 500        imp_state_name = ptlrpc_import_state_name(imp->imp_state);
 501        seq_printf(m, "%s\t%s%s\n",
 502                   obd2cli_tgt(obd), imp_state_name,
 503                   imp->imp_deactive ? "\tDEACTIVATED" : "");
 504
 505        up_read(&obd->u.cli.cl_sem);
 506
 507        return 0;
 508}
 509EXPORT_SYMBOL(lprocfs_rd_server_uuid);
 510
 511int lprocfs_rd_conn_uuid(struct seq_file *m, void *data)
 512{
 513        struct obd_device *obd = data;
 514        struct ptlrpc_connection *conn;
 515        int rc;
 516
 517        LASSERT(obd);
 518
 519        rc = lprocfs_climp_check(obd);
 520        if (rc)
 521                return rc;
 522
 523        conn = obd->u.cli.cl_import->imp_connection;
 524        if (conn && obd->u.cli.cl_import)
 525                seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
 526        else
 527                seq_puts(m, "<none>\n");
 528
 529        up_read(&obd->u.cli.cl_sem);
 530
 531        return 0;
 532}
 533EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
 534
 535/** add up per-cpu counters */
 536void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
 537                           struct lprocfs_counter *cnt)
 538{
 539        unsigned int                    num_entry;
 540        struct lprocfs_counter          *percpu_cntr;
 541        int                             i;
 542        unsigned long                   flags = 0;
 543
 544        memset(cnt, 0, sizeof(*cnt));
 545
 546        if (!stats) {
 547                /* set count to 1 to avoid divide-by-zero errs in callers */
 548                cnt->lc_count = 1;
 549                return;
 550        }
 551
 552        cnt->lc_min = LC_MIN_INIT;
 553
 554        num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
 555
 556        for (i = 0; i < num_entry; i++) {
 557                if (!stats->ls_percpu[i])
 558                        continue;
 559                percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
 560
 561                cnt->lc_count += percpu_cntr->lc_count;
 562                cnt->lc_sum += percpu_cntr->lc_sum;
 563                if (percpu_cntr->lc_min < cnt->lc_min)
 564                        cnt->lc_min = percpu_cntr->lc_min;
 565                if (percpu_cntr->lc_max > cnt->lc_max)
 566                        cnt->lc_max = percpu_cntr->lc_max;
 567                cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
 568        }
 569
 570        lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
 571}
 572EXPORT_SYMBOL(lprocfs_stats_collect);
 573
 574/**
 575 * Append a space separated list of current set flags to str.
 576 */
 577#define flag2str(flag, first)                                           \
 578        do {                                                            \
 579                if (imp->imp_##flag)                                    \
 580                        seq_printf(m, "%s" #flag, first ? "" : ", ");   \
 581        } while (0)
 582static int obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
 583{
 584        bool first = true;
 585
 586        if (imp->imp_obd->obd_no_recov) {
 587                seq_printf(m, "no_recov");
 588                first = false;
 589        }
 590
 591        flag2str(invalid, first);
 592        first = false;
 593        flag2str(deactive, first);
 594        flag2str(replayable, first);
 595        flag2str(pingable, first);
 596        return 0;
 597}
 598
 599#undef flags2str
 600
 601static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
 602{
 603        __u64 mask = 1;
 604        int i;
 605        bool first = true;
 606
 607        for (i = 0; obd_connect_names[i]; i++, mask <<= 1) {
 608                if (flags & mask) {
 609                        seq_printf(m, "%s%s",
 610                                   first ? sep : "", obd_connect_names[i]);
 611                        first = false;
 612                }
 613        }
 614        if (flags & ~(mask - 1))
 615                seq_printf(m, "%sunknown flags %#llx",
 616                           first ? sep : "", flags & ~(mask - 1));
 617}
 618
 619int lprocfs_rd_import(struct seq_file *m, void *data)
 620{
 621        char                            nidstr[LNET_NIDSTR_SIZE];
 622        struct lprocfs_counter          ret;
 623        struct lprocfs_counter_header   *header;
 624        struct obd_device               *obd    = data;
 625        struct obd_import               *imp;
 626        struct obd_import_conn          *conn;
 627        int                             j;
 628        int                             k;
 629        int                             rw      = 0;
 630        int                             rc;
 631
 632        LASSERT(obd);
 633        rc = lprocfs_climp_check(obd);
 634        if (rc)
 635                return rc;
 636
 637        imp = obd->u.cli.cl_import;
 638
 639        seq_printf(m,
 640                   "import:\n"
 641                   "    name: %s\n"
 642                   "    target: %s\n"
 643                   "    state: %s\n"
 644                   "    instance: %u\n"
 645                   "    connect_flags: [ ",
 646                   obd->obd_name,
 647                   obd2cli_tgt(obd),
 648                   ptlrpc_import_state_name(imp->imp_state),
 649                   imp->imp_connect_data.ocd_instance);
 650        obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
 651                                  ", ");
 652        seq_printf(m,
 653                   " ]\n"
 654                   "    import_flags: [ ");
 655        obd_import_flags2str(imp, m);
 656
 657        seq_printf(m,
 658                   " ]\n"
 659                   "    connection:\n"
 660                   "       failover_nids: [ ");
 661        spin_lock(&imp->imp_lock);
 662        j = 0;
 663        list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
 664                libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
 665                                 nidstr, sizeof(nidstr));
 666                seq_printf(m, "%s%s", j ? ", " : "", nidstr);
 667                j++;
 668        }
 669        if (imp->imp_connection)
 670                libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
 671                                 nidstr, sizeof(nidstr));
 672        else
 673                strncpy(nidstr, "<none>", sizeof(nidstr));
 674        seq_printf(m,
 675                   " ]\n"
 676                   "       current_connection: %s\n"
 677                   "       connection_attempts: %u\n"
 678                   "       generation: %u\n"
 679                   "       in-progress_invalidations: %u\n",
 680                   nidstr,
 681                   imp->imp_conn_cnt,
 682                   imp->imp_generation,
 683                   atomic_read(&imp->imp_inval_count));
 684        spin_unlock(&imp->imp_lock);
 685
 686        if (!obd->obd_svc_stats)
 687                goto out_climp;
 688
 689        header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
 690        lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
 691        if (ret.lc_count != 0) {
 692                /* first argument to do_div MUST be __u64 */
 693                __u64 sum = ret.lc_sum;
 694
 695                do_div(sum, ret.lc_count);
 696                ret.lc_sum = sum;
 697        } else
 698                ret.lc_sum = 0;
 699        seq_printf(m,
 700                   "    rpcs:\n"
 701                   "       inflight: %u\n"
 702                   "       unregistering: %u\n"
 703                   "       timeouts: %u\n"
 704                   "       avg_waittime: %llu %s\n",
 705                   atomic_read(&imp->imp_inflight),
 706                   atomic_read(&imp->imp_unregistering),
 707                   atomic_read(&imp->imp_timeouts),
 708                   ret.lc_sum, header->lc_units);
 709
 710        k = 0;
 711        for (j = 0; j < IMP_AT_MAX_PORTALS; j++) {
 712                if (imp->imp_at.iat_portal[j] == 0)
 713                        break;
 714                k = max_t(unsigned int, k,
 715                          at_get(&imp->imp_at.iat_service_estimate[j]));
 716        }
 717        seq_printf(m,
 718                   "    service_estimates:\n"
 719                   "       services: %u sec\n"
 720                   "       network: %u sec\n",
 721                   k,
 722                   at_get(&imp->imp_at.iat_net_latency));
 723
 724        seq_printf(m,
 725                   "    transactions:\n"
 726                   "       last_replay: %llu\n"
 727                   "       peer_committed: %llu\n"
 728                   "       last_checked: %llu\n",
 729                   imp->imp_last_replay_transno,
 730                   imp->imp_peer_committed_transno,
 731                   imp->imp_last_transno_checked);
 732
 733        /* avg data rates */
 734        for (rw = 0; rw <= 1; rw++) {
 735                lprocfs_stats_collect(obd->obd_svc_stats,
 736                                      PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
 737                                      &ret);
 738                if (ret.lc_sum > 0 && ret.lc_count > 0) {
 739                        /* first argument to do_div MUST be __u64 */
 740                        __u64 sum = ret.lc_sum;
 741
 742                        do_div(sum, ret.lc_count);
 743                        ret.lc_sum = sum;
 744                        seq_printf(m,
 745                                   "    %s_data_averages:\n"
 746                                   "       bytes_per_rpc: %llu\n",
 747                                   rw ? "write" : "read",
 748                                   ret.lc_sum);
 749                }
 750                k = (int)ret.lc_sum;
 751                j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
 752                header = &obd->obd_svc_stats->ls_cnt_header[j];
 753                lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
 754                if (ret.lc_sum > 0 && ret.lc_count != 0) {
 755                        /* first argument to do_div MUST be __u64 */
 756                        __u64 sum = ret.lc_sum;
 757
 758                        do_div(sum, ret.lc_count);
 759                        ret.lc_sum = sum;
 760                        seq_printf(m,
 761                                   "       %s_per_rpc: %llu\n",
 762                                   header->lc_units, ret.lc_sum);
 763                        j = (int)ret.lc_sum;
 764                        if (j > 0)
 765                                seq_printf(m,
 766                                           "       MB_per_sec: %u.%.02u\n",
 767                                           k / j, (100 * k / j) % 100);
 768                }
 769        }
 770
 771out_climp:
 772        up_read(&obd->u.cli.cl_sem);
 773        return 0;
 774}
 775EXPORT_SYMBOL(lprocfs_rd_import);
 776
 777int lprocfs_rd_state(struct seq_file *m, void *data)
 778{
 779        struct obd_device *obd = data;
 780        struct obd_import *imp;
 781        int j, k, rc;
 782
 783        LASSERT(obd);
 784        rc = lprocfs_climp_check(obd);
 785        if (rc)
 786                return rc;
 787
 788        imp = obd->u.cli.cl_import;
 789
 790        seq_printf(m, "current_state: %s\n",
 791                   ptlrpc_import_state_name(imp->imp_state));
 792        seq_printf(m, "state_history:\n");
 793        k = imp->imp_state_hist_idx;
 794        for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
 795                struct import_state_hist *ish =
 796                        &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
 797                if (ish->ish_state == 0)
 798                        continue;
 799                seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
 800                           ptlrpc_import_state_name(ish->ish_state));
 801        }
 802
 803        up_read(&obd->u.cli.cl_sem);
 804        return 0;
 805}
 806EXPORT_SYMBOL(lprocfs_rd_state);
 807
 808int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
 809{
 810        int i;
 811
 812        for (i = 0; i < AT_BINS; i++)
 813                seq_printf(m, "%3u ", at->at_hist[i]);
 814        seq_printf(m, "\n");
 815        return 0;
 816}
 817EXPORT_SYMBOL(lprocfs_at_hist_helper);
 818
 819/* See also ptlrpc_lprocfs_rd_timeouts */
 820int lprocfs_rd_timeouts(struct seq_file *m, void *data)
 821{
 822        struct obd_device *obd = data;
 823        struct obd_import *imp;
 824        unsigned int cur, worst;
 825        time64_t now, worstt;
 826        struct dhms ts;
 827        int i, rc;
 828
 829        LASSERT(obd);
 830        rc = lprocfs_climp_check(obd);
 831        if (rc)
 832                return rc;
 833
 834        imp = obd->u.cli.cl_import;
 835
 836        now = ktime_get_real_seconds();
 837
 838        /* Some network health info for kicks */
 839        s2dhms(&ts, now - imp->imp_last_reply_time);
 840        seq_printf(m, "%-10s : %lld, " DHMS_FMT " ago\n",
 841                   "last reply", (s64)imp->imp_last_reply_time, DHMS_VARS(&ts));
 842
 843        cur = at_get(&imp->imp_at.iat_net_latency);
 844        worst = imp->imp_at.iat_net_latency.at_worst_ever;
 845        worstt = imp->imp_at.iat_net_latency.at_worst_time;
 846        s2dhms(&ts, now - worstt);
 847        seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, " DHMS_FMT " ago) ",
 848                   "network", cur, worst, (s64)worstt, DHMS_VARS(&ts));
 849        lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
 850
 851        for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
 852                if (imp->imp_at.iat_portal[i] == 0)
 853                        break;
 854                cur = at_get(&imp->imp_at.iat_service_estimate[i]);
 855                worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
 856                worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
 857                s2dhms(&ts, now - worstt);
 858                seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, "
 859                           DHMS_FMT " ago) ", imp->imp_at.iat_portal[i],
 860                           cur, worst, (s64)worstt, DHMS_VARS(&ts));
 861                lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
 862        }
 863
 864        up_read(&obd->u.cli.cl_sem);
 865        return 0;
 866}
 867EXPORT_SYMBOL(lprocfs_rd_timeouts);
 868
 869int lprocfs_rd_connect_flags(struct seq_file *m, void *data)
 870{
 871        struct obd_device *obd = data;
 872        __u64 flags;
 873        int rc;
 874
 875        rc = lprocfs_climp_check(obd);
 876        if (rc)
 877                return rc;
 878
 879        flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
 880        seq_printf(m, "flags=%#llx\n", flags);
 881        obd_connect_seq_flags2str(m, flags, "\n");
 882        seq_printf(m, "\n");
 883        up_read(&obd->u.cli.cl_sem);
 884        return 0;
 885}
 886EXPORT_SYMBOL(lprocfs_rd_connect_flags);
 887
 888static struct attribute *obd_def_attrs[] = {
 889        &lustre_attr_blocksize.attr,
 890        &lustre_attr_kbytestotal.attr,
 891        &lustre_attr_kbytesfree.attr,
 892        &lustre_attr_kbytesavail.attr,
 893        &lustre_attr_filestotal.attr,
 894        &lustre_attr_filesfree.attr,
 895        &lustre_attr_uuid.attr,
 896        NULL,
 897};
 898
 899static void obd_sysfs_release(struct kobject *kobj)
 900{
 901        struct obd_device *obd = container_of(kobj, struct obd_device,
 902                                              obd_kobj);
 903
 904        complete(&obd->obd_kobj_unregister);
 905}
 906
 907static struct kobj_type obd_ktype = {
 908        .default_attrs  = obd_def_attrs,
 909        .sysfs_ops      = &lustre_sysfs_ops,
 910        .release        = obd_sysfs_release,
 911};
 912
 913int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list,
 914                      struct attribute_group *attrs)
 915{
 916        int rc = 0;
 917
 918        init_completion(&obd->obd_kobj_unregister);
 919        rc = kobject_init_and_add(&obd->obd_kobj, &obd_ktype,
 920                                  obd->obd_type->typ_kobj,
 921                                  "%s", obd->obd_name);
 922        if (rc)
 923                return rc;
 924
 925        if (attrs) {
 926                rc = sysfs_create_group(&obd->obd_kobj, attrs);
 927                if (rc) {
 928                        kobject_put(&obd->obd_kobj);
 929                        return rc;
 930                }
 931        }
 932
 933        obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
 934                                                   obd->obd_type->typ_debugfs_entry,
 935                                                   list, obd);
 936        if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
 937                rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
 938                                            : -ENOMEM;
 939                CERROR("error %d setting up lprocfs for %s\n",
 940                       rc, obd->obd_name);
 941                obd->obd_debugfs_entry = NULL;
 942        }
 943
 944        return rc;
 945}
 946EXPORT_SYMBOL_GPL(lprocfs_obd_setup);
 947
 948int lprocfs_obd_cleanup(struct obd_device *obd)
 949{
 950        if (!obd)
 951                return -EINVAL;
 952
 953        if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
 954                ldebugfs_remove(&obd->obd_debugfs_entry);
 955
 956        kobject_put(&obd->obd_kobj);
 957        wait_for_completion(&obd->obd_kobj_unregister);
 958
 959        return 0;
 960}
 961EXPORT_SYMBOL_GPL(lprocfs_obd_cleanup);
 962
 963int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
 964{
 965        struct lprocfs_counter  *cntr;
 966        unsigned int            percpusize;
 967        int                     rc = -ENOMEM;
 968        unsigned long           flags = 0;
 969        int                     i;
 970
 971        LASSERT(!stats->ls_percpu[cpuid]);
 972        LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
 973
 974        percpusize = lprocfs_stats_counter_size(stats);
 975        LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
 976        if (stats->ls_percpu[cpuid]) {
 977                rc = 0;
 978                if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
 979                        if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
 980                                spin_lock_irqsave(&stats->ls_lock, flags);
 981                        else
 982                                spin_lock(&stats->ls_lock);
 983                        if (stats->ls_biggest_alloc_num <= cpuid)
 984                                stats->ls_biggest_alloc_num = cpuid + 1;
 985                        if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
 986                                spin_unlock_irqrestore(&stats->ls_lock, flags);
 987                        else
 988                                spin_unlock(&stats->ls_lock);
 989                }
 990                /* initialize the ls_percpu[cpuid] non-zero counter */
 991                for (i = 0; i < stats->ls_num; ++i) {
 992                        cntr = lprocfs_stats_counter_get(stats, cpuid, i);
 993                        cntr->lc_min = LC_MIN_INIT;
 994                }
 995        }
 996        return rc;
 997}
 998EXPORT_SYMBOL(lprocfs_stats_alloc_one);
 999
1000struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1001                                          enum lprocfs_stats_flags flags)
1002{
1003        struct lprocfs_stats    *stats;
1004        unsigned int            num_entry;
1005        unsigned int            percpusize = 0;
1006        int                     i;
1007
1008        if (num == 0)
1009                return NULL;
1010
1011        if (lprocfs_no_percpu_stats != 0)
1012                flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1013
1014        if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1015                num_entry = 1;
1016        else
1017                num_entry = num_possible_cpus();
1018
1019        /* alloc percpu pointers for all possible cpu slots */
1020        LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1021        if (!stats)
1022                return NULL;
1023
1024        stats->ls_num = num;
1025        stats->ls_flags = flags;
1026        spin_lock_init(&stats->ls_lock);
1027
1028        /* alloc num of counter headers */
1029        LIBCFS_ALLOC(stats->ls_cnt_header,
1030                     stats->ls_num * sizeof(struct lprocfs_counter_header));
1031        if (!stats->ls_cnt_header)
1032                goto fail;
1033
1034        if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1035                /* contains only one set counters */
1036                percpusize = lprocfs_stats_counter_size(stats);
1037                LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1038                if (!stats->ls_percpu[0])
1039                        goto fail;
1040                stats->ls_biggest_alloc_num = 1;
1041        } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1042                /* alloc all percpu data */
1043                for (i = 0; i < num_entry; ++i)
1044                        if (lprocfs_stats_alloc_one(stats, i) < 0)
1045                                goto fail;
1046        }
1047
1048        return stats;
1049
1050fail:
1051        lprocfs_free_stats(&stats);
1052        return NULL;
1053}
1054EXPORT_SYMBOL(lprocfs_alloc_stats);
1055
1056void lprocfs_free_stats(struct lprocfs_stats **statsh)
1057{
1058        struct lprocfs_stats *stats = *statsh;
1059        unsigned int num_entry;
1060        unsigned int percpusize;
1061        unsigned int i;
1062
1063        if (!stats || stats->ls_num == 0)
1064                return;
1065        *statsh = NULL;
1066
1067        if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1068                num_entry = 1;
1069        else
1070                num_entry = num_possible_cpus();
1071
1072        percpusize = lprocfs_stats_counter_size(stats);
1073        for (i = 0; i < num_entry; i++)
1074                if (stats->ls_percpu[i])
1075                        LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1076        if (stats->ls_cnt_header)
1077                LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1078                                        sizeof(struct lprocfs_counter_header));
1079        LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1080}
1081EXPORT_SYMBOL(lprocfs_free_stats);
1082
1083void lprocfs_clear_stats(struct lprocfs_stats *stats)
1084{
1085        struct lprocfs_counter          *percpu_cntr;
1086        int                             i;
1087        int                             j;
1088        unsigned int                    num_entry;
1089        unsigned long                   flags = 0;
1090
1091        num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1092
1093        for (i = 0; i < num_entry; i++) {
1094                if (!stats->ls_percpu[i])
1095                        continue;
1096                for (j = 0; j < stats->ls_num; j++) {
1097                        percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1098                        percpu_cntr->lc_count           = 0;
1099                        percpu_cntr->lc_min             = LC_MIN_INIT;
1100                        percpu_cntr->lc_max             = 0;
1101                        percpu_cntr->lc_sumsquare       = 0;
1102                        percpu_cntr->lc_sum             = 0;
1103                        if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1104                                percpu_cntr->lc_sum_irq = 0;
1105                }
1106        }
1107
1108        lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1109}
1110EXPORT_SYMBOL(lprocfs_clear_stats);
1111
1112static ssize_t lprocfs_stats_seq_write(struct file *file,
1113                                       const char __user *buf,
1114                                       size_t len, loff_t *off)
1115{
1116        struct seq_file *seq = file->private_data;
1117        struct lprocfs_stats *stats = seq->private;
1118
1119        lprocfs_clear_stats(stats);
1120
1121        return len;
1122}
1123
1124static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1125{
1126        struct lprocfs_stats *stats = p->private;
1127
1128        return (*pos < stats->ls_num) ? pos : NULL;
1129}
1130
1131static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1132{
1133}
1134
1135static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1136{
1137        (*pos)++;
1138        return lprocfs_stats_seq_start(p, pos);
1139}
1140
1141/* seq file export of one lprocfs counter */
1142static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1143{
1144        struct lprocfs_stats            *stats  = p->private;
1145        struct lprocfs_counter_header   *hdr;
1146        struct lprocfs_counter           ctr;
1147        int                              idx    = *(loff_t *)v;
1148
1149        if (idx == 0) {
1150                struct timespec64 now;
1151
1152                ktime_get_real_ts64(&now);
1153                seq_printf(p, "%-25s %llu.%9lu secs.usecs\n",
1154                           "snapshot_time",
1155                           (s64)now.tv_sec, (unsigned long)now.tv_nsec);
1156        }
1157
1158        hdr = &stats->ls_cnt_header[idx];
1159        lprocfs_stats_collect(stats, idx, &ctr);
1160
1161        if (ctr.lc_count != 0) {
1162                seq_printf(p, "%-25s %lld samples [%s]",
1163                           hdr->lc_name, ctr.lc_count, hdr->lc_units);
1164
1165                if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) &&
1166                    (ctr.lc_count > 0)) {
1167                        seq_printf(p, " %lld %lld %lld",
1168                                   ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1169                        if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1170                                seq_printf(p, " %lld", ctr.lc_sumsquare);
1171                }
1172                seq_putc(p, '\n');
1173        }
1174
1175        return 0;
1176}
1177
1178static const struct seq_operations lprocfs_stats_seq_sops = {
1179        .start  = lprocfs_stats_seq_start,
1180        .stop   = lprocfs_stats_seq_stop,
1181        .next   = lprocfs_stats_seq_next,
1182        .show   = lprocfs_stats_seq_show,
1183};
1184
1185static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1186{
1187        struct seq_file *seq;
1188        int rc;
1189
1190        rc = seq_open(file, &lprocfs_stats_seq_sops);
1191        if (rc)
1192                return rc;
1193
1194        seq = file->private_data;
1195        seq->private = inode->i_private;
1196
1197        return 0;
1198}
1199
1200static const struct file_operations lprocfs_stats_seq_fops = {
1201        .owner   = THIS_MODULE,
1202        .open    = lprocfs_stats_seq_open,
1203        .read    = seq_read,
1204        .write   = lprocfs_stats_seq_write,
1205        .llseek  = seq_lseek,
1206        .release = lprocfs_seq_release,
1207};
1208
1209int ldebugfs_register_stats(struct dentry *parent, const char *name,
1210                            struct lprocfs_stats *stats)
1211{
1212        struct dentry *entry;
1213
1214        LASSERT(!IS_ERR_OR_NULL(parent));
1215
1216        entry = debugfs_create_file(name, 0644, parent, stats,
1217                                    &lprocfs_stats_seq_fops);
1218        if (IS_ERR_OR_NULL(entry))
1219                return entry ? PTR_ERR(entry) : -ENOMEM;
1220
1221        return 0;
1222}
1223EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1224
1225void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1226                          unsigned conf, const char *name, const char *units)
1227{
1228        struct lprocfs_counter_header   *header;
1229        struct lprocfs_counter          *percpu_cntr;
1230        unsigned long                   flags = 0;
1231        unsigned int                    i;
1232        unsigned int                    num_cpu;
1233
1234        header = &stats->ls_cnt_header[index];
1235        LASSERTF(header, "Failed to allocate stats header:[%d]%s/%s\n",
1236                 index, name, units);
1237
1238        header->lc_config = conf;
1239        header->lc_name   = name;
1240        header->lc_units  = units;
1241
1242        num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1243        for (i = 0; i < num_cpu; ++i) {
1244                if (!stats->ls_percpu[i])
1245                        continue;
1246                percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1247                percpu_cntr->lc_count           = 0;
1248                percpu_cntr->lc_min             = LC_MIN_INIT;
1249                percpu_cntr->lc_max             = 0;
1250                percpu_cntr->lc_sumsquare       = 0;
1251                percpu_cntr->lc_sum             = 0;
1252                if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1253                        percpu_cntr->lc_sum_irq = 0;
1254        }
1255        lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1256}
1257EXPORT_SYMBOL(lprocfs_counter_init);
1258
1259int lprocfs_exp_cleanup(struct obd_export *exp)
1260{
1261        return 0;
1262}
1263EXPORT_SYMBOL(lprocfs_exp_cleanup);
1264
1265__s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1266                          struct lprocfs_counter_header *header,
1267                          enum lprocfs_stats_flags flags,
1268                          enum lprocfs_fields_flags field)
1269{
1270        __s64 ret = 0;
1271
1272        if (!lc || !header)
1273                return 0;
1274
1275        switch (field) {
1276        case LPROCFS_FIELDS_FLAGS_CONFIG:
1277                ret = header->lc_config;
1278                break;
1279        case LPROCFS_FIELDS_FLAGS_SUM:
1280                ret = lc->lc_sum;
1281                if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1282                        ret += lc->lc_sum_irq;
1283                break;
1284        case LPROCFS_FIELDS_FLAGS_MIN:
1285                ret = lc->lc_min;
1286                break;
1287        case LPROCFS_FIELDS_FLAGS_MAX:
1288                ret = lc->lc_max;
1289                break;
1290        case LPROCFS_FIELDS_FLAGS_AVG:
1291                ret = (lc->lc_max - lc->lc_min) / 2;
1292                break;
1293        case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1294                ret = lc->lc_sumsquare;
1295                break;
1296        case LPROCFS_FIELDS_FLAGS_COUNT:
1297                ret = lc->lc_count;
1298                break;
1299        default:
1300                break;
1301        }
1302
1303        return 0;
1304}
1305EXPORT_SYMBOL(lprocfs_read_helper);
1306
1307int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1308                         int *val)
1309{
1310        return lprocfs_write_frac_helper(buffer, count, val, 1);
1311}
1312EXPORT_SYMBOL(lprocfs_write_helper);
1313
1314int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1315                             __u64 *val)
1316{
1317        return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1318}
1319EXPORT_SYMBOL(lprocfs_write_u64_helper);
1320
1321int lprocfs_write_frac_u64_helper(const char __user *buffer,
1322                                  unsigned long count, __u64 *val, int mult)
1323{
1324        char kernbuf[22], *end, *pbuf;
1325        __u64 whole, frac = 0, units;
1326        unsigned frac_d = 1;
1327        int sign = 1;
1328
1329        if (count > (sizeof(kernbuf) - 1))
1330                return -EINVAL;
1331
1332        if (copy_from_user(kernbuf, buffer, count))
1333                return -EFAULT;
1334
1335        kernbuf[count] = '\0';
1336        pbuf = kernbuf;
1337        if (*pbuf == '-') {
1338                sign = -1;
1339                pbuf++;
1340        }
1341
1342        whole = simple_strtoull(pbuf, &end, 10);
1343        if (pbuf == end)
1344                return -EINVAL;
1345
1346        if (*end == '.') {
1347                int i;
1348
1349                pbuf = end + 1;
1350
1351                /* need to limit frac_d to a __u32 */
1352                if (strlen(pbuf) > 10)
1353                        pbuf[10] = '\0';
1354
1355                frac = simple_strtoull(pbuf, &end, 10);
1356                /* count decimal places */
1357                for (i = 0; i < (end - pbuf); i++)
1358                        frac_d *= 10;
1359        }
1360
1361        units = 1;
1362        if (end) {
1363                switch (tolower(*end)) {
1364                case 'p':
1365                        units <<= 10;
1366                case 't':
1367                        units <<= 10;
1368                case 'g':
1369                        units <<= 10;
1370                case 'm':
1371                        units <<= 10;
1372                case 'k':
1373                        units <<= 10;
1374                }
1375        }
1376        /* Specified units override the multiplier */
1377        if (units > 1)
1378                mult = units;
1379
1380        frac *= mult;
1381        do_div(frac, frac_d);
1382        *val = sign * (whole * mult + frac);
1383        return 0;
1384}
1385EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1386
1387static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1388{
1389        size_t l2;
1390
1391        l2 = strlen(s2);
1392        if (!l2)
1393                return (char *)s1;
1394        while (len >= l2) {
1395                len--;
1396                if (!memcmp(s1, s2, l2))
1397                        return (char *)s1;
1398                s1++;
1399        }
1400        return NULL;
1401}
1402
1403/**
1404 * Find the string \a name in the input \a buffer, and return a pointer to the
1405 * value immediately following \a name, reducing \a count appropriately.
1406 * If \a name is not found the original \a buffer is returned.
1407 */
1408char *lprocfs_find_named_value(const char *buffer, const char *name,
1409                               size_t *count)
1410{
1411        char *val;
1412        size_t buflen = *count;
1413
1414        /* there is no strnstr() in rhel5 and ubuntu kernels */
1415        val = lprocfs_strnstr(buffer, name, buflen);
1416        if (!val)
1417                return (char *)buffer;
1418
1419        val += strlen(name);                         /* skip prefix */
1420        while (val < buffer + buflen && isspace(*val)) /* skip separator */
1421                val++;
1422
1423        *count = 0;
1424        while (val < buffer + buflen && isalnum(*val)) {
1425                ++*count;
1426                ++val;
1427        }
1428
1429        return val - *count;
1430}
1431EXPORT_SYMBOL(lprocfs_find_named_value);
1432
1433int ldebugfs_seq_create(struct dentry *parent, const char *name,
1434                        umode_t mode, const struct file_operations *seq_fops,
1435                        void *data)
1436{
1437        struct dentry *entry;
1438
1439        /* Disallow secretly (un)writable entries. */
1440        LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1441
1442        entry = debugfs_create_file(name, mode, parent, data, seq_fops);
1443        if (IS_ERR_OR_NULL(entry))
1444                return entry ? PTR_ERR(entry) : -ENOMEM;
1445
1446        return 0;
1447}
1448EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
1449
1450int ldebugfs_obd_seq_create(struct obd_device *dev,
1451                            const char *name,
1452                            umode_t mode,
1453                            const struct file_operations *seq_fops,
1454                            void *data)
1455{
1456        return ldebugfs_seq_create(dev->obd_debugfs_entry, name,
1457                                   mode, seq_fops, data);
1458}
1459EXPORT_SYMBOL_GPL(ldebugfs_obd_seq_create);
1460
1461void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1462{
1463        if (value >= OBD_HIST_MAX)
1464                value = OBD_HIST_MAX - 1;
1465
1466        spin_lock(&oh->oh_lock);
1467        oh->oh_buckets[value]++;
1468        spin_unlock(&oh->oh_lock);
1469}
1470EXPORT_SYMBOL(lprocfs_oh_tally);
1471
1472void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1473{
1474        unsigned int val;
1475
1476        for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
1477                ;
1478
1479        lprocfs_oh_tally(oh, val);
1480}
1481EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1482
1483unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1484{
1485        unsigned long ret = 0;
1486        int i;
1487
1488        for (i = 0; i < OBD_HIST_MAX; i++)
1489                ret +=  oh->oh_buckets[i];
1490        return ret;
1491}
1492EXPORT_SYMBOL(lprocfs_oh_sum);
1493
1494void lprocfs_oh_clear(struct obd_histogram *oh)
1495{
1496        spin_lock(&oh->oh_lock);
1497        memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1498        spin_unlock(&oh->oh_lock);
1499}
1500EXPORT_SYMBOL(lprocfs_oh_clear);
1501
1502static ssize_t lustre_attr_show(struct kobject *kobj,
1503                                struct attribute *attr, char *buf)
1504{
1505        struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1506
1507        return a->show ? a->show(kobj, attr, buf) : 0;
1508}
1509
1510static ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1511                                 const char *buf, size_t len)
1512{
1513        struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1514
1515        return a->store ? a->store(kobj, attr, buf, len) : len;
1516}
1517
1518const struct sysfs_ops lustre_sysfs_ops = {
1519        .show  = lustre_attr_show,
1520        .store = lustre_attr_store,
1521};
1522EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1523