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