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