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