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