Whamcloud - gitweb
- added md_ops stats
[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,
551                            const char *name,
552                            struct lprocfs_stats *stats)
553 {
554         struct proc_dir_entry *entry;
555         LASSERT(root != NULL);
556
557         entry = create_proc_entry(name, 0444, root);
558         if (entry == NULL)
559                 return -ENOMEM;
560         entry->proc_fops = &lprocfs_stats_seq_fops;
561         entry->data = (void *)stats;
562         entry->write_proc = lprocfs_counter_write;
563         return 0;
564 }
565
566 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
567                           unsigned conf, const char *name,
568                           const char *units)
569 {
570         struct lprocfs_counter *c;
571         int i;
572
573         LASSERT(stats != NULL);
574         for (i = 0; i < num_online_cpus(); i++) {
575                 c = &(stats->ls_percpu[i]->lp_cntr[index]);
576                 c->lc_config = conf;
577                 c->lc_min = ~(__u64)0;
578                 c->lc_name = name;
579                 c->lc_units = units;
580         }
581 }
582 EXPORT_SYMBOL(lprocfs_counter_init);
583
584 #define LPROCFS_OBD_OP_INIT(base, stats, op)                               \
585 do {                                                                       \
586         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);              \
587         LASSERT(coffset < stats->ls_num);                                  \
588         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");              \
589 } while (0)
590
591 int lprocfs_alloc_obd_stats(struct obd_device *obd,
592                             unsigned num_private_stats)
593 {
594         struct lprocfs_stats *stats;
595         unsigned int num_stats;
596         int rc, i;
597
598         LASSERT(obd->obd_stats == NULL);
599         LASSERT(obd->obd_proc_entry != NULL);
600         LASSERT(obd->obd_cntr_base == 0);
601
602         num_stats = 1 + OBD_COUNTER_OFFSET(init_ea_size) +
603                 num_private_stats;
604         stats = lprocfs_alloc_stats(num_stats);
605         if (stats == NULL)
606                 return -ENOMEM;
607
608         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
609         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
610         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info);
611         LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
612         LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
613         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
614         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
615         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
616         LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
617         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
618         LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
619         LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
620         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
621         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect_post);
622         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
623         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
624         LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
625         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
626         LPROCFS_OBD_OP_INIT(num_private_stats, stats, revalidate_md);
627         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
628         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
629         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
630         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
631         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
632         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
633         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
634         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw_async);
635         LPROCFS_OBD_OP_INIT(num_private_stats, stats, prep_async_page);
636         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_async_io);
637         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_async_flags);
638         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_group_io);
639         LPROCFS_OBD_OP_INIT(num_private_stats, stats, trigger_group_io);
640         LPROCFS_OBD_OP_INIT(num_private_stats, stats, teardown_async_page);
641         LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
642         LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
643         LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
644         LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
645         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
646         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
647         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
648         LPROCFS_OBD_OP_INIT(num_private_stats, stats, do_cow);
649         LPROCFS_OBD_OP_INIT(num_private_stats, stats, write_extents);
650         LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
651         LPROCFS_OBD_OP_INIT(num_private_stats, stats, match);
652         LPROCFS_OBD_OP_INIT(num_private_stats, stats, change_cbdata);
653         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
654         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
655         LPROCFS_OBD_OP_INIT(num_private_stats, stats, san_preprw);
656         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
657         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
658         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_init); 
659         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_finish); 
660         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_connect); 
661         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin); 
662         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
663         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
664         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
665         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getready);
666         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_ea_size);
667
668         for (i = num_private_stats; i < num_stats; i++) {
669                 /* if this LBUGs, it is likely that an obd operation was added
670                  * to struct obd_ops in <linux/obd.h>, and that the
671                  * corresponding line item LPROCFS_OBD_OP_INIT(.., .., opname)
672                  * is missing from the list above. */
673                 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
674                         CERROR("Missing obd_stat initializer obd_op "
675                                "operation at offset %d. Aborting.\n",
676                                i - num_private_stats);
677                         LBUG();
678                 }
679         }
680         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
681         if (rc < 0) {
682                 lprocfs_free_stats(stats);
683         } else {
684                 obd->obd_stats  = stats;
685                 obd->obd_cntr_base = num_private_stats;
686         }
687         return rc;
688 }
689
690 void lprocfs_free_obd_stats(struct obd_device *obd)
691 {
692         struct lprocfs_stats *stats = obd->obd_stats;
693
694         if (stats != NULL) {
695                 obd->obd_stats = NULL;
696                 lprocfs_free_stats(stats);
697         }
698 }
699
700 #define LPROCFS_MD_OP_INIT(base, stats, op)                             \
701 do {                                                                    \
702         unsigned int coffset = base + MD_COUNTER_OFFSET(op);            \
703         LASSERT(coffset < stats->ls_num);                               \
704         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");           \
705 } while (0)
706
707 int lprocfs_alloc_md_stats(struct obd_device *obd,
708                            unsigned num_private_stats)
709 {
710         struct lprocfs_stats *stats;
711         unsigned int num_stats;
712         int rc, i;
713
714         LASSERT(obd->md_stats == NULL);
715         LASSERT(obd->obd_proc_entry != NULL);
716         LASSERT(obd->md_cntr_base == 0);
717
718         num_stats = 1 + MD_COUNTER_OFFSET(delete_inode) +
719                 num_private_stats;
720         stats = lprocfs_alloc_stats(num_stats);
721         if (stats == NULL)
722                 return -ENOMEM;
723
724         LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
725         LPROCFS_MD_OP_INIT(num_private_stats, stats, change_cbdata);
726         LPROCFS_MD_OP_INIT(num_private_stats, stats, change_cbdata_name);
727         LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
728         LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
729         LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
730         LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
731         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
732         LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_lock);
733         LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
734         LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
735         LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
736         LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
737         LPROCFS_MD_OP_INIT(num_private_stats, stats, sync);
738         LPROCFS_MD_OP_INIT(num_private_stats, stats, readpage);
739         LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
740         LPROCFS_MD_OP_INIT(num_private_stats, stats, valid_attrs);
741         LPROCFS_MD_OP_INIT(num_private_stats, stats, get_real_obd);
742         LPROCFS_MD_OP_INIT(num_private_stats, stats, req2lustre_md);
743         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
744         LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
745         LPROCFS_MD_OP_INIT(num_private_stats, stats, store_inode_generation);
746         LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
747         LPROCFS_MD_OP_INIT(num_private_stats, stats, delete_inode);
748
749         for (i = num_private_stats; i < num_stats; i++) {
750                 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
751                         CERROR("Missing md_stat initializer md_op "
752                                "operation at offset %d. Aborting.\n",
753                                i - num_private_stats);
754                         LBUG();
755                 }
756         }
757         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
758         if (rc < 0) {
759                 lprocfs_free_stats(stats);
760         } else {
761                 obd->md_stats  = stats;
762                 obd->md_cntr_base = num_private_stats;
763         }
764         return rc;
765 }
766
767 void lprocfs_free_md_stats(struct obd_device *obd)
768 {
769         struct lprocfs_stats *stats = obd->md_stats;
770
771         if (stats != NULL) {
772                 obd->md_stats = NULL;
773                 lprocfs_free_stats(stats);
774         }
775 }
776
777 int lprocfs_write_helper(const char *buffer, unsigned long count,
778                          int *val)
779 {
780         char kernbuf[20], *end;
781
782         if (count > (sizeof(kernbuf) - 1))
783                 return -EINVAL;
784
785         if (copy_from_user(kernbuf, buffer, count))
786                 return -EFAULT;
787
788         kernbuf[count] = '\0';
789
790         *val = simple_strtol(kernbuf, &end, 0);
791         if (kernbuf == end)
792                 return -EINVAL;
793
794         return 0;
795 }
796
797 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
798                              __u64 *val)
799 {
800         char kernbuf[22], *end;
801
802         if (count > (sizeof(kernbuf) - 1))
803                 return -EINVAL;
804
805         if (copy_from_user(kernbuf, buffer, count))
806                 return -EFAULT;
807
808         kernbuf[count] = '\0';
809
810         *val = simple_strtoull(kernbuf, &end, 0);
811         if (kernbuf == end)
812                 return -EINVAL;
813
814         return 0;
815 }
816
817 int lprocfs_obd_seq_create(struct obd_device *dev, char *name, mode_t mode,
818                            struct file_operations *seq_fops, void *data)
819 {
820         struct proc_dir_entry *entry;
821         ENTRY;
822
823         entry = create_proc_entry(name, mode, dev->obd_proc_entry);
824         if (entry == NULL)
825                 RETURN(-ENOMEM);
826         entry->proc_fops = seq_fops;
827         entry->data = data;
828
829         RETURN(0);
830 }
831 EXPORT_SYMBOL(lprocfs_obd_seq_create);
832
833 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
834 {
835         unsigned long flags;
836
837         if (value >= OBD_HIST_MAX)
838                 value = OBD_HIST_MAX - 1;
839
840         spin_lock_irqsave(&oh->oh_lock, flags);
841         oh->oh_buckets[value]++;
842         spin_unlock_irqrestore(&oh->oh_lock, flags);
843 }
844 EXPORT_SYMBOL(lprocfs_oh_tally);
845
846 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
847 {
848         unsigned int val;
849
850         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
851                 ;
852
853         lprocfs_oh_tally(oh, val);
854 }
855 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
856
857 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
858 {
859         unsigned long ret = 0;
860         int i;
861
862         for (i = 0; i < OBD_HIST_MAX; i++)
863                 ret +=  oh->oh_buckets[i];
864         return ret;
865 }
866 EXPORT_SYMBOL(lprocfs_oh_sum);
867
868 void lprocfs_oh_clear(struct obd_histogram *oh)
869 {
870         unsigned long flags;
871         spin_lock_irqsave(&oh->oh_lock, flags);
872         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
873         spin_unlock_irqrestore(&oh->oh_lock, flags);
874 }
875 EXPORT_SYMBOL(lprocfs_oh_clear);
876
877 /* XXX copied from ldlm/ldlm_lockd.c, copied from ptlrpc/service.c */
878 static long timeval_sub(struct timeval *large, struct timeval *small)
879 {
880         return ((large->tv_sec - small->tv_sec) * 1000000) +
881                 (large->tv_usec - small->tv_usec);
882 }
883
884 void lprocfs_stime_record(struct obd_service_time *stime, struct timeval *large,
885                           struct timeval *small)
886 {
887         long diff = timeval_sub(large, small);
888
889         if (diff < 0)
890                 return;
891
892         stime->st_num++;
893         stime->st_total_us += diff;
894 }
895 EXPORT_SYMBOL(lprocfs_stime_record);
896
897 unsigned long lprocfs_stime_avg_ms(struct obd_service_time *stime)
898 {
899         struct obd_service_time copy;
900
901         memcpy(&copy, stime, sizeof(copy));
902         if (copy.st_num > 0) {
903                 do_div(copy.st_total_us, copy.st_num * 1000);
904                 return (unsigned long)copy.st_total_us;
905         }
906         return 0;
907 }
908 EXPORT_SYMBOL(lprocfs_stime_avg_ms);
909
910 unsigned long lprocfs_stime_avg_us(struct obd_service_time *stime)
911 {
912         struct obd_service_time copy;
913         __u32 remainder;
914
915         memcpy(&copy, stime, sizeof(copy));
916         if (copy.st_num > 0) {
917                 do_div(copy.st_total_us, copy.st_num);
918                 remainder = do_div(copy.st_total_us, 1000);
919                 return (unsigned long)remainder;
920         }
921         return 0;
922 }
923 EXPORT_SYMBOL(lprocfs_stime_avg_us);
924
925 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
926                                           int count, int *eof, void *data)
927 {
928         struct obd_device *obd = data;
929         int len = 0, n,
930                 connected = obd->obd_connected_clients,
931                 max_recoverable = obd->obd_max_recoverable_clients,
932                 recoverable = obd->obd_recoverable_clients,
933                 completed = max_recoverable - recoverable,
934                 queue_len = obd->obd_requests_queued_for_recovery,
935                 replayed = obd->obd_replayed_requests;
936         __u64 next_transno = obd->obd_next_recovery_transno;
937
938         LASSERT(obd != NULL);
939         *eof = 1;
940
941         n = snprintf(page, count, "status: ");
942         page += n; len += n; count -= n;
943         if (obd->obd_max_recoverable_clients == 0) {
944                 n = snprintf(page, count, "INACTIVE\n");
945                 return len + n;
946         }
947
948         /* sampled unlocked, but really... */
949         if (obd->obd_recovering == 0) {
950                 n = snprintf(page, count, "COMPLETE\n");
951                 page += n; len += n; count -= n;
952
953                 n = snprintf(page, count, "recovery_start: %lu\n",
954                              obd->obd_recovery_start);
955                 page += n; len += n; count -= n;
956                 n = snprintf(page, count, "recovery_end: %lu\n",
957                              obd->obd_recovery_end);
958                 page += n; len += n; count -= n;
959                 n = snprintf(page, count, "recovered_clients: %d\n",
960                              completed);
961                 page += n; len += n; count -= n;
962                 n = snprintf(page, count, "unrecovered_clients: %d\n",
963                              obd->obd_recoverable_clients);
964                 page += n; len += n; count -= n;
965                 n = snprintf(page, count, "last_transno: "LPD64"\n",
966                              next_transno - 1);
967                 page += n; len += n; count -= n;
968                 n = snprintf(page, count, "replayed_requests: %d\n", replayed);
969                 return len + n;
970         }
971
972         n = snprintf(page, count, "RECOVERING\n");
973         page += n; len += n; count -= n;
974         n = snprintf(page, count, "recovery_start: %lu\n",
975                      obd->obd_recovery_start);
976         page += n; len += n; count -= n;
977         n = snprintf(page, count, "connected_clients: %d/%d\n",
978                      connected, max_recoverable);
979         page += n; len += n; count -= n;
980         n = snprintf(page, count, "completed_clients: %d/%d\n",
981                      completed, max_recoverable);
982         page += n; len += n; count -= n;
983         n = snprintf(page, count, "replayed_requests: %d/??\n", replayed);
984         page += n; len += n; count -= n;
985         n = snprintf(page, count, "queued_requests: %d\n", queue_len);
986         page += n; len += n; count -= n;
987         n = snprintf(page, count, "next_transno: "LPD64"\n", next_transno);
988         return len + n;
989 }
990 EXPORT_SYMBOL(lprocfs_obd_rd_recovery_status);
991 #endif /* LPROCFS*/
992
993 EXPORT_SYMBOL(lprocfs_register);
994 EXPORT_SYMBOL(lprocfs_srch);
995 EXPORT_SYMBOL(lprocfs_remove);
996 EXPORT_SYMBOL(lprocfs_add_vars);
997 EXPORT_SYMBOL(lprocfs_obd_attach);
998 EXPORT_SYMBOL(lprocfs_obd_detach);
999 EXPORT_SYMBOL(lprocfs_alloc_stats);
1000 EXPORT_SYMBOL(lprocfs_free_stats);
1001 EXPORT_SYMBOL(lprocfs_register_stats);
1002 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
1003 EXPORT_SYMBOL(lprocfs_free_obd_stats);
1004 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1005 EXPORT_SYMBOL(lprocfs_free_md_stats);
1006
1007 EXPORT_SYMBOL(lprocfs_rd_u64);
1008 EXPORT_SYMBOL(lprocfs_rd_uuid);
1009 EXPORT_SYMBOL(lprocfs_rd_name);
1010 EXPORT_SYMBOL(lprocfs_rd_fstype);
1011 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
1012 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
1013 EXPORT_SYMBOL(lprocfs_rd_num_exports);
1014 EXPORT_SYMBOL(lprocfs_rd_numrefs);
1015
1016 EXPORT_SYMBOL(lprocfs_rd_blksize);
1017 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
1018 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
1019 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
1020 EXPORT_SYMBOL(lprocfs_rd_filestotal);
1021 EXPORT_SYMBOL(lprocfs_rd_filesfree);
1022
1023 EXPORT_SYMBOL(lprocfs_write_helper);
1024 EXPORT_SYMBOL(lprocfs_write_u64_helper);