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