Whamcloud - gitweb
- landing of b_fid after merge with b_hd_cleanup_merge.
[fs/lustre-release.git] / lustre / obdclass / lprocfs_status.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Hariharan Thantry <thantry@users.sourceforge.net>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26 #define DEBUG_SUBSYSTEM S_CLASS
27
28 #ifdef __KERNEL__
29 # include <linux/config.h>
30 # include <linux/module.h>
31 # include <linux/version.h>
32 # include <linux/slab.h>
33 # include <linux/types.h>
34 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
35 #  include <asm/statfs.h>
36 # endif
37 # include <linux/seq_file.h>
38 # include <asm/div64.h>
39 #else /* __KERNEL__ */
40 # include <liblustre.h>
41 #endif
42
43 #include <linux/obd_class.h>
44 #include <linux/lprocfs_status.h>
45 #include <linux/lustre_fsfilt.h>
46
47 #if defined(LPROCFS) && defined(__KERNEL__)
48 struct proc_dir_entry *lprocfs_srch(struct proc_dir_entry *head,
49                                     const char *name)
50 {
51         struct proc_dir_entry *temp;
52
53         if (head == NULL)
54                 return NULL;
55
56         temp = head->subdir;
57         while (temp != NULL) {
58                 if (strcmp(temp->name, name) == 0)
59                         return temp;
60
61                 temp = temp->next;
62         }
63         return NULL;
64 }
65
66 /* lprocfs API calls */
67 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
68                      void *data)
69 {
70         if (root == NULL || list == NULL)
71                 return -EINVAL;
72
73         while (list->name != NULL) {
74                 struct proc_dir_entry *cur_root, *proc;
75                 char *pathcopy, *cur, *next, pathbuf[64];
76                 int pathsize = strlen(list->name) + 1;
77
78                 proc = NULL;
79                 cur_root = root;
80
81                 /* need copy of path for strsep */
82                 if (strlen(list->name) > sizeof(pathbuf) - 1) {
83                         OBD_ALLOC(pathcopy, pathsize);
84                         if (pathcopy == NULL)
85                                 return -ENOMEM;
86                 } else {
87                         pathcopy = pathbuf;
88                 }
89
90                 next = pathcopy;
91                 strcpy(pathcopy, list->name);
92
93                 while (cur_root != NULL && (cur = strsep(&next, "/"))) {
94                         if (*cur =='\0') /* skip double/trailing "/" */
95                                 continue;
96
97                         proc = lprocfs_srch(cur_root, cur);
98                         CDEBUG(D_OTHER, "cur_root=%s, cur=%s, next=%s, (%s)\n",
99                                cur_root->name, cur, next,
100                                (proc ? "exists" : "new"));
101                         if (next != NULL) {
102                                 cur_root = (proc ? proc :
103                                             proc_mkdir(cur, cur_root));
104                         } else if (proc == NULL) {
105                                 mode_t mode = 0;
106                                 if (list->read_fptr)
107                                         mode = 0444;
108                                 if (list->write_fptr)
109                                         mode |= 0200;
110                                 proc = create_proc_entry(cur, mode, cur_root);
111                         }
112                 }
113
114                 if (pathcopy != pathbuf)
115                         OBD_FREE(pathcopy, pathsize);
116
117                 if (cur_root == NULL || proc == NULL) {
118                         CERROR("LprocFS: No memory to create /proc entry %s",
119                                list->name);
120                         return -ENOMEM;
121                 }
122
123                 proc->read_proc = list->read_fptr;
124                 proc->write_proc = list->write_fptr;
125                 proc->data = (list->data ? list->data : data);
126                 list++;
127         }
128         return 0;
129 }
130
131 void lprocfs_remove(struct proc_dir_entry *root)
132 {
133         struct proc_dir_entry *temp = root;
134         struct proc_dir_entry *rm_entry;
135         struct proc_dir_entry *parent;
136
137         LASSERT(root != NULL);
138         parent = root->parent;
139         LASSERT(parent != NULL);
140  
141         while (1) {
142                 while (temp->subdir != NULL)
143                         temp = temp->subdir;
144
145                 rm_entry = temp;
146                 temp = temp->parent;
147
148                 /* Memory corruption once caused this to fail, and
149                    without this LASSERT we would loop here forever. */
150                 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
151                          "0x%p  %s/%s len %d\n", rm_entry, temp->name,
152                          rm_entry->name, (int)strlen(rm_entry->name));
153
154                 remove_proc_entry(rm_entry->name, rm_entry->parent);
155                 if (temp == parent)
156                         break;
157         }
158 }
159
160 struct proc_dir_entry *lprocfs_register(const char *name,
161                                         struct proc_dir_entry *parent,
162                                         struct lprocfs_vars *list, void *data)
163 {
164         struct proc_dir_entry *newchild;
165
166         newchild = lprocfs_srch(parent, name);
167         if (newchild != NULL) {
168                 CERROR(" Lproc: Attempting to register %s more than once \n",
169                        name);
170                 return ERR_PTR(-EALREADY);
171         }
172
173         newchild = proc_mkdir(name, parent);
174         if (newchild != NULL && list != NULL) {
175                 int rc = lprocfs_add_vars(newchild, list, data);
176                 if (rc) {
177                         lprocfs_remove(newchild);
178                         return ERR_PTR(rc);
179                 }
180         }
181         return newchild;
182 }
183
184 /* Generic callbacks */
185
186 int lprocfs_rd_u64(char *page, char **start, off_t off,
187                    int count, int *eof, void *data)
188 {
189         LASSERT(data != NULL);
190         *eof = 1;
191         return snprintf(page, count, LPU64"\n", *(__u64 *)data);
192 }
193
194 int lprocfs_rd_uuid(char *page, char **start, off_t off, int count,
195                     int *eof, void *data)
196 {
197         struct obd_device *dev = (struct obd_device*)data;
198
199         LASSERT(dev != NULL);
200         *eof = 1;
201         return snprintf(page, count, "%s\n", dev->obd_uuid.uuid);
202 }
203
204 int lprocfs_rd_name(char *page, char **start, off_t off, int count,
205                     int *eof, void* data)
206 {
207         struct obd_device *dev = (struct obd_device *)data;
208
209         LASSERT(dev != NULL);
210         LASSERT(dev->obd_name != NULL);
211         *eof = 1;
212         return snprintf(page, count, "%s\n", dev->obd_name);
213 }
214
215 int lprocfs_rd_fstype(char *page, char **start, off_t off, int count, int *eof,
216                       void *data)
217 {
218         struct obd_device *obd = (struct obd_device *)data;
219
220         LASSERT(obd != NULL);
221         LASSERT(obd->obd_fsops != NULL);
222         LASSERT(obd->obd_fsops->fs_type != NULL);
223         return snprintf(page, count, "%s\n", obd->obd_fsops->fs_type);
224 }
225
226 int lprocfs_rd_blksize(char *page, char **start, off_t off, int count,
227                        int *eof, void *data)
228 {
229         struct obd_statfs osfs;
230         int rc = obd_statfs(data, &osfs, jiffies - HZ);
231         if (!rc) {
232                 *eof = 1;
233                 rc = snprintf(page, count, "%u\n", osfs.os_bsize);
234         }
235         return rc;
236 }
237
238 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off, int count,
239                            int *eof, void *data)
240 {
241         struct obd_statfs osfs;
242         int rc = obd_statfs(data, &osfs, jiffies - HZ);
243         if (!rc) {
244                 __u32 blk_size = osfs.os_bsize >> 10;
245                 __u64 result = osfs.os_blocks;
246
247                 while (blk_size >>= 1)
248                         result <<= 1;
249
250                 *eof = 1;
251                 rc = snprintf(page, count, LPU64"\n", result);
252         }
253         return rc;
254 }
255
256 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off, int count,
257                           int *eof, void *data)
258 {
259         struct obd_statfs osfs;
260         int rc = obd_statfs(data, &osfs, jiffies - HZ);
261         if (!rc) {
262                 __u32 blk_size = osfs.os_bsize >> 10;
263                 __u64 result = osfs.os_bfree;
264
265                 while (blk_size >>= 1)
266                         result <<= 1;
267
268                 *eof = 1;
269                 rc = snprintf(page, count, LPU64"\n", result);
270         }
271         return rc;
272 }
273
274 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off, int count,
275                            int *eof, void *data)
276 {
277         struct obd_statfs osfs;
278         int rc = obd_statfs(data, &osfs, jiffies - HZ);
279         if (!rc) {
280                 __u32 blk_size = osfs.os_bsize >> 10;
281                 __u64 result = osfs.os_bavail;
282
283                 while (blk_size >>= 1)
284                         result <<= 1;
285
286                 *eof = 1;
287                 rc = snprintf(page, count, LPU64"\n", result);
288         }
289         return rc;
290 }
291
292 int lprocfs_rd_filestotal(char *page, char **start, off_t off, int count,
293                           int *eof, void *data)
294 {
295         struct obd_statfs osfs;
296         int rc = obd_statfs(data, &osfs, jiffies - HZ);
297         if (!rc) {
298                 *eof = 1;
299                 rc = snprintf(page, count, LPU64"\n", osfs.os_files);
300         }
301
302         return rc;
303 }
304
305 int lprocfs_rd_filesfree(char *page, char **start, off_t off, int count,
306                          int *eof, void *data)
307 {
308         struct obd_statfs osfs;
309         int rc = obd_statfs(data, &osfs, jiffies - HZ);
310         if (!rc) {
311                 *eof = 1;
312                 rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
313         }
314         return rc;
315 }
316
317 int lprocfs_rd_server_uuid(char *page, char **start, off_t off, int count,
318                            int *eof, void *data)
319 {
320         struct obd_device *obd = (struct obd_device *)data;
321         struct obd_import *imp;
322         char *imp_state_name = NULL;
323
324         LASSERT(obd != NULL);
325         imp = obd->u.cli.cl_import;
326         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
327         *eof = 1;
328         return snprintf(page, count, "%s\t%s\n",
329                         imp->imp_target_uuid.uuid, imp_state_name);
330 }
331
332 int lprocfs_rd_conn_uuid(char *page, char **start, off_t off, int count,
333                          int *eof,  void *data)
334 {
335         struct obd_device *obd = (struct obd_device*)data;
336         struct ptlrpc_connection *conn;
337
338         LASSERT(obd != NULL);
339         conn = obd->u.cli.cl_import->imp_connection;
340         LASSERT(conn != NULL);
341         *eof = 1;
342         return snprintf(page, count, "%s\n", conn->c_remote_uuid.uuid);
343 }
344
345 int lprocfs_rd_num_exports(char *page, char **start, off_t off, int count,
346                            int *eof,  void *data)
347 {
348         struct obd_device *obd = (struct obd_device*)data;
349
350         LASSERT(obd != NULL);
351         *eof = 1;
352         return snprintf(page, count, "%u\n", obd->obd_num_exports);
353 }
354
355 int lprocfs_rd_numrefs(char *page, char **start, off_t off, int count,
356                        int *eof, void *data)
357 {
358         struct obd_type *class = (struct obd_type*) data;
359
360         LASSERT(class != NULL);
361         *eof = 1;
362         return snprintf(page, count, "%d\n", class->typ_refcnt);
363 }
364
365 int lprocfs_obd_attach(struct obd_device *dev, struct lprocfs_vars *list)
366 {
367         int rc = 0;
368
369         LASSERT(dev != NULL);
370         LASSERT(dev->obd_type != NULL);
371         LASSERT(dev->obd_type->typ_procroot != NULL);
372
373         dev->obd_proc_entry = lprocfs_register(dev->obd_name,
374                                                dev->obd_type->typ_procroot,
375                                                list, dev);
376         if (IS_ERR(dev->obd_proc_entry)) {
377                 rc = PTR_ERR(dev->obd_proc_entry);
378                 dev->obd_proc_entry = NULL;
379         }
380         return rc;
381 }
382
383 int lprocfs_obd_detach(struct obd_device *dev)
384 {
385         if (dev && dev->obd_proc_entry) {
386                 lprocfs_remove(dev->obd_proc_entry);
387                 dev->obd_proc_entry = NULL;
388         }
389         return 0;
390 }
391
392 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num)
393 {
394         struct lprocfs_stats *stats;
395         struct lprocfs_percpu *percpu;
396         unsigned int percpusize;
397         unsigned int i;
398
399         if (num == 0)
400                 return NULL;
401
402         OBD_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
403         if (stats == NULL)
404                 return NULL;
405
406         percpusize = L1_CACHE_ALIGN(offsetof(typeof(*percpu), lp_cntr[num]));
407         stats->ls_percpu_size = num_online_cpus() * percpusize;
408         OBD_ALLOC(stats->ls_percpu[0], stats->ls_percpu_size);
409         if (stats->ls_percpu[0] == NULL) {
410                 OBD_FREE(stats, offsetof(typeof(*stats),
411                                          ls_percpu[num_online_cpus()]));
412                 return NULL;
413         }
414
415         stats->ls_num = num;
416         for (i = 1; i < num_online_cpus(); i++)
417                 stats->ls_percpu[i] = (void *)(stats->ls_percpu[i - 1]) +
418                         percpusize;
419
420         return stats;
421 }
422
423 void lprocfs_free_stats(struct lprocfs_stats *stats)
424 {
425         if (stats->ls_num == 0)
426                 return;
427
428         OBD_FREE(stats->ls_percpu[0], stats->ls_percpu_size);
429         OBD_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
430 }
431
432 /* Reset counter under lock */
433 int lprocfs_counter_write(struct file *file, const char *buffer,
434                           unsigned long count, void *data)
435 {
436         /* not supported */
437         return 0;
438 }
439
440 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
441 {
442         struct lprocfs_stats *stats = p->private;
443         /* return 1st cpu location */
444         return (*pos >= stats->ls_num) ? NULL :
445                 &(stats->ls_percpu[0]->lp_cntr[*pos]);
446 }
447
448 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
449 {
450 }
451
452 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
453 {
454         struct lprocfs_stats *stats = p->private;
455         ++*pos;
456         return (*pos >= stats->ls_num) ? NULL :
457                 &(stats->ls_percpu[0]->lp_cntr[*pos]);
458 }
459
460 /* seq file export of one lprocfs counter */
461 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
462 {
463        struct lprocfs_stats *stats = p->private;
464        struct lprocfs_counter  *cntr = v;
465        struct lprocfs_counter  t, ret = { .lc_min = ~(__u64)0 };
466        int i, idx, rc;
467
468        if (cntr == &(stats->ls_percpu[0])->lp_cntr[0]) {
469                struct timeval now;
470                do_gettimeofday(&now);
471                rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
472                                "snapshot_time", now.tv_sec, now.tv_usec);
473                if (rc < 0)
474                        return rc;
475        }
476        idx = cntr - &(stats->ls_percpu[0])->lp_cntr[0];
477
478        for (i = 0; i < num_online_cpus(); i++) {
479                struct lprocfs_counter *percpu_cntr =
480                        &(stats->ls_percpu[i])->lp_cntr[idx];
481                int centry;
482
483                do {
484                        centry = atomic_read(&percpu_cntr->lc_cntl.la_entry);
485                        t.lc_count = percpu_cntr->lc_count;
486                        t.lc_sum = percpu_cntr->lc_sum;
487                        t.lc_min = percpu_cntr->lc_min;
488                        t.lc_max = percpu_cntr->lc_max;
489                        t.lc_sumsquare = percpu_cntr->lc_sumsquare;
490                } while (centry != atomic_read(&percpu_cntr->lc_cntl.la_entry) &&
491                         centry != atomic_read(&percpu_cntr->lc_cntl.la_exit));
492                ret.lc_count += t.lc_count;
493                ret.lc_sum += t.lc_sum;
494                if (t.lc_min < ret.lc_min)
495                        ret.lc_min = t.lc_min;
496                if (t.lc_max > ret.lc_max)
497                        ret.lc_max = t.lc_max;
498                ret.lc_sumsquare += t.lc_sumsquare;
499        }
500
501        rc = seq_printf(p, "%-25s "LPU64" samples [%s]", cntr->lc_name,
502                        ret.lc_count, cntr->lc_units);
503        if (rc < 0)
504                goto out;
505
506        if ((cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) && (ret.lc_count > 0)) {
507                rc = seq_printf(p, " "LPU64" "LPU64" "LPU64,
508                                ret.lc_min, ret.lc_max, ret.lc_sum);
509                if (rc < 0)
510                        goto out;
511                if (cntr->lc_config & LPROCFS_CNTR_STDDEV)
512                        rc = seq_printf(p, " "LPU64, ret.lc_sumsquare);
513                if (rc < 0)
514                        goto out;
515        }
516        rc = seq_printf(p, "\n");
517  out:
518        return (rc < 0) ? rc : 0;
519 }
520
521 struct seq_operations lprocfs_stats_seq_sops = {
522         start: lprocfs_stats_seq_start,
523         stop:  lprocfs_stats_seq_stop,
524         next:  lprocfs_stats_seq_next,
525         show:  lprocfs_stats_seq_show,
526 };
527
528 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
529 {
530         struct proc_dir_entry *dp = PDE(inode);
531         struct seq_file *seq;
532         int rc;
533
534         rc = seq_open(file, &lprocfs_stats_seq_sops);
535         if (rc)
536                 return rc;
537         seq = file->private_data;
538         seq->private = dp->data;
539         return 0;
540 }
541
542 struct file_operations lprocfs_stats_seq_fops = {
543         .owner   = THIS_MODULE,
544         .open    = lprocfs_stats_seq_open,
545         .read    = seq_read,
546         .llseek  = seq_lseek,
547         .release = seq_release,
548 };
549
550 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
551                            struct lprocfs_stats *stats)
552 {
553         struct proc_dir_entry *entry;
554         LASSERT(root != NULL);
555
556         entry = create_proc_entry(name, 0444, root);
557         if (entry == NULL)
558                 return -ENOMEM;
559         entry->proc_fops = &lprocfs_stats_seq_fops;
560         entry->data = (void *)stats;
561         entry->write_proc = lprocfs_counter_write;
562         return 0;
563 }
564
565 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
566                           unsigned conf, const char *name, const char *units)
567 {
568         struct lprocfs_counter *c;
569         int i;
570
571         LASSERT(stats != NULL);
572         for (i = 0; i < num_online_cpus(); i++) {
573                 c = &(stats->ls_percpu[i]->lp_cntr[index]);
574                 c->lc_config = conf;
575                 c->lc_min = ~(__u64)0;
576                 c->lc_name = name;
577                 c->lc_units = units;
578         }
579 }
580 EXPORT_SYMBOL(lprocfs_counter_init);
581
582 #define LPROCFS_OBD_OP_INIT(base, stats, op)                               \
583 do {                                                                       \
584         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);              \
585         LASSERT(coffset < stats->ls_num);                                  \
586         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");              \
587 } while (0)
588
589 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
590 {
591         struct lprocfs_stats *stats;
592         unsigned int num_stats;
593         int rc, i;
594
595         LASSERT(obd->obd_stats == NULL);
596         LASSERT(obd->obd_proc_entry != NULL);
597         LASSERT(obd->obd_cntr_base == 0);
598
599         num_stats = 1 + OBD_COUNTER_OFFSET(init_ea_size) +
600                 num_private_stats;
601         stats = lprocfs_alloc_stats(num_stats);
602         if (stats == NULL)
603                 return -ENOMEM;
604
605         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
606         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
607         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info);
608         LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
609         LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
610         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
611         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
612         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
613         LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
614         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
615         LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
616         LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
617         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
618         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect_post);
619         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
620         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
621         LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
622         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
623         LPROCFS_OBD_OP_INIT(num_private_stats, stats, revalidate_md);
624         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
625         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
626         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
627         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
628         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
629         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
630         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
631         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw_async);
632         LPROCFS_OBD_OP_INIT(num_private_stats, stats, prep_async_page);
633         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_async_io);
634         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_async_flags);
635         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_group_io);
636         LPROCFS_OBD_OP_INIT(num_private_stats, stats, trigger_group_io);
637         LPROCFS_OBD_OP_INIT(num_private_stats, stats, teardown_async_page);
638         LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
639         LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
640         LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
641         LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
642         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
643         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
644         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
645         LPROCFS_OBD_OP_INIT(num_private_stats, stats, do_cow);
646         LPROCFS_OBD_OP_INIT(num_private_stats, stats, write_extents);
647         LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
648         LPROCFS_OBD_OP_INIT(num_private_stats, stats, match);
649         LPROCFS_OBD_OP_INIT(num_private_stats, stats, change_cbdata);
650         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
651         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
652         LPROCFS_OBD_OP_INIT(num_private_stats, stats, san_preprw);
653         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
654         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
655         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_init); 
656         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_finish); 
657         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_connect); 
658         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin); 
659         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
660         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
661         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
662         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getready);
663         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_ea_size);
664
665         for (i = num_private_stats; i < num_stats; i++) {
666                 /* if this LBUGs, it is likely that an obd operation was added
667                  * to struct obd_ops in <linux/obd.h>, and that the
668                  * corresponding line item LPROCFS_OBD_OP_INIT(.., .., opname)
669                  * is missing from the list above. */
670                 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
671                         CERROR("Missing obd_stat initializer obd_op "
672                                "operation at offset %d. Aborting.\n",
673                                i - num_private_stats);
674                         LBUG();
675                 }
676         }
677         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
678         if (rc < 0) {
679                 lprocfs_free_stats(stats);
680         } else {
681                 obd->obd_stats  = stats;
682                 obd->obd_cntr_base = num_private_stats;
683         }
684         return rc;
685 }
686
687 void lprocfs_free_obd_stats(struct obd_device *obd)
688 {
689         struct lprocfs_stats *stats = obd->obd_stats;
690
691         if (stats != NULL) {
692                 obd->obd_stats = NULL;
693                 lprocfs_free_stats(stats);
694         }
695 }
696
697 int lprocfs_write_helper(const char *buffer, unsigned long count,
698                          int *val)
699 {
700         char kernbuf[20], *end;
701
702         if (count > (sizeof(kernbuf) - 1))
703                 return -EINVAL;
704
705         if (copy_from_user(kernbuf, buffer, count))
706                 return -EFAULT;
707
708         kernbuf[count] = '\0';
709
710         *val = simple_strtol(kernbuf, &end, 0);
711         if (kernbuf == end)
712                 return -EINVAL;
713
714         return 0;
715 }
716
717 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
718                              __u64 *val)
719 {
720         char kernbuf[22], *end;
721
722         if (count > (sizeof(kernbuf) - 1))
723                 return -EINVAL;
724
725         if (copy_from_user(kernbuf, buffer, count))
726                 return -EFAULT;
727
728         kernbuf[count] = '\0';
729
730         *val = simple_strtoull(kernbuf, &end, 0);
731         if (kernbuf == end)
732                 return -EINVAL;
733
734         return 0;
735 }
736
737 int lprocfs_obd_seq_create(struct obd_device *dev, char *name, mode_t mode,
738                            struct file_operations *seq_fops, void *data)
739 {
740         struct proc_dir_entry *entry;
741         ENTRY;
742
743         entry = create_proc_entry(name, mode, dev->obd_proc_entry);
744         if (entry == NULL)
745                 RETURN(-ENOMEM);
746         entry->proc_fops = seq_fops;
747         entry->data = data;
748
749         RETURN(0);
750 }
751 EXPORT_SYMBOL(lprocfs_obd_seq_create);
752
753 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
754 {
755         unsigned long flags;
756
757         if (value >= OBD_HIST_MAX)
758                 value = OBD_HIST_MAX - 1;
759
760         spin_lock_irqsave(&oh->oh_lock, flags);
761         oh->oh_buckets[value]++;
762         spin_unlock_irqrestore(&oh->oh_lock, flags);
763 }
764 EXPORT_SYMBOL(lprocfs_oh_tally);
765
766 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
767 {
768         unsigned int val;
769
770         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
771                 ;
772
773         lprocfs_oh_tally(oh, val);
774 }
775 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
776
777 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
778 {
779         unsigned long ret = 0;
780         int i;
781
782         for (i = 0; i < OBD_HIST_MAX; i++)
783                 ret +=  oh->oh_buckets[i];
784         return ret;
785 }
786 EXPORT_SYMBOL(lprocfs_oh_sum);
787
788 void lprocfs_oh_clear(struct obd_histogram *oh)
789 {
790         unsigned long flags;
791         spin_lock_irqsave(&oh->oh_lock, flags);
792         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
793         spin_unlock_irqrestore(&oh->oh_lock, flags);
794 }
795 EXPORT_SYMBOL(lprocfs_oh_clear);
796
797 /* XXX copied from ldlm/ldlm_lockd.c, copied from ptlrpc/service.c */
798 static long timeval_sub(struct timeval *large, struct timeval *small)
799 {
800         return ((large->tv_sec - small->tv_sec) * 1000000) +
801                 (large->tv_usec - small->tv_usec);
802 }
803 void lprocfs_stime_record(struct obd_service_time *stime, struct timeval *large,
804                           struct timeval *small)
805 {
806         long diff = timeval_sub(large, small);
807
808         if (diff < 0)
809                 return;
810
811         stime->st_num++;
812         stime->st_total_us += diff;
813 }
814 EXPORT_SYMBOL(lprocfs_stime_record);
815
816 unsigned long lprocfs_stime_avg_ms(struct obd_service_time *stime)
817 {
818         struct obd_service_time copy;
819
820         memcpy(&copy, stime, sizeof(copy));
821         if (copy.st_num > 0) {
822                 do_div(copy.st_total_us, copy.st_num * 1000);
823                 return (unsigned long)copy.st_total_us;
824         }
825         return 0;
826 }
827 EXPORT_SYMBOL(lprocfs_stime_avg_ms);
828
829 unsigned long lprocfs_stime_avg_us(struct obd_service_time *stime)
830 {
831         struct obd_service_time copy;
832         __u32 remainder;
833
834         memcpy(&copy, stime, sizeof(copy));
835         if (copy.st_num > 0) {
836                 do_div(copy.st_total_us, copy.st_num);
837                 remainder = do_div(copy.st_total_us, 1000);
838                 return (unsigned long)remainder;
839         }
840         return 0;
841 }
842 EXPORT_SYMBOL(lprocfs_stime_avg_us);
843
844 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
845                                           int count, int *eof, void *data)
846 {
847         struct obd_device *obd = data;
848         int len = 0, n,
849                 connected = obd->obd_connected_clients,
850                 max_recoverable = obd->obd_max_recoverable_clients,
851                 recoverable = obd->obd_recoverable_clients,
852                 completed = max_recoverable - recoverable,
853                 queue_len = obd->obd_requests_queued_for_recovery,
854                 replayed = obd->obd_replayed_requests;
855         __u64 next_transno = obd->obd_next_recovery_transno;
856
857         LASSERT(obd != NULL);
858         *eof = 1;
859
860         n = snprintf(page, count, "status: ");
861         page += n; len += n; count -= n;
862         if (obd->obd_max_recoverable_clients == 0) {
863                 n = snprintf(page, count, "INACTIVE\n");
864                 return len + n;
865         }
866
867         /* sampled unlocked, but really... */
868         if (obd->obd_recovering == 0) {
869                 n = snprintf(page, count, "COMPLETE\n");
870                 page += n; len += n; count -= n;
871
872                 n = snprintf(page, count, "recovery_start: %lu\n",
873                              obd->obd_recovery_start);
874                 page += n; len += n; count -= n;
875                 n = snprintf(page, count, "recovery_end: %lu\n",
876                              obd->obd_recovery_end);
877                 page += n; len += n; count -= n;
878                 n = snprintf(page, count, "recovered_clients: %d\n",
879                              completed);
880                 page += n; len += n; count -= n;
881                 n = snprintf(page, count, "unrecovered_clients: %d\n",
882                              obd->obd_recoverable_clients);
883                 page += n; len += n; count -= n;
884                 n = snprintf(page, count, "last_transno: "LPD64"\n",
885                              next_transno - 1);
886                 page += n; len += n; count -= n;
887                 n = snprintf(page, count, "replayed_requests: %d\n", replayed);
888                 return len + n;
889         }
890
891         n = snprintf(page, count, "RECOVERING\n");
892         page += n; len += n; count -= n;
893         n = snprintf(page, count, "recovery_start: %lu\n",
894                      obd->obd_recovery_start);
895         page += n; len += n; count -= n;
896         n = snprintf(page, count, "connected_clients: %d/%d\n",
897                      connected, max_recoverable);
898         page += n; len += n; count -= n;
899         n = snprintf(page, count, "completed_clients: %d/%d\n",
900                      completed, max_recoverable);
901         page += n; len += n; count -= n;
902         n = snprintf(page, count, "replayed_requests: %d/??\n", replayed);
903         page += n; len += n; count -= n;
904         n = snprintf(page, count, "queued_requests: %d\n", queue_len);
905         page += n; len += n; count -= n;
906         n = snprintf(page, count, "next_transno: "LPD64"\n", next_transno);
907         return len + n;
908 }
909 EXPORT_SYMBOL(lprocfs_obd_rd_recovery_status);
910 #endif /* LPROCFS*/
911
912 EXPORT_SYMBOL(lprocfs_register);
913 EXPORT_SYMBOL(lprocfs_srch);
914 EXPORT_SYMBOL(lprocfs_remove);
915 EXPORT_SYMBOL(lprocfs_add_vars);
916 EXPORT_SYMBOL(lprocfs_obd_attach);
917 EXPORT_SYMBOL(lprocfs_obd_detach);
918 EXPORT_SYMBOL(lprocfs_alloc_stats);
919 EXPORT_SYMBOL(lprocfs_free_stats);
920 EXPORT_SYMBOL(lprocfs_register_stats);
921 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
922 EXPORT_SYMBOL(lprocfs_free_obd_stats);
923
924 EXPORT_SYMBOL(lprocfs_rd_u64);
925 EXPORT_SYMBOL(lprocfs_rd_uuid);
926 EXPORT_SYMBOL(lprocfs_rd_name);
927 EXPORT_SYMBOL(lprocfs_rd_fstype);
928 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
929 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
930 EXPORT_SYMBOL(lprocfs_rd_num_exports);
931 EXPORT_SYMBOL(lprocfs_rd_numrefs);
932
933 EXPORT_SYMBOL(lprocfs_rd_blksize);
934 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
935 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
936 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
937 EXPORT_SYMBOL(lprocfs_rd_filestotal);
938 EXPORT_SYMBOL(lprocfs_rd_filesfree);
939
940 EXPORT_SYMBOL(lprocfs_write_helper);
941 EXPORT_SYMBOL(lprocfs_write_u64_helper);