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