drivers: staging: lustre: Use mult if units not specified
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / lustre / obdclass / lprocfs_status.c
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, 2012, 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
44 #include "../include/obd_class.h"
45 #include "../include/lprocfs_status.h"
46 #include "../include/lustre/lustre_idl.h"
47 #include <linux/seq_file.h>
48
49 static 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
107 int 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] != NULL; 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 }
123 EXPORT_SYMBOL(obd_connect_flags2str);
124
125 int 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 > /proc/xxx       output result : x
153                  *      2. #echo x.0x > /proc/xxx       output result : x.0x
154                  *      3. #echo x.x0 > /proc/xxx       output result : x.x
155                  *      4. #echo x.xx > /proc/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 }
178 EXPORT_SYMBOL(lprocfs_read_frac_helper);
179
180 int 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 != NULL && *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 }
221 EXPORT_SYMBOL(lprocfs_write_frac_helper);
222
223 #if defined (CONFIG_PROC_FS)
224
225 static int lprocfs_no_percpu_stats = 0;
226 module_param(lprocfs_no_percpu_stats, int, 0644);
227 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
228
229 #define MAX_STRING_SIZE 128
230
231 int lprocfs_single_release(struct inode *inode, struct file *file)
232 {
233         return single_release(inode, file);
234 }
235 EXPORT_SYMBOL(lprocfs_single_release);
236
237 int lprocfs_seq_release(struct inode *inode, struct file *file)
238 {
239         return seq_release(inode, file);
240 }
241 EXPORT_SYMBOL(lprocfs_seq_release);
242
243 /* lprocfs API calls */
244
245 struct proc_dir_entry *lprocfs_add_simple(struct proc_dir_entry *root,
246                                      char *name, void *data,
247                                      struct file_operations *fops)
248 {
249         struct proc_dir_entry *proc;
250         umode_t mode = 0;
251
252         if (root == NULL || name == NULL || fops == NULL)
253                 return ERR_PTR(-EINVAL);
254
255         if (fops->read)
256                 mode = 0444;
257         if (fops->write)
258                 mode |= 0200;
259         proc = proc_create_data(name, mode, root, fops, data);
260         if (!proc) {
261                 CERROR("LprocFS: No memory to create /proc entry %s", name);
262                 return ERR_PTR(-ENOMEM);
263         }
264         return proc;
265 }
266 EXPORT_SYMBOL(lprocfs_add_simple);
267
268 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
269                         struct proc_dir_entry *parent, const char *format, ...)
270 {
271         struct proc_dir_entry *entry;
272         char *dest;
273         va_list ap;
274
275         if (parent == NULL || format == NULL)
276                 return NULL;
277
278         OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
279         if (dest == NULL)
280                 return NULL;
281
282         va_start(ap, format);
283         vsnprintf(dest, MAX_STRING_SIZE, format, ap);
284         va_end(ap);
285
286         entry = proc_symlink(name, parent, dest);
287         if (entry == NULL)
288                 CERROR("LprocFS: Could not create symbolic link from %s to %s",
289                         name, dest);
290
291         OBD_FREE(dest, MAX_STRING_SIZE + 1);
292         return entry;
293 }
294 EXPORT_SYMBOL(lprocfs_add_symlink);
295
296 static struct file_operations lprocfs_generic_fops = { };
297
298 /**
299  * Add /proc entries.
300  *
301  * \param root [in]  The parent proc entry on which new entry will be added.
302  * \param list [in]  Array of proc entries to be added.
303  * \param data [in]  The argument to be passed when entries read/write routines
304  *                 are called through /proc file.
305  *
306  * \retval 0   on success
307  *       < 0 on error
308  */
309 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
310                      void *data)
311 {
312         if (root == NULL || list == NULL)
313                 return -EINVAL;
314
315         while (list->name != NULL) {
316                 struct proc_dir_entry *proc;
317                 umode_t mode = 0;
318
319                 if (list->proc_mode != 0000) {
320                         mode = list->proc_mode;
321                 } else if (list->fops) {
322                         if (list->fops->read)
323                                 mode = 0444;
324                         if (list->fops->write)
325                                 mode |= 0200;
326                 }
327                 proc = proc_create_data(list->name, mode, root,
328                                         list->fops ?: &lprocfs_generic_fops,
329                                         list->data ?: data);
330                 if (proc == NULL)
331                         return -ENOMEM;
332                 list++;
333         }
334         return 0;
335 }
336 EXPORT_SYMBOL(lprocfs_add_vars);
337
338 void lprocfs_remove(struct proc_dir_entry **rooth)
339 {
340         proc_remove(*rooth);
341         *rooth = NULL;
342 }
343 EXPORT_SYMBOL(lprocfs_remove);
344
345 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
346 {
347         LASSERT(parent != NULL);
348         remove_proc_entry(name, parent);
349 }
350 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
351
352 struct proc_dir_entry *lprocfs_register(const char *name,
353                                         struct proc_dir_entry *parent,
354                                         struct lprocfs_vars *list, void *data)
355 {
356         struct proc_dir_entry *entry;
357
358         entry = proc_mkdir(name, parent);
359         if (entry == NULL) {
360                 entry = ERR_PTR(-ENOMEM);
361                 goto out;
362         }
363
364         if (list != NULL) {
365                 int rc = lprocfs_add_vars(entry, list, data);
366                 if (rc != 0) {
367                         lprocfs_remove(&entry);
368                         entry = ERR_PTR(rc);
369                 }
370         }
371 out:
372         return entry;
373 }
374 EXPORT_SYMBOL(lprocfs_register);
375
376 /* Generic callbacks */
377 int lprocfs_rd_uint(struct seq_file *m, void *data)
378 {
379         return seq_printf(m, "%u\n", *(unsigned int *)data);
380 }
381 EXPORT_SYMBOL(lprocfs_rd_uint);
382
383 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
384                     unsigned long count, void *data)
385 {
386         unsigned *p = data;
387         char dummy[MAX_STRING_SIZE + 1], *end;
388         unsigned long tmp;
389
390         dummy[MAX_STRING_SIZE] = '\0';
391         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
392                 return -EFAULT;
393
394         tmp = simple_strtoul(dummy, &end, 0);
395         if (dummy == end)
396                 return -EINVAL;
397
398         *p = (unsigned int)tmp;
399         return count;
400 }
401 EXPORT_SYMBOL(lprocfs_wr_uint);
402
403 int lprocfs_rd_u64(struct seq_file *m, void *data)
404 {
405         return seq_printf(m, "%llu\n", *(__u64 *)data);
406 }
407 EXPORT_SYMBOL(lprocfs_rd_u64);
408
409 int lprocfs_rd_atomic(struct seq_file *m, void *data)
410 {
411         atomic_t *atom = data;
412         LASSERT(atom != NULL);
413         return seq_printf(m, "%d\n", atomic_read(atom));
414 }
415 EXPORT_SYMBOL(lprocfs_rd_atomic);
416
417 int lprocfs_wr_atomic(struct file *file, const char __user *buffer,
418                       unsigned long count, void *data)
419 {
420         atomic_t *atm = data;
421         int val = 0;
422         int rc;
423
424         rc = lprocfs_write_helper(buffer, count, &val);
425         if (rc < 0)
426                 return rc;
427
428         if (val <= 0)
429                 return -ERANGE;
430
431         atomic_set(atm, val);
432         return count;
433 }
434 EXPORT_SYMBOL(lprocfs_wr_atomic);
435
436 int lprocfs_rd_uuid(struct seq_file *m, void *data)
437 {
438         struct obd_device *obd = data;
439
440         LASSERT(obd != NULL);
441         return seq_printf(m, "%s\n", obd->obd_uuid.uuid);
442 }
443 EXPORT_SYMBOL(lprocfs_rd_uuid);
444
445 int lprocfs_rd_name(struct seq_file *m, void *data)
446 {
447         struct obd_device *dev = data;
448
449         LASSERT(dev != NULL);
450         return seq_printf(m, "%s\n", dev->obd_name);
451 }
452 EXPORT_SYMBOL(lprocfs_rd_name);
453
454 int lprocfs_rd_blksize(struct seq_file *m, void *data)
455 {
456         struct obd_device *obd = data;
457         struct obd_statfs  osfs;
458         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
459                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
460                             OBD_STATFS_NODELAY);
461         if (!rc)
462                 rc = seq_printf(m, "%u\n", osfs.os_bsize);
463         return rc;
464 }
465 EXPORT_SYMBOL(lprocfs_rd_blksize);
466
467 int lprocfs_rd_kbytestotal(struct seq_file *m, void *data)
468 {
469         struct obd_device *obd = data;
470         struct obd_statfs  osfs;
471         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
472                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
473                             OBD_STATFS_NODELAY);
474         if (!rc) {
475                 __u32 blk_size = osfs.os_bsize >> 10;
476                 __u64 result = osfs.os_blocks;
477
478                 while (blk_size >>= 1)
479                         result <<= 1;
480
481                 rc = seq_printf(m, "%llu\n", result);
482         }
483         return rc;
484 }
485 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
486
487 int lprocfs_rd_kbytesfree(struct seq_file *m, void *data)
488 {
489         struct obd_device *obd = data;
490         struct obd_statfs  osfs;
491         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
492                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
493                             OBD_STATFS_NODELAY);
494         if (!rc) {
495                 __u32 blk_size = osfs.os_bsize >> 10;
496                 __u64 result = osfs.os_bfree;
497
498                 while (blk_size >>= 1)
499                         result <<= 1;
500
501                 rc = seq_printf(m, "%llu\n", result);
502         }
503         return rc;
504 }
505 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
506
507 int lprocfs_rd_kbytesavail(struct seq_file *m, void *data)
508 {
509         struct obd_device *obd = data;
510         struct obd_statfs  osfs;
511         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
512                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
513                             OBD_STATFS_NODELAY);
514         if (!rc) {
515                 __u32 blk_size = osfs.os_bsize >> 10;
516                 __u64 result = osfs.os_bavail;
517
518                 while (blk_size >>= 1)
519                         result <<= 1;
520
521                 rc = seq_printf(m, "%llu\n", result);
522         }
523         return rc;
524 }
525 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
526
527 int lprocfs_rd_filestotal(struct seq_file *m, void *data)
528 {
529         struct obd_device *obd = data;
530         struct obd_statfs  osfs;
531         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
532                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
533                             OBD_STATFS_NODELAY);
534         if (!rc)
535                 rc = seq_printf(m, "%llu\n", osfs.os_files);
536
537         return rc;
538 }
539 EXPORT_SYMBOL(lprocfs_rd_filestotal);
540
541 int lprocfs_rd_filesfree(struct seq_file *m, void *data)
542 {
543         struct obd_device *obd = data;
544         struct obd_statfs  osfs;
545         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
546                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
547                             OBD_STATFS_NODELAY);
548         if (!rc)
549                 rc = seq_printf(m, "%llu\n", osfs.os_ffree);
550         return rc;
551 }
552 EXPORT_SYMBOL(lprocfs_rd_filesfree);
553
554 int lprocfs_rd_server_uuid(struct seq_file *m, void *data)
555 {
556         struct obd_device *obd = data;
557         struct obd_import *imp;
558         char *imp_state_name = NULL;
559         int rc = 0;
560
561         LASSERT(obd != NULL);
562         LPROCFS_CLIMP_CHECK(obd);
563         imp = obd->u.cli.cl_import;
564         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
565         rc = seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
566                         imp->imp_deactive ? "\tDEACTIVATED" : "");
567
568         LPROCFS_CLIMP_EXIT(obd);
569         return rc;
570 }
571 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
572
573 int lprocfs_rd_conn_uuid(struct seq_file *m, void *data)
574 {
575         struct obd_device *obd = data;
576         struct ptlrpc_connection *conn;
577         int rc = 0;
578
579         LASSERT(obd != NULL);
580
581         LPROCFS_CLIMP_CHECK(obd);
582         conn = obd->u.cli.cl_import->imp_connection;
583         if (conn && obd->u.cli.cl_import)
584                 rc = seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
585         else
586                 rc = seq_printf(m, "%s\n", "<none>");
587
588         LPROCFS_CLIMP_EXIT(obd);
589         return rc;
590 }
591 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
592
593 /** add up per-cpu counters */
594 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
595                            struct lprocfs_counter *cnt)
596 {
597         unsigned int                    num_entry;
598         struct lprocfs_counter          *percpu_cntr;
599         int                             i;
600         unsigned long                   flags = 0;
601
602         memset(cnt, 0, sizeof(*cnt));
603
604         if (stats == NULL) {
605                 /* set count to 1 to avoid divide-by-zero errs in callers */
606                 cnt->lc_count = 1;
607                 return;
608         }
609
610         cnt->lc_min = LC_MIN_INIT;
611
612         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
613
614         for (i = 0; i < num_entry; i++) {
615                 if (stats->ls_percpu[i] == NULL)
616                         continue;
617                 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
618
619                 cnt->lc_count += percpu_cntr->lc_count;
620                 cnt->lc_sum += percpu_cntr->lc_sum;
621                 if (percpu_cntr->lc_min < cnt->lc_min)
622                         cnt->lc_min = percpu_cntr->lc_min;
623                 if (percpu_cntr->lc_max > cnt->lc_max)
624                         cnt->lc_max = percpu_cntr->lc_max;
625                 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
626         }
627
628         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
629 }
630 EXPORT_SYMBOL(lprocfs_stats_collect);
631
632 /**
633  * Append a space separated list of current set flags to str.
634  */
635 #define flag2str(flag, first)                                           \
636         do {                                                            \
637                 if (imp->imp_##flag)                                    \
638                      seq_printf(m, "%s" #flag, first ? "" : ", ");      \
639         } while (0)
640 static int obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
641 {
642         bool first = true;
643
644         if (imp->imp_obd->obd_no_recov) {
645                 seq_printf(m, "no_recov");
646                 first = false;
647         }
648
649         flag2str(invalid, first);
650         first = false;
651         flag2str(deactive, first);
652         flag2str(replayable, first);
653         flag2str(pingable, first);
654         return 0;
655 }
656 #undef flags2str
657
658 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
659 {
660         __u64 mask = 1;
661         int i;
662         bool first = true;
663
664         for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
665                 if (flags & mask) {
666                         seq_printf(m, "%s%s",
667                                         first ? sep : "", obd_connect_names[i]);
668                         first = false;
669                 }
670         }
671         if (flags & ~(mask - 1))
672                 seq_printf(m, "%sunknown flags %#llx",
673                                 first ? sep : "", flags & ~(mask - 1));
674 }
675
676 int lprocfs_rd_import(struct seq_file *m, void *data)
677 {
678         struct lprocfs_counter          ret;
679         struct lprocfs_counter_header   *header;
680         struct obd_device               *obd    = (struct obd_device *)data;
681         struct obd_import               *imp;
682         struct obd_import_conn          *conn;
683         int                             j;
684         int                             k;
685         int                             rw      = 0;
686
687         LASSERT(obd != NULL);
688         LPROCFS_CLIMP_CHECK(obd);
689         imp = obd->u.cli.cl_import;
690
691         seq_printf(m,
692                      "import:\n"
693                      "    name: %s\n"
694                      "    target: %s\n"
695                      "    state: %s\n"
696                      "    instance: %u\n"
697                      "    connect_flags: [",
698                      obd->obd_name,
699                      obd2cli_tgt(obd),
700                      ptlrpc_import_state_name(imp->imp_state),
701                      imp->imp_connect_data.ocd_instance);
702         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags, ", ");
703         seq_printf(m,
704                       "]\n"
705                       "    import_flags: [");
706         obd_import_flags2str(imp, m);
707
708         seq_printf(m,
709                       "]\n"
710                       "    connection:\n"
711                       "       failover_nids: [");
712         spin_lock(&imp->imp_lock);
713         j = 0;
714         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
715                 seq_printf(m, "%s%s", j ? ", " : "",
716                            libcfs_nid2str(conn->oic_conn->c_peer.nid));
717                 j++;
718         }
719         seq_printf(m,
720                       "]\n"
721                       "       current_connection: %s\n"
722                       "       connection_attempts: %u\n"
723                       "       generation: %u\n"
724                       "       in-progress_invalidations: %u\n",
725                       imp->imp_connection == NULL ? "<none>" :
726                               libcfs_nid2str(imp->imp_connection->c_peer.nid),
727                       imp->imp_conn_cnt,
728                       imp->imp_generation,
729                       atomic_read(&imp->imp_inval_count));
730         spin_unlock(&imp->imp_lock);
731
732         if (obd->obd_svc_stats == NULL)
733                 goto out_climp;
734
735         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
736         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
737         if (ret.lc_count != 0) {
738                 /* first argument to do_div MUST be __u64 */
739                 __u64 sum = ret.lc_sum;
740                 do_div(sum, ret.lc_count);
741                 ret.lc_sum = sum;
742         } else
743                 ret.lc_sum = 0;
744         seq_printf(m,
745                       "    rpcs:\n"
746                       "       inflight: %u\n"
747                       "       unregistering: %u\n"
748                       "       timeouts: %u\n"
749                       "       avg_waittime: %llu %s\n",
750                       atomic_read(&imp->imp_inflight),
751                       atomic_read(&imp->imp_unregistering),
752                       atomic_read(&imp->imp_timeouts),
753                       ret.lc_sum, header->lc_units);
754
755         k = 0;
756         for (j = 0; j < IMP_AT_MAX_PORTALS; j++) {
757                 if (imp->imp_at.iat_portal[j] == 0)
758                         break;
759                 k = max_t(unsigned int, k,
760                           at_get(&imp->imp_at.iat_service_estimate[j]));
761         }
762         seq_printf(m,
763                       "    service_estimates:\n"
764                       "       services: %u sec\n"
765                       "       network: %u sec\n",
766                       k,
767                       at_get(&imp->imp_at.iat_net_latency));
768
769         seq_printf(m,
770                       "    transactions:\n"
771                       "       last_replay: %llu\n"
772                       "       peer_committed: %llu\n"
773                       "       last_checked: %llu\n",
774                       imp->imp_last_replay_transno,
775                       imp->imp_peer_committed_transno,
776                       imp->imp_last_transno_checked);
777
778         /* avg data rates */
779         for (rw = 0; rw <= 1; rw++) {
780                 lprocfs_stats_collect(obd->obd_svc_stats,
781                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
782                                       &ret);
783                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
784                         /* first argument to do_div MUST be __u64 */
785                         __u64 sum = ret.lc_sum;
786                         do_div(sum, ret.lc_count);
787                         ret.lc_sum = sum;
788                         seq_printf(m,
789                                       "    %s_data_averages:\n"
790                                       "       bytes_per_rpc: %llu\n",
791                                       rw ? "write" : "read",
792                                       ret.lc_sum);
793                 }
794                 k = (int)ret.lc_sum;
795                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
796                 header = &obd->obd_svc_stats->ls_cnt_header[j];
797                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
798                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
799                         /* first argument to do_div MUST be __u64 */
800                         __u64 sum = ret.lc_sum;
801                         do_div(sum, ret.lc_count);
802                         ret.lc_sum = sum;
803                         seq_printf(m,
804                                       "       %s_per_rpc: %llu\n",
805                                       header->lc_units, ret.lc_sum);
806                         j = (int)ret.lc_sum;
807                         if (j > 0)
808                                 seq_printf(m,
809                                               "       MB_per_sec: %u.%.02u\n",
810                                               k / j, (100 * k / j) % 100);
811                 }
812         }
813
814 out_climp:
815         LPROCFS_CLIMP_EXIT(obd);
816         return 0;
817 }
818 EXPORT_SYMBOL(lprocfs_rd_import);
819
820 int lprocfs_rd_state(struct seq_file *m, void *data)
821 {
822         struct obd_device *obd = (struct obd_device *)data;
823         struct obd_import *imp;
824         int j, k;
825
826         LASSERT(obd != NULL);
827         LPROCFS_CLIMP_CHECK(obd);
828         imp = obd->u.cli.cl_import;
829
830         seq_printf(m, "current_state: %s\n",
831                      ptlrpc_import_state_name(imp->imp_state));
832         seq_printf(m, "state_history:\n");
833         k = imp->imp_state_hist_idx;
834         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
835                 struct import_state_hist *ish =
836                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
837                 if (ish->ish_state == 0)
838                         continue;
839                 seq_printf(m, " - ["CFS_TIME_T", %s]\n",
840                               ish->ish_time,
841                               ptlrpc_import_state_name(ish->ish_state));
842         }
843
844         LPROCFS_CLIMP_EXIT(obd);
845         return 0;
846 }
847 EXPORT_SYMBOL(lprocfs_rd_state);
848
849 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
850 {
851         int i;
852         for (i = 0; i < AT_BINS; i++)
853                 seq_printf(m, "%3u ", at->at_hist[i]);
854         seq_printf(m, "\n");
855         return 0;
856 }
857 EXPORT_SYMBOL(lprocfs_at_hist_helper);
858
859 /* See also ptlrpc_lprocfs_rd_timeouts */
860 int lprocfs_rd_timeouts(struct seq_file *m, void *data)
861 {
862         struct obd_device *obd = (struct obd_device *)data;
863         struct obd_import *imp;
864         unsigned int cur, worst;
865         time_t now, worstt;
866         struct dhms ts;
867         int i;
868
869         LASSERT(obd != NULL);
870         LPROCFS_CLIMP_CHECK(obd);
871         imp = obd->u.cli.cl_import;
872
873         now = get_seconds();
874
875         /* Some network health info for kicks */
876         s2dhms(&ts, now - imp->imp_last_reply_time);
877         seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
878                        "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
879
880         cur = at_get(&imp->imp_at.iat_net_latency);
881         worst = imp->imp_at.iat_net_latency.at_worst_ever;
882         worstt = imp->imp_at.iat_net_latency.at_worst_time;
883         s2dhms(&ts, now - worstt);
884         seq_printf(m, "%-10s : cur %3u  worst %3u (at %ld, "DHMS_FMT" ago) ",
885                        "network", cur, worst, worstt, DHMS_VARS(&ts));
886         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
887
888         for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
889                 if (imp->imp_at.iat_portal[i] == 0)
890                         break;
891                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
892                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
893                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
894                 s2dhms(&ts, now - worstt);
895                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %ld, "
896                                DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
897                                cur, worst, worstt, DHMS_VARS(&ts));
898                 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
899         }
900
901         LPROCFS_CLIMP_EXIT(obd);
902         return 0;
903 }
904 EXPORT_SYMBOL(lprocfs_rd_timeouts);
905
906 int lprocfs_rd_connect_flags(struct seq_file *m, void *data)
907 {
908         struct obd_device *obd = data;
909         __u64 flags;
910
911         LPROCFS_CLIMP_CHECK(obd);
912         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
913         seq_printf(m, "flags=%#llx\n", flags);
914         obd_connect_seq_flags2str(m, flags, "\n");
915         seq_printf(m, "\n");
916         LPROCFS_CLIMP_EXIT(obd);
917         return 0;
918 }
919 EXPORT_SYMBOL(lprocfs_rd_connect_flags);
920
921 int lprocfs_rd_num_exports(struct seq_file *m, void *data)
922 {
923         struct obd_device *obd = data;
924
925         LASSERT(obd != NULL);
926         return seq_printf(m, "%u\n", obd->obd_num_exports);
927 }
928 EXPORT_SYMBOL(lprocfs_rd_num_exports);
929
930 int lprocfs_rd_numrefs(struct seq_file *m, void *data)
931 {
932         struct obd_type *class = (struct obd_type *) data;
933
934         LASSERT(class != NULL);
935         return seq_printf(m, "%d\n", class->typ_refcnt);
936 }
937 EXPORT_SYMBOL(lprocfs_rd_numrefs);
938
939 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list)
940 {
941         int rc = 0;
942
943         LASSERT(obd != NULL);
944         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
945         LASSERT(obd->obd_type->typ_procroot != NULL);
946
947         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
948                                                obd->obd_type->typ_procroot,
949                                                list, obd);
950         if (IS_ERR(obd->obd_proc_entry)) {
951                 rc = PTR_ERR(obd->obd_proc_entry);
952                 CERROR("error %d setting up lprocfs for %s\n",
953                        rc, obd->obd_name);
954                 obd->obd_proc_entry = NULL;
955         }
956         return rc;
957 }
958 EXPORT_SYMBOL(lprocfs_obd_setup);
959
960 int lprocfs_obd_cleanup(struct obd_device *obd)
961 {
962         if (!obd)
963                 return -EINVAL;
964         if (obd->obd_proc_exports_entry) {
965                 /* Should be no exports left */
966                 lprocfs_remove(&obd->obd_proc_exports_entry);
967                 obd->obd_proc_exports_entry = NULL;
968         }
969         if (obd->obd_proc_entry) {
970                 lprocfs_remove(&obd->obd_proc_entry);
971                 obd->obd_proc_entry = NULL;
972         }
973         return 0;
974 }
975 EXPORT_SYMBOL(lprocfs_obd_cleanup);
976
977 static void lprocfs_free_client_stats(struct nid_stat *client_stat)
978 {
979         CDEBUG(D_CONFIG, "stat %p - data %p/%p\n", client_stat,
980                client_stat->nid_proc, client_stat->nid_stats);
981
982         LASSERTF(atomic_read(&client_stat->nid_exp_ref_count) == 0,
983                  "nid %s:count %d\n", libcfs_nid2str(client_stat->nid),
984                  atomic_read(&client_stat->nid_exp_ref_count));
985
986         if (client_stat->nid_proc)
987                 lprocfs_remove(&client_stat->nid_proc);
988
989         if (client_stat->nid_stats)
990                 lprocfs_free_stats(&client_stat->nid_stats);
991
992         if (client_stat->nid_ldlm_stats)
993                 lprocfs_free_stats(&client_stat->nid_ldlm_stats);
994
995         OBD_FREE_PTR(client_stat);
996         return;
997
998 }
999
1000 void lprocfs_free_per_client_stats(struct obd_device *obd)
1001 {
1002         struct cfs_hash *hash = obd->obd_nid_stats_hash;
1003         struct nid_stat *stat;
1004
1005         /* we need extra list - because hash_exit called to early */
1006         /* not need locking because all clients is died */
1007         while (!list_empty(&obd->obd_nid_stats)) {
1008                 stat = list_entry(obd->obd_nid_stats.next,
1009                                       struct nid_stat, nid_list);
1010                 list_del_init(&stat->nid_list);
1011                 cfs_hash_del(hash, &stat->nid, &stat->nid_hash);
1012                 lprocfs_free_client_stats(stat);
1013         }
1014 }
1015 EXPORT_SYMBOL(lprocfs_free_per_client_stats);
1016
1017 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1018 {
1019         struct lprocfs_counter  *cntr;
1020         unsigned int            percpusize;
1021         int                     rc = -ENOMEM;
1022         unsigned long           flags = 0;
1023         int                     i;
1024
1025         LASSERT(stats->ls_percpu[cpuid] == NULL);
1026         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1027
1028         percpusize = lprocfs_stats_counter_size(stats);
1029         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1030         if (stats->ls_percpu[cpuid] != NULL) {
1031                 rc = 0;
1032                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1033                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1034                                 spin_lock_irqsave(&stats->ls_lock, flags);
1035                         else
1036                                 spin_lock(&stats->ls_lock);
1037                         if (stats->ls_biggest_alloc_num <= cpuid)
1038                                 stats->ls_biggest_alloc_num = cpuid + 1;
1039                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1040                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
1041                         else
1042                                 spin_unlock(&stats->ls_lock);
1043                 }
1044                 /* initialize the ls_percpu[cpuid] non-zero counter */
1045                 for (i = 0; i < stats->ls_num; ++i) {
1046                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1047                         cntr->lc_min = LC_MIN_INIT;
1048                 }
1049         }
1050         return rc;
1051 }
1052 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1053
1054 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1055                                           enum lprocfs_stats_flags flags)
1056 {
1057         struct lprocfs_stats    *stats;
1058         unsigned int            num_entry;
1059         unsigned int            percpusize = 0;
1060         int                     i;
1061
1062         if (num == 0)
1063                 return NULL;
1064
1065         if (lprocfs_no_percpu_stats != 0)
1066                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1067
1068         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1069                 num_entry = 1;
1070         else
1071                 num_entry = num_possible_cpus();
1072
1073         /* alloc percpu pointers for all possible cpu slots */
1074         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1075         if (stats == NULL)
1076                 return NULL;
1077
1078         stats->ls_num = num;
1079         stats->ls_flags = flags;
1080         spin_lock_init(&stats->ls_lock);
1081
1082         /* alloc num of counter headers */
1083         LIBCFS_ALLOC(stats->ls_cnt_header,
1084                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1085         if (stats->ls_cnt_header == NULL)
1086                 goto fail;
1087
1088         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1089                 /* contains only one set counters */
1090                 percpusize = lprocfs_stats_counter_size(stats);
1091                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1092                 if (stats->ls_percpu[0] == NULL)
1093                         goto fail;
1094                 stats->ls_biggest_alloc_num = 1;
1095         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1096                 /* alloc all percpu data, currently only obd_memory use this */
1097                 for (i = 0; i < num_entry; ++i)
1098                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1099                                 goto fail;
1100         }
1101
1102         return stats;
1103
1104 fail:
1105         lprocfs_free_stats(&stats);
1106         return NULL;
1107 }
1108 EXPORT_SYMBOL(lprocfs_alloc_stats);
1109
1110 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1111 {
1112         struct lprocfs_stats *stats = *statsh;
1113         unsigned int num_entry;
1114         unsigned int percpusize;
1115         unsigned int i;
1116
1117         if (stats == NULL || stats->ls_num == 0)
1118                 return;
1119         *statsh = NULL;
1120
1121         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1122                 num_entry = 1;
1123         else
1124                 num_entry = num_possible_cpus();
1125
1126         percpusize = lprocfs_stats_counter_size(stats);
1127         for (i = 0; i < num_entry; i++)
1128                 if (stats->ls_percpu[i] != NULL)
1129                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1130         if (stats->ls_cnt_header != NULL)
1131                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1132                                         sizeof(struct lprocfs_counter_header));
1133         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1134 }
1135 EXPORT_SYMBOL(lprocfs_free_stats);
1136
1137 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1138 {
1139         struct lprocfs_counter          *percpu_cntr;
1140         int                             i;
1141         int                             j;
1142         unsigned int                    num_entry;
1143         unsigned long                   flags = 0;
1144
1145         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1146
1147         for (i = 0; i < num_entry; i++) {
1148                 if (stats->ls_percpu[i] == NULL)
1149                         continue;
1150                 for (j = 0; j < stats->ls_num; j++) {
1151                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1152                         percpu_cntr->lc_count           = 0;
1153                         percpu_cntr->lc_min             = LC_MIN_INIT;
1154                         percpu_cntr->lc_max             = 0;
1155                         percpu_cntr->lc_sumsquare       = 0;
1156                         percpu_cntr->lc_sum             = 0;
1157                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1158                                 percpu_cntr->lc_sum_irq = 0;
1159                 }
1160         }
1161
1162         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1163 }
1164 EXPORT_SYMBOL(lprocfs_clear_stats);
1165
1166 static ssize_t lprocfs_stats_seq_write(struct file *file,
1167                                        const char __user *buf,
1168                                        size_t len, loff_t *off)
1169 {
1170         struct seq_file *seq = file->private_data;
1171         struct lprocfs_stats *stats = seq->private;
1172
1173         lprocfs_clear_stats(stats);
1174
1175         return len;
1176 }
1177
1178 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1179 {
1180         struct lprocfs_stats *stats = p->private;
1181
1182         return (*pos < stats->ls_num) ? pos : NULL;
1183 }
1184
1185 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1186 {
1187 }
1188
1189 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1190 {
1191         (*pos)++;
1192         return lprocfs_stats_seq_start(p, pos);
1193 }
1194
1195 /* seq file export of one lprocfs counter */
1196 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1197 {
1198         struct lprocfs_stats            *stats  = p->private;
1199         struct lprocfs_counter_header   *hdr;
1200         struct lprocfs_counter           ctr;
1201         int                              idx    = *(loff_t *)v;
1202         int                              rc     = 0;
1203
1204         if (idx == 0) {
1205                 struct timeval now;
1206                 do_gettimeofday(&now);
1207                 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1208                                 "snapshot_time", now.tv_sec, (unsigned long)now.tv_usec);
1209                 if (rc < 0)
1210                         return rc;
1211         }
1212         hdr = &stats->ls_cnt_header[idx];
1213         lprocfs_stats_collect(stats, idx, &ctr);
1214
1215         if (ctr.lc_count == 0)
1216                 goto out;
1217
1218         rc = seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1219                         ctr.lc_count, hdr->lc_units);
1220
1221         if (rc < 0)
1222                 goto out;
1223
1224         if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && (ctr.lc_count > 0)) {
1225                 rc = seq_printf(p, " %lld %lld %lld",
1226                                 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1227                 if (rc < 0)
1228                         goto out;
1229                 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1230                         rc = seq_printf(p, " %lld", ctr.lc_sumsquare);
1231                 if (rc < 0)
1232                         goto out;
1233         }
1234         rc = seq_printf(p, "\n");
1235 out:
1236         return (rc < 0) ? rc : 0;
1237 }
1238
1239 static const struct seq_operations lprocfs_stats_seq_sops = {
1240         .start  = lprocfs_stats_seq_start,
1241         .stop   = lprocfs_stats_seq_stop,
1242         .next   = lprocfs_stats_seq_next,
1243         .show   = lprocfs_stats_seq_show,
1244 };
1245
1246 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1247 {
1248         struct seq_file *seq;
1249         int rc;
1250
1251         rc = seq_open(file, &lprocfs_stats_seq_sops);
1252         if (rc)
1253                 return rc;
1254         seq = file->private_data;
1255         seq->private = PDE_DATA(inode);
1256         return 0;
1257 }
1258
1259 struct file_operations lprocfs_stats_seq_fops = {
1260         .owner   = THIS_MODULE,
1261         .open    = lprocfs_stats_seq_open,
1262         .read    = seq_read,
1263         .write   = lprocfs_stats_seq_write,
1264         .llseek  = seq_lseek,
1265         .release = lprocfs_seq_release,
1266 };
1267
1268 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1269                            struct lprocfs_stats *stats)
1270 {
1271         struct proc_dir_entry *entry;
1272         LASSERT(root != NULL);
1273
1274         entry = proc_create_data(name, 0644, root,
1275                                  &lprocfs_stats_seq_fops, stats);
1276         if (entry == NULL)
1277                 return -ENOMEM;
1278
1279         return 0;
1280 }
1281 EXPORT_SYMBOL(lprocfs_register_stats);
1282
1283 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1284                           unsigned conf, const char *name, const char *units)
1285 {
1286         struct lprocfs_counter_header   *header;
1287         struct lprocfs_counter          *percpu_cntr;
1288         unsigned long                   flags = 0;
1289         unsigned int                    i;
1290         unsigned int                    num_cpu;
1291
1292         LASSERT(stats != NULL);
1293
1294         header = &stats->ls_cnt_header[index];
1295         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1296                  index, name, units);
1297
1298         header->lc_config = conf;
1299         header->lc_name   = name;
1300         header->lc_units  = units;
1301
1302         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1303         for (i = 0; i < num_cpu; ++i) {
1304                 if (stats->ls_percpu[i] == NULL)
1305                         continue;
1306                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1307                 percpu_cntr->lc_count           = 0;
1308                 percpu_cntr->lc_min             = LC_MIN_INIT;
1309                 percpu_cntr->lc_max             = 0;
1310                 percpu_cntr->lc_sumsquare       = 0;
1311                 percpu_cntr->lc_sum             = 0;
1312                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1313                         percpu_cntr->lc_sum_irq = 0;
1314         }
1315         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1316 }
1317 EXPORT_SYMBOL(lprocfs_counter_init);
1318
1319 #define LPROCFS_OBD_OP_INIT(base, stats, op)                           \
1320 do {                                                                   \
1321         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);         \
1322         LASSERT(coffset < stats->ls_num);                                 \
1323         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");         \
1324 } while (0)
1325
1326 void lprocfs_init_ops_stats(int num_private_stats, struct lprocfs_stats *stats)
1327 {
1328         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
1329         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
1330         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info_async);
1331         LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
1332         LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
1333         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
1334         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
1335         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
1336         LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
1337         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
1338         LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
1339         LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
1340         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
1341         LPROCFS_OBD_OP_INIT(num_private_stats, stats, reconnect);
1342         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
1343         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_init);
1344         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_fini);
1345         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_alloc);
1346         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
1347         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs_async);
1348         LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
1349         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
1350         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
1351         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
1352         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
1353         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
1354         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr_async);
1355         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
1356         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
1357         LPROCFS_OBD_OP_INIT(num_private_stats, stats, adjust_kms);
1358         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
1359         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
1360         LPROCFS_OBD_OP_INIT(num_private_stats, stats, find_cbdata);
1361         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
1362         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
1363         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
1364         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
1365         LPROCFS_OBD_OP_INIT(num_private_stats, stats, health_check);
1366         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_uuid);
1367         LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotacheck);
1368         LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotactl);
1369         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_new);
1370         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_rem);
1371         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_add);
1372         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_del);
1373         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getref);
1374         LPROCFS_OBD_OP_INIT(num_private_stats, stats, putref);
1375 }
1376 EXPORT_SYMBOL(lprocfs_init_ops_stats);
1377
1378 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
1379 {
1380         struct lprocfs_stats *stats;
1381         unsigned int num_stats;
1382         int rc, i;
1383
1384         LASSERT(obd->obd_stats == NULL);
1385         LASSERT(obd->obd_proc_entry != NULL);
1386         LASSERT(obd->obd_cntr_base == 0);
1387
1388         num_stats = ((int)sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
1389                 num_private_stats - 1 /* o_owner */;
1390         stats = lprocfs_alloc_stats(num_stats, 0);
1391         if (stats == NULL)
1392                 return -ENOMEM;
1393
1394         lprocfs_init_ops_stats(num_private_stats, stats);
1395
1396         for (i = num_private_stats; i < num_stats; i++) {
1397                 /* If this LBUGs, it is likely that an obd
1398                  * operation was added to struct obd_ops in
1399                  * <obd.h>, and that the corresponding line item
1400                  * LPROCFS_OBD_OP_INIT(.., .., opname)
1401                  * is missing from the list above. */
1402                 LASSERTF(stats->ls_cnt_header[i].lc_name != NULL,
1403                          "Missing obd_stat initializer obd_op operation at offset %d.\n",
1404                          i - num_private_stats);
1405         }
1406         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
1407         if (rc < 0) {
1408                 lprocfs_free_stats(&stats);
1409         } else {
1410                 obd->obd_stats  = stats;
1411                 obd->obd_cntr_base = num_private_stats;
1412         }
1413         return rc;
1414 }
1415 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
1416
1417 void lprocfs_free_obd_stats(struct obd_device *obd)
1418 {
1419         if (obd->obd_stats)
1420                 lprocfs_free_stats(&obd->obd_stats);
1421 }
1422 EXPORT_SYMBOL(lprocfs_free_obd_stats);
1423
1424 #define LPROCFS_MD_OP_INIT(base, stats, op)                          \
1425 do {                                                                \
1426         unsigned int coffset = base + MD_COUNTER_OFFSET(op);        \
1427         LASSERT(coffset < stats->ls_num);                              \
1428         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");      \
1429 } while (0)
1430
1431 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1432 {
1433         LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1434         LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1435         LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1436         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1437         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1438         LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
1439         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1440         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1441         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1442         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1443         LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1444         LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1445         LPROCFS_MD_OP_INIT(num_private_stats, stats, is_subdir);
1446         LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1447         LPROCFS_MD_OP_INIT(num_private_stats, stats, sync);
1448         LPROCFS_MD_OP_INIT(num_private_stats, stats, readpage);
1449         LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1450         LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1451         LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1452         LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1453         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1454         LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1455         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1456         LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1457         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1458         LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1459         LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1460         LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
1461         LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
1462         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1463         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1464         LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1465 }
1466 EXPORT_SYMBOL(lprocfs_init_mps_stats);
1467
1468 int lprocfs_alloc_md_stats(struct obd_device *obd,
1469                            unsigned num_private_stats)
1470 {
1471         struct lprocfs_stats *stats;
1472         unsigned int num_stats;
1473         int rc, i;
1474
1475         LASSERT(obd->md_stats == NULL);
1476         LASSERT(obd->obd_proc_entry != NULL);
1477         LASSERT(obd->md_cntr_base == 0);
1478
1479         num_stats = 1 + MD_COUNTER_OFFSET(revalidate_lock) +
1480                     num_private_stats;
1481         stats = lprocfs_alloc_stats(num_stats, 0);
1482         if (stats == NULL)
1483                 return -ENOMEM;
1484
1485         lprocfs_init_mps_stats(num_private_stats, stats);
1486
1487         for (i = num_private_stats; i < num_stats; i++) {
1488                 if (stats->ls_cnt_header[i].lc_name == NULL) {
1489                         CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1490                                i - num_private_stats);
1491                         LBUG();
1492                 }
1493         }
1494         rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1495         if (rc < 0) {
1496                 lprocfs_free_stats(&stats);
1497         } else {
1498                 obd->md_stats  = stats;
1499                 obd->md_cntr_base = num_private_stats;
1500         }
1501         return rc;
1502 }
1503 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1504
1505 void lprocfs_free_md_stats(struct obd_device *obd)
1506 {
1507         struct lprocfs_stats *stats = obd->md_stats;
1508
1509         if (stats != NULL) {
1510                 obd->md_stats = NULL;
1511                 obd->md_cntr_base = 0;
1512                 lprocfs_free_stats(&stats);
1513         }
1514 }
1515 EXPORT_SYMBOL(lprocfs_free_md_stats);
1516
1517 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1518 {
1519         lprocfs_counter_init(ldlm_stats,
1520                              LDLM_ENQUEUE - LDLM_FIRST_OPC,
1521                              0, "ldlm_enqueue", "reqs");
1522         lprocfs_counter_init(ldlm_stats,
1523                              LDLM_CONVERT - LDLM_FIRST_OPC,
1524                              0, "ldlm_convert", "reqs");
1525         lprocfs_counter_init(ldlm_stats,
1526                              LDLM_CANCEL - LDLM_FIRST_OPC,
1527                              0, "ldlm_cancel", "reqs");
1528         lprocfs_counter_init(ldlm_stats,
1529                              LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1530                              0, "ldlm_bl_callback", "reqs");
1531         lprocfs_counter_init(ldlm_stats,
1532                              LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1533                              0, "ldlm_cp_callback", "reqs");
1534         lprocfs_counter_init(ldlm_stats,
1535                              LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1536                              0, "ldlm_gl_callback", "reqs");
1537 }
1538 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1539
1540 int lprocfs_exp_print_uuid(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1541                            struct hlist_node *hnode, void *data)
1542
1543 {
1544         struct obd_export *exp = cfs_hash_object(hs, hnode);
1545         struct seq_file *m = (struct seq_file *)data;
1546
1547         if (exp->exp_nid_stats)
1548                 seq_printf(m, "%s\n", obd_uuid2str(&exp->exp_client_uuid));
1549
1550         return 0;
1551 }
1552
1553 static int
1554 lproc_exp_uuid_seq_show(struct seq_file *m, void *unused)
1555 {
1556         struct nid_stat *stats = (struct nid_stat *)m->private;
1557         struct obd_device *obd = stats->nid_obd;
1558
1559         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1560                               lprocfs_exp_print_uuid, m);
1561         return 0;
1562 }
1563
1564 LPROC_SEQ_FOPS_RO(lproc_exp_uuid);
1565
1566 struct exp_hash_cb_data {
1567         struct seq_file *m;
1568         bool            first;
1569 };
1570
1571 int lprocfs_exp_print_hash(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1572                            struct hlist_node *hnode, void *cb_data)
1573
1574 {
1575         struct exp_hash_cb_data *data = (struct exp_hash_cb_data *)cb_data;
1576         struct obd_export       *exp = cfs_hash_object(hs, hnode);
1577
1578         if (exp->exp_lock_hash != NULL) {
1579                 if (data->first) {
1580                         cfs_hash_debug_header(data->m);
1581                         data->first = false;
1582                 }
1583                 cfs_hash_debug_str(hs, data->m);
1584         }
1585
1586         return 0;
1587 }
1588
1589 static int
1590 lproc_exp_hash_seq_show(struct seq_file *m, void *unused)
1591 {
1592         struct nid_stat *stats = (struct nid_stat *)m->private;
1593         struct obd_device *obd = stats->nid_obd;
1594         struct exp_hash_cb_data cb_data = {
1595                 .m = m,
1596                 .first = true
1597         };
1598
1599         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1600                               lprocfs_exp_print_hash, &cb_data);
1601         return 0;
1602 }
1603
1604 LPROC_SEQ_FOPS_RO(lproc_exp_hash);
1605
1606 int lprocfs_nid_stats_clear_read(struct seq_file *m, void *data)
1607 {
1608         return seq_printf(m, "%s\n",
1609                           "Write into this file to clear all nid stats and stale nid entries");
1610 }
1611 EXPORT_SYMBOL(lprocfs_nid_stats_clear_read);
1612
1613 static int lprocfs_nid_stats_clear_write_cb(void *obj, void *data)
1614 {
1615         struct nid_stat *stat = obj;
1616
1617         CDEBUG(D_INFO, "refcnt %d\n", atomic_read(&stat->nid_exp_ref_count));
1618         if (atomic_read(&stat->nid_exp_ref_count) == 1) {
1619                 /* object has only hash references. */
1620                 spin_lock(&stat->nid_obd->obd_nid_lock);
1621                 list_move(&stat->nid_list, data);
1622                 spin_unlock(&stat->nid_obd->obd_nid_lock);
1623                 return 1;
1624         }
1625         /* we has reference to object - only clear data*/
1626         if (stat->nid_stats)
1627                 lprocfs_clear_stats(stat->nid_stats);
1628
1629         return 0;
1630 }
1631
1632 int lprocfs_nid_stats_clear_write(struct file *file, const char *buffer,
1633                                   unsigned long count, void *data)
1634 {
1635         struct obd_device *obd = (struct obd_device *)data;
1636         struct nid_stat *client_stat;
1637         LIST_HEAD(free_list);
1638
1639         cfs_hash_cond_del(obd->obd_nid_stats_hash,
1640                           lprocfs_nid_stats_clear_write_cb, &free_list);
1641
1642         while (!list_empty(&free_list)) {
1643                 client_stat = list_entry(free_list.next, struct nid_stat,
1644                                              nid_list);
1645                 list_del_init(&client_stat->nid_list);
1646                 lprocfs_free_client_stats(client_stat);
1647         }
1648
1649         return count;
1650 }
1651 EXPORT_SYMBOL(lprocfs_nid_stats_clear_write);
1652
1653 int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid, int *newnid)
1654 {
1655         struct nid_stat *new_stat, *old_stat;
1656         struct obd_device *obd = NULL;
1657         struct proc_dir_entry *entry;
1658         char *buffer = NULL;
1659         int rc = 0;
1660
1661         *newnid = 0;
1662
1663         if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry ||
1664             !exp->exp_obd->obd_nid_stats_hash)
1665                 return -EINVAL;
1666
1667         /* not test against zero because eric say:
1668          * You may only test nid against another nid, or LNET_NID_ANY.
1669          * Anything else is nonsense.*/
1670         if (!nid || *nid == LNET_NID_ANY)
1671                 return 0;
1672
1673         obd = exp->exp_obd;
1674
1675         CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash);
1676
1677         OBD_ALLOC_PTR(new_stat);
1678         if (new_stat == NULL)
1679                 return -ENOMEM;
1680
1681         new_stat->nid          = *nid;
1682         new_stat->nid_obd          = exp->exp_obd;
1683         /* we need set default refcount to 1 to balance obd_disconnect */
1684         atomic_set(&new_stat->nid_exp_ref_count, 1);
1685
1686         old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash,
1687                                            nid, &new_stat->nid_hash);
1688         CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
1689                old_stat, libcfs_nid2str(*nid),
1690                atomic_read(&new_stat->nid_exp_ref_count));
1691
1692         /* We need to release old stats because lprocfs_exp_cleanup() hasn't
1693          * been and will never be called. */
1694         if (exp->exp_nid_stats) {
1695                 nidstat_putref(exp->exp_nid_stats);
1696                 exp->exp_nid_stats = NULL;
1697         }
1698
1699         /* Return -EALREADY here so that we know that the /proc
1700          * entry already has been created */
1701         if (old_stat != new_stat) {
1702                 exp->exp_nid_stats = old_stat;
1703                 rc = -EALREADY;
1704                 goto destroy_new;
1705         }
1706         /* not found - create */
1707         OBD_ALLOC(buffer, LNET_NIDSTR_SIZE);
1708         if (buffer == NULL) {
1709                 rc = -ENOMEM;
1710                 goto destroy_new;
1711         }
1712
1713         memcpy(buffer, libcfs_nid2str(*nid), LNET_NIDSTR_SIZE);
1714         new_stat->nid_proc = lprocfs_register(buffer,
1715                                               obd->obd_proc_exports_entry,
1716                                               NULL, NULL);
1717         OBD_FREE(buffer, LNET_NIDSTR_SIZE);
1718
1719         if (IS_ERR(new_stat->nid_proc)) {
1720                 CERROR("Error making export directory for nid %s\n",
1721                        libcfs_nid2str(*nid));
1722                 rc = PTR_ERR(new_stat->nid_proc);
1723                 new_stat->nid_proc = NULL;
1724                 goto destroy_new_ns;
1725         }
1726
1727         entry = lprocfs_add_simple(new_stat->nid_proc, "uuid",
1728                                    new_stat, &lproc_exp_uuid_fops);
1729         if (IS_ERR(entry)) {
1730                 CWARN("Error adding the NID stats file\n");
1731                 rc = PTR_ERR(entry);
1732                 goto destroy_new_ns;
1733         }
1734
1735         entry = lprocfs_add_simple(new_stat->nid_proc, "hash",
1736                                    new_stat, &lproc_exp_hash_fops);
1737         if (IS_ERR(entry)) {
1738                 CWARN("Error adding the hash file\n");
1739                 rc = PTR_ERR(entry);
1740                 goto destroy_new_ns;
1741         }
1742
1743         exp->exp_nid_stats = new_stat;
1744         *newnid = 1;
1745         /* protect competitive add to list, not need locking on destroy */
1746         spin_lock(&obd->obd_nid_lock);
1747         list_add(&new_stat->nid_list, &obd->obd_nid_stats);
1748         spin_unlock(&obd->obd_nid_lock);
1749
1750         return rc;
1751
1752 destroy_new_ns:
1753         if (new_stat->nid_proc != NULL)
1754                 lprocfs_remove(&new_stat->nid_proc);
1755         cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash);
1756
1757 destroy_new:
1758         nidstat_putref(new_stat);
1759         OBD_FREE_PTR(new_stat);
1760         return rc;
1761 }
1762 EXPORT_SYMBOL(lprocfs_exp_setup);
1763
1764 int lprocfs_exp_cleanup(struct obd_export *exp)
1765 {
1766         struct nid_stat *stat = exp->exp_nid_stats;
1767
1768         if (!stat || !exp->exp_obd)
1769                 return 0;
1770
1771         nidstat_putref(exp->exp_nid_stats);
1772         exp->exp_nid_stats = NULL;
1773
1774         return 0;
1775 }
1776 EXPORT_SYMBOL(lprocfs_exp_cleanup);
1777
1778 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1779                           struct lprocfs_counter_header *header,
1780                           enum lprocfs_stats_flags flags,
1781                           enum lprocfs_fields_flags field)
1782 {
1783         __s64 ret = 0;
1784
1785         if (lc == NULL || header == NULL)
1786                 return 0;
1787
1788         switch (field) {
1789         case LPROCFS_FIELDS_FLAGS_CONFIG:
1790                 ret = header->lc_config;
1791                 break;
1792         case LPROCFS_FIELDS_FLAGS_SUM:
1793                 ret = lc->lc_sum;
1794                 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1795                         ret += lc->lc_sum_irq;
1796                 break;
1797         case LPROCFS_FIELDS_FLAGS_MIN:
1798                 ret = lc->lc_min;
1799                 break;
1800         case LPROCFS_FIELDS_FLAGS_MAX:
1801                 ret = lc->lc_max;
1802                 break;
1803         case LPROCFS_FIELDS_FLAGS_AVG:
1804                 ret = (lc->lc_max - lc->lc_min) / 2;
1805                 break;
1806         case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1807                 ret = lc->lc_sumsquare;
1808                 break;
1809         case LPROCFS_FIELDS_FLAGS_COUNT:
1810                 ret = lc->lc_count;
1811                 break;
1812         default:
1813                 break;
1814         }
1815
1816         return 0;
1817 }
1818 EXPORT_SYMBOL(lprocfs_read_helper);
1819
1820 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1821                          int *val)
1822 {
1823         return lprocfs_write_frac_helper(buffer, count, val, 1);
1824 }
1825 EXPORT_SYMBOL(lprocfs_write_helper);
1826
1827 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1828 {
1829         long decimal_val, frac_val;
1830
1831         decimal_val = val / mult;
1832         seq_printf(m, "%ld", decimal_val);
1833         frac_val = val % mult;
1834
1835         if (frac_val > 0) {
1836                 frac_val *= 100;
1837                 frac_val /= mult;
1838         }
1839         if (frac_val > 0) {
1840                 /* Three cases: x0, xx, 0x */
1841                 if ((frac_val % 10) != 0)
1842                         seq_printf(m, ".%ld", frac_val);
1843                 else
1844                         seq_printf(m, ".%ld", frac_val / 10);
1845         }
1846
1847         seq_printf(m, "\n");
1848         return 0;
1849 }
1850 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1851
1852 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1853                              __u64 *val)
1854 {
1855         return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1856 }
1857 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1858
1859 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
1860                               __u64 *val, int mult)
1861 {
1862         char kernbuf[22], *end, *pbuf;
1863         __u64 whole, frac = 0, units;
1864         unsigned frac_d = 1;
1865
1866         if (count > (sizeof(kernbuf) - 1))
1867                 return -EINVAL;
1868
1869         if (copy_from_user(kernbuf, buffer, count))
1870                 return -EFAULT;
1871
1872         kernbuf[count] = '\0';
1873         pbuf = kernbuf;
1874         if (*pbuf == '-') {
1875                 mult = -mult;
1876                 pbuf++;
1877         }
1878
1879         whole = simple_strtoull(pbuf, &end, 10);
1880         if (pbuf == end)
1881                 return -EINVAL;
1882
1883         if (end != NULL && *end == '.') {
1884                 int i;
1885                 pbuf = end + 1;
1886
1887                 /* need to limit frac_d to a __u32 */
1888                 if (strlen(pbuf) > 10)
1889                         pbuf[10] = '\0';
1890
1891                 frac = simple_strtoull(pbuf, &end, 10);
1892                 /* count decimal places */
1893                 for (i = 0; i < (end - pbuf); i++)
1894                         frac_d *= 10;
1895         }
1896
1897         units = 1;
1898         switch (*end) {
1899         case 'p': case 'P':
1900                 units <<= 10;
1901         case 't': case 'T':
1902                 units <<= 10;
1903         case 'g': case 'G':
1904                 units <<= 10;
1905         case 'm': case 'M':
1906                 units <<= 10;
1907         case 'k': case 'K':
1908                 units <<= 10;
1909         }
1910         /* Specified units override the multiplier */
1911         if (units > 1)
1912                 mult = mult < 0 ? -units : units;
1913
1914         frac *= mult;
1915         do_div(frac, frac_d);
1916         *val = whole * mult + frac;
1917         return 0;
1918 }
1919 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1920
1921 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1922 {
1923         size_t l2;
1924
1925         l2 = strlen(s2);
1926         if (!l2)
1927                 return (char *)s1;
1928         while (len >= l2) {
1929                 len--;
1930                 if (!memcmp(s1, s2, l2))
1931                         return (char *)s1;
1932                 s1++;
1933         }
1934         return NULL;
1935 }
1936
1937 /**
1938  * Find the string \a name in the input \a buffer, and return a pointer to the
1939  * value immediately following \a name, reducing \a count appropriately.
1940  * If \a name is not found the original \a buffer is returned.
1941  */
1942 char *lprocfs_find_named_value(const char *buffer, const char *name,
1943                                size_t *count)
1944 {
1945         char *val;
1946         size_t buflen = *count;
1947
1948         /* there is no strnstr() in rhel5 and ubuntu kernels */
1949         val = lprocfs_strnstr(buffer, name, buflen);
1950         if (val == NULL)
1951                 return (char *)buffer;
1952
1953         val += strlen(name);                         /* skip prefix */
1954         while (val < buffer + buflen && isspace(*val)) /* skip separator */
1955                 val++;
1956
1957         *count = 0;
1958         while (val < buffer + buflen && isalnum(*val)) {
1959                 ++*count;
1960                 ++val;
1961         }
1962
1963         return val - *count;
1964 }
1965 EXPORT_SYMBOL(lprocfs_find_named_value);
1966
1967 int lprocfs_seq_create(struct proc_dir_entry *parent,
1968                        const char *name,
1969                        umode_t mode,
1970                        const struct file_operations *seq_fops,
1971                        void *data)
1972 {
1973         struct proc_dir_entry *entry;
1974
1975         /* Disallow secretly (un)writable entries. */
1976         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1977         entry = proc_create_data(name, mode, parent, seq_fops, data);
1978
1979         if (entry == NULL)
1980                 return -ENOMEM;
1981
1982         return 0;
1983 }
1984 EXPORT_SYMBOL(lprocfs_seq_create);
1985
1986 int lprocfs_obd_seq_create(struct obd_device *dev,
1987                            const char *name,
1988                            umode_t mode,
1989                            const struct file_operations *seq_fops,
1990                            void *data)
1991 {
1992         return lprocfs_seq_create(dev->obd_proc_entry, name,
1993                                   mode, seq_fops, data);
1994 }
1995 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1996
1997 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1998 {
1999         if (value >= OBD_HIST_MAX)
2000                 value = OBD_HIST_MAX - 1;
2001
2002         spin_lock(&oh->oh_lock);
2003         oh->oh_buckets[value]++;
2004         spin_unlock(&oh->oh_lock);
2005 }
2006 EXPORT_SYMBOL(lprocfs_oh_tally);
2007
2008 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2009 {
2010         unsigned int val;
2011
2012         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
2013                 ;
2014
2015         lprocfs_oh_tally(oh, val);
2016 }
2017 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2018
2019 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2020 {
2021         unsigned long ret = 0;
2022         int i;
2023
2024         for (i = 0; i < OBD_HIST_MAX; i++)
2025                 ret +=  oh->oh_buckets[i];
2026         return ret;
2027 }
2028 EXPORT_SYMBOL(lprocfs_oh_sum);
2029
2030 void lprocfs_oh_clear(struct obd_histogram *oh)
2031 {
2032         spin_lock(&oh->oh_lock);
2033         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2034         spin_unlock(&oh->oh_lock);
2035 }
2036 EXPORT_SYMBOL(lprocfs_oh_clear);
2037
2038 int lprocfs_obd_rd_max_pages_per_rpc(struct seq_file *m, void *data)
2039 {
2040         struct obd_device *dev = data;
2041         struct client_obd *cli = &dev->u.cli;
2042         int rc;
2043
2044         client_obd_list_lock(&cli->cl_loi_list_lock);
2045         rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2046         client_obd_list_unlock(&cli->cl_loi_list_lock);
2047         return rc;
2048 }
2049 EXPORT_SYMBOL(lprocfs_obd_rd_max_pages_per_rpc);
2050
2051 #endif