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