Whamcloud - gitweb
Landing b_bug974 onto HEAD (20040213_1538).
[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 #else /* __KERNEL__ */
39 # include <liblustre.h>
40 #endif
41
42 #include <linux/obd_class.h>
43 #include <linux/lprocfs_status.h>
44 #include <linux/lustre_fsfilt.h>
45
46 #if defined(LPROCFS) && defined(__KERNEL__)
47
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
68 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
69                      void *data)
70 {
71         if (root == NULL || list == NULL)
72                 return -EINVAL;
73
74         while (list->name != NULL) {
75                 struct proc_dir_entry *cur_root, *proc;
76                 char *pathcopy, *cur, *next, pathbuf[64];
77                 int pathsize = strlen(list->name) + 1;
78
79                 proc = NULL;
80                 cur_root = root;
81
82                 /* need copy of path for strsep */
83                 if (strlen(list->name) > sizeof(pathbuf) - 1) {
84                         OBD_ALLOC(pathcopy, pathsize);
85                         if (pathcopy == NULL)
86                                 return -ENOMEM;
87                 } else {
88                         pathcopy = pathbuf;
89                 }
90
91                 next = pathcopy;
92                 strcpy(pathcopy, list->name);
93
94                 while (cur_root != NULL && (cur = strsep(&next, "/"))) {
95                         if (*cur =='\0') /* skip double/trailing "/" */
96                                 continue;
97
98                         proc = lprocfs_srch(cur_root, cur);
99                         CDEBUG(D_OTHER, "cur_root=%s, cur=%s, next=%s, (%s)\n",
100                                cur_root->name, cur, next,
101                                (proc ? "exists" : "new"));
102                         if (next != NULL) {
103                                 cur_root = (proc ? proc :
104                                             proc_mkdir(cur, cur_root));
105                         } else if (proc == NULL) {
106                                 mode_t mode = 0;
107                                 if (list->read_fptr)
108                                         mode = 0444;
109                                 if (list->write_fptr)
110                                         mode |= 0200;
111                                 proc = create_proc_entry(cur, mode, cur_root);
112                         }
113                 }
114
115                 if (pathcopy != pathbuf)
116                         OBD_FREE(pathcopy, pathsize);
117
118                 if (cur_root == NULL || proc == NULL) {
119                         CERROR("LprocFS: No memory to create /proc entry %s",
120                                list->name);
121                         return -ENOMEM;
122                 }
123
124                 proc->read_proc = list->read_fptr;
125                 proc->write_proc = list->write_fptr;
126                 proc->data = (list->data ? list->data : data);
127                 list++;
128         }
129         return 0;
130 }
131
132 void lprocfs_remove(struct proc_dir_entry *root)
133 {
134         struct proc_dir_entry *temp = root;
135         struct proc_dir_entry *rm_entry;
136         struct proc_dir_entry *parent;
137
138         LASSERT(root != NULL);
139         parent = root->parent;
140         LASSERT(parent != NULL);
141
142         while (1) {
143                 while (temp->subdir != NULL)
144                         temp = temp->subdir;
145
146                 rm_entry = temp;
147                 temp = temp->parent;
148                 remove_proc_entry(rm_entry->name, rm_entry->parent);
149                 if (temp == parent)
150                         break;
151         }
152 }
153
154 struct proc_dir_entry *lprocfs_register(const char *name,
155                                         struct proc_dir_entry *parent,
156                                         struct lprocfs_vars *list, void *data)
157 {
158         struct proc_dir_entry *newchild;
159
160         newchild = lprocfs_srch(parent, name);
161         if (newchild != NULL) {
162                 CERROR(" Lproc: Attempting to register %s more than once \n",
163                        name);
164                 return ERR_PTR(-EALREADY);
165         }
166
167         newchild = proc_mkdir(name, parent);
168         if (newchild != NULL && list != NULL) {
169                 int rc = lprocfs_add_vars(newchild, list, data);
170                 if (rc) {
171                         lprocfs_remove(newchild);
172                         return ERR_PTR(rc);
173                 }
174         }
175         return newchild;
176 }
177
178 /* Generic callbacks */
179
180 int lprocfs_rd_u64(char *page, char **start, off_t off,
181                    int count, int *eof, void *data)
182 {
183         LASSERT(data != NULL);
184         *eof = 1;
185         return snprintf(page, count, LPU64"\n", *(__u64 *)data);
186 }
187
188 int lprocfs_rd_uuid(char *page, char **start, off_t off, int count,
189                     int *eof, void *data)
190 {
191         struct obd_device *dev = (struct obd_device*)data;
192
193         LASSERT(dev != NULL);
194         *eof = 1;
195         return snprintf(page, count, "%s\n", dev->obd_uuid.uuid);
196 }
197
198 int lprocfs_rd_name(char *page, char **start, off_t off, int count,
199                     int *eof, void* data)
200 {
201         struct obd_device *dev = (struct obd_device *)data;
202
203         LASSERT(dev != NULL);
204         LASSERT(dev->obd_name != NULL);
205         *eof = 1;
206         return snprintf(page, count, "%s\n", dev->obd_name);
207 }
208
209 int lprocfs_rd_fstype(char *page, char **start, off_t off, int count, int *eof,
210                       void *data)
211 {
212         struct obd_device *obd = (struct obd_device *)data;
213
214         LASSERT(obd != NULL);
215         LASSERT(obd->obd_fsops != NULL);
216         LASSERT(obd->obd_fsops->fs_type != NULL);
217         return snprintf(page, count, "%s\n", obd->obd_fsops->fs_type);
218 }
219
220 int lprocfs_rd_blksize(char *page, char **start, off_t off, int count,
221                        int *eof, void *data)
222 {
223         struct obd_statfs osfs;
224         int rc = obd_statfs(data, &osfs, jiffies - HZ);
225         if (!rc) {
226                 *eof = 1;
227                 rc = snprintf(page, count, "%u\n", osfs.os_bsize);
228         }
229         return rc;
230 }
231
232 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off, int count,
233                            int *eof, void *data)
234 {
235         struct obd_statfs osfs;
236         int rc = obd_statfs(data, &osfs, jiffies - HZ);
237         if (!rc) {
238                 __u32 blk_size = osfs.os_bsize >> 10;
239                 __u64 result = osfs.os_blocks;
240
241                 while (blk_size >>= 1)
242                         result <<= 1;
243
244                 *eof = 1;
245                 rc = snprintf(page, count, LPU64"\n", result);
246         }
247         return rc;
248 }
249
250 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off, int count,
251                           int *eof, void *data)
252 {
253         struct obd_statfs osfs;
254         int rc = obd_statfs(data, &osfs, jiffies - HZ);
255         if (!rc) {
256                 __u32 blk_size = osfs.os_bsize >> 10;
257                 __u64 result = osfs.os_bfree;
258
259                 while (blk_size >>= 1)
260                         result <<= 1;
261
262                 *eof = 1;
263                 rc = snprintf(page, count, LPU64"\n", result);
264         }
265         return rc;
266 }
267
268 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off, int count,
269                            int *eof, void *data)
270 {
271         struct obd_statfs osfs;
272         int rc = obd_statfs(data, &osfs, jiffies - HZ);
273         if (!rc) {
274                 __u32 blk_size = osfs.os_bsize >> 10;
275                 __u64 result = osfs.os_bavail;
276
277                 while (blk_size >>= 1)
278                         result <<= 1;
279
280                 *eof = 1;
281                 rc = snprintf(page, count, LPU64"\n", result);
282         }
283         return rc;
284 }
285
286 int lprocfs_rd_filestotal(char *page, char **start, off_t off, int count,
287                           int *eof, void *data)
288 {
289         struct obd_statfs osfs;
290         int rc = obd_statfs(data, &osfs, jiffies - HZ);
291         if (!rc) {
292                 *eof = 1;
293                 rc = snprintf(page, count, LPU64"\n", osfs.os_files);
294         }
295
296         return rc;
297 }
298
299 int lprocfs_rd_filesfree(char *page, char **start, off_t off, int count,
300                          int *eof, void *data)
301 {
302         struct obd_statfs osfs;
303         int rc = obd_statfs(data, &osfs, jiffies - HZ);
304         if (!rc) {
305                 *eof = 1;
306                 rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
307         }
308         return rc;
309 }
310
311 int lprocfs_rd_filegroups(char *page, char **start, off_t off, int count,
312                           int *eof, void *data)
313 {
314         *eof = 1;
315         return snprintf(page, count, "unimplemented\n");
316 }
317
318 int lprocfs_rd_server_uuid(char *page, char **start, off_t off, int count,
319                            int *eof, void *data)
320 {
321         struct obd_device *obd = (struct obd_device *)data;
322         struct obd_import *imp;
323         char *imp_state_name = NULL;
324         
325         LASSERT(obd != NULL);
326         imp = obd->u.cli.cl_import;
327         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
328         *eof = 1;
329         return snprintf(page, count, "%s\t%s\n",
330                         imp->imp_target_uuid.uuid, imp_state_name);
331 }
332
333 int lprocfs_rd_conn_uuid(char *page, char **start, off_t off, int count,
334                          int *eof,  void *data)
335 {
336         struct obd_device *obd = (struct obd_device*)data;
337         struct ptlrpc_connection *conn;
338
339         LASSERT(obd != NULL);
340         conn = obd->u.cli.cl_import->imp_connection;
341         LASSERT(conn != NULL);
342         *eof = 1;
343         return snprintf(page, count, "%s\n", conn->c_remote_uuid.uuid);
344 }
345
346 int lprocfs_rd_numrefs(char *page, char **start, off_t off, int count,
347                        int *eof, void *data)
348 {
349         struct obd_type *class = (struct obd_type*) data;
350
351         LASSERT(class != NULL);
352         *eof = 1;
353         return snprintf(page, count, "%d\n", class->typ_refcnt);
354 }
355
356 int lprocfs_obd_attach(struct obd_device *dev, struct lprocfs_vars *list)
357 {
358         int rc = 0;
359
360         LASSERT(dev != NULL);
361         LASSERT(dev->obd_type != NULL);
362         LASSERT(dev->obd_type->typ_procroot != NULL);
363
364         dev->obd_proc_entry = lprocfs_register(dev->obd_name,
365                                                dev->obd_type->typ_procroot,
366                                                list, dev);
367         if (IS_ERR(dev->obd_proc_entry)) {
368                 rc = PTR_ERR(dev->obd_proc_entry);
369                 dev->obd_proc_entry = NULL;
370         }
371         return rc;
372 }
373
374 int lprocfs_obd_detach(struct obd_device *dev)
375 {
376         if (dev && dev->obd_proc_entry) {
377                 lprocfs_remove(dev->obd_proc_entry);
378                 dev->obd_proc_entry = NULL;
379         }
380         return 0;
381 }
382
383 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num)
384 {
385         struct lprocfs_stats *stats;
386         struct lprocfs_percpu *percpu;
387         unsigned int percpusize;
388         unsigned int i;
389
390         if (num == 0)
391                 return NULL;
392
393         OBD_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
394         if (stats == NULL)
395                 return NULL;
396
397         percpusize = L1_CACHE_ALIGN(offsetof(typeof(*percpu), lp_cntr[num]));
398         stats->ls_percpu_size = num_online_cpus() * percpusize;
399         OBD_ALLOC(stats->ls_percpu[0], stats->ls_percpu_size);
400         if (stats->ls_percpu[0] == NULL) {
401                 OBD_FREE(stats, offsetof(typeof(*stats),
402                                          ls_percpu[num_online_cpus()]));
403                 return NULL;
404         }
405
406         stats->ls_num = num;
407         for (i = 1; i < num_online_cpus(); i++)
408                 stats->ls_percpu[i] = (void *)(stats->ls_percpu[i - 1]) +
409                         percpusize;
410
411         return stats;
412 }
413
414 void lprocfs_free_stats(struct lprocfs_stats *stats)
415 {
416         if (stats->ls_num == 0)
417                 return;
418
419         OBD_FREE(stats->ls_percpu[0], stats->ls_percpu_size);
420         OBD_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
421 }
422
423 /* Reset counter under lock */
424 int lprocfs_counter_write(struct file *file, const char *buffer,
425                           unsigned long count, void *data)
426 {
427         /* not supported */
428         return 0;
429 }
430
431 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
432 {
433         struct lprocfs_stats *stats = p->private;
434         /* return 1st cpu location */
435         return (*pos >= stats->ls_num) ? NULL :
436                 &(stats->ls_percpu[0]->lp_cntr[*pos]);
437 }
438
439 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
440 {
441 }
442
443 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
444 {
445         struct lprocfs_stats *stats = p->private;
446         ++*pos;
447         return (*pos >= stats->ls_num) ? NULL :
448                 &(stats->ls_percpu[0]->lp_cntr[*pos]);
449 }
450
451 /* seq file export of one lprocfs counter */
452 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
453 {
454        struct lprocfs_stats *stats = p->private;
455        struct lprocfs_counter  *cntr = v;
456        struct lprocfs_counter  t, ret = { .lc_min = ~(__u64)0 };
457        int i, idx, rc;
458
459        if (cntr == &(stats->ls_percpu[0])->lp_cntr[0]) {
460                struct timeval now;
461                do_gettimeofday(&now);
462                rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
463                                "snapshot_time", now.tv_sec, now.tv_usec);
464                if (rc < 0)
465                        return rc;
466        }
467        idx = cntr - &(stats->ls_percpu[0])->lp_cntr[0];
468
469        for (i = 0; i < num_online_cpus(); i++) {
470                struct lprocfs_counter *percpu_cntr =
471                        &(stats->ls_percpu[i])->lp_cntr[idx];
472                int centry;
473
474                do {
475                        centry = atomic_read(&percpu_cntr->lc_cntl.la_entry);
476                        t.lc_count = percpu_cntr->lc_count;
477                        t.lc_sum = percpu_cntr->lc_sum;
478                        t.lc_min = percpu_cntr->lc_min;
479                        t.lc_max = percpu_cntr->lc_max;
480                        t.lc_sumsquare = percpu_cntr->lc_sumsquare;
481                } while (centry != atomic_read(&percpu_cntr->lc_cntl.la_entry) &&
482                         centry != atomic_read(&percpu_cntr->lc_cntl.la_exit));
483                ret.lc_count += t.lc_count;
484                ret.lc_sum += t.lc_sum;
485                if (t.lc_min < ret.lc_min)
486                        ret.lc_min = t.lc_min;
487                if (t.lc_max > ret.lc_max)
488                        ret.lc_max = t.lc_max;
489                ret.lc_sumsquare += t.lc_sumsquare;
490        }
491
492        rc = seq_printf(p, "%-25s "LPU64" samples [%s]", cntr->lc_name,
493                        ret.lc_count, cntr->lc_units);
494        if (rc < 0)
495                goto out;
496
497        if ((cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) && (ret.lc_count > 0)) {
498                rc = seq_printf(p, " "LPU64" "LPU64" "LPU64,
499                                ret.lc_min, ret.lc_max, ret.lc_sum);
500                if (rc < 0)
501                        goto out;
502                if (cntr->lc_config & LPROCFS_CNTR_STDDEV)
503                        rc = seq_printf(p, " "LPU64, ret.lc_sumsquare);
504                if (rc < 0)
505                        goto out;
506        }
507        rc = seq_printf(p, "\n");
508  out:
509        return (rc < 0) ? rc : 0;
510 }
511
512 struct seq_operations lprocfs_stats_seq_sops = {
513         start: lprocfs_stats_seq_start,
514         stop:  lprocfs_stats_seq_stop,
515         next:  lprocfs_stats_seq_next,
516         show:  lprocfs_stats_seq_show,
517 };
518
519 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
520 {
521         struct proc_dir_entry *dp = PDE(inode);
522         struct seq_file *seq;
523         int rc;
524
525         rc = seq_open(file, &lprocfs_stats_seq_sops);
526         if (rc)
527                 return rc;
528         seq = file->private_data;
529         seq->private = dp->data;
530         return 0;
531 }
532
533 struct file_operations lprocfs_stats_seq_fops = {
534         open:    lprocfs_stats_seq_open,
535         read:    seq_read,
536         llseek:  seq_lseek,
537         release: seq_release,
538 };
539
540 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
541                            struct lprocfs_stats *stats)
542 {
543         struct proc_dir_entry *entry;
544         LASSERT(root != NULL);
545
546         entry = create_proc_entry(name, 0444, root);
547         if (entry == NULL)
548                 return -ENOMEM;
549         entry->proc_fops = &lprocfs_stats_seq_fops;
550         entry->data = (void *)stats;
551         entry->write_proc = lprocfs_counter_write;
552         return 0;
553 }
554
555 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
556                           unsigned conf, const char *name, const char *units)
557 {
558         struct lprocfs_counter *c;
559         int i;
560
561         LASSERT(stats != NULL);
562         for (i = 0; i < num_online_cpus(); i++) {
563                 c = &(stats->ls_percpu[i]->lp_cntr[index]);
564                 c->lc_config = conf;
565                 c->lc_min = ~(__u64)0;
566                 c->lc_name = name;
567                 c->lc_units = units;
568         }
569 }
570 EXPORT_SYMBOL(lprocfs_counter_init);
571
572 #define LPROCFS_OBD_OP_INIT(base, stats, op)                               \
573 do {                                                                       \
574         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);              \
575         LASSERT(coffset < stats->ls_num);                                  \
576         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");              \
577 } while (0)
578
579 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
580 {
581         struct lprocfs_stats *stats;
582         unsigned int num_stats;
583         int rc, i;
584
585         LASSERT(obd->obd_stats == NULL);
586         LASSERT(obd->obd_proc_entry != NULL);
587         LASSERT(obd->obd_cntr_base == 0);
588
589         num_stats = 1 + OBD_COUNTER_OFFSET(notify) +
590                 num_private_stats;
591         stats = lprocfs_alloc_stats(num_stats);
592         if (stats == NULL)
593                 return -ENOMEM;
594
595         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
596         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
597         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info);
598         LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
599         LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
600         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
601         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
602         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
603         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
604         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
605         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
606         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
607         LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
608         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
609         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
610         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
611         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
612         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
613         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
614         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
615         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
616         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw_async);
617         LPROCFS_OBD_OP_INIT(num_private_stats, stats, prep_async_page);
618         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_async_io);
619         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_async_flags);
620         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_sync_io);
621         LPROCFS_OBD_OP_INIT(num_private_stats, stats, trigger_sync_io);
622         LPROCFS_OBD_OP_INIT(num_private_stats, stats, teardown_async_page);
623         LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
624         LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
625         LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
626         LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
627         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
628         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
629         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
630         LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
631         LPROCFS_OBD_OP_INIT(num_private_stats, stats, match);
632         LPROCFS_OBD_OP_INIT(num_private_stats, stats, change_cbdata);
633         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
634         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
635         LPROCFS_OBD_OP_INIT(num_private_stats, stats, san_preprw);
636         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
637         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
638         LPROCFS_OBD_OP_INIT(num_private_stats, stats, lock_contains);
639         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_init); 
640         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_finish); 
641         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin); 
642         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
643         LPROCFS_OBD_OP_INIT(num_private_stats, stats, invalidate_import);
644         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
645         
646         for (i = num_private_stats; i < num_stats; i++) {
647                 /* If this LBUGs, it is likely that an obd
648                  * operation was added to struct obd_ops in
649                  * <linux/obd.h>, and that the corresponding line item
650                  * LPROCFS_OBD_OP_INIT(.., .., opname)
651                  * is missing from the list above. */
652                 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
653                         CERROR("Missing obd_stat initializer obd_op "
654                                "operation at offset %d. Aborting.\n",
655                                i - num_private_stats);
656                         LBUG();
657                 }
658         }
659         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
660         if (rc < 0) {
661                 lprocfs_free_stats(stats);
662         } else {
663                 obd->obd_stats  = stats;
664                 obd->obd_cntr_base = num_private_stats;
665         }
666         return rc;
667 }
668
669 void lprocfs_free_obd_stats(struct obd_device *obd)
670 {
671         struct lprocfs_stats *stats = obd->obd_stats;
672
673         if (stats != NULL) {
674                 obd->obd_stats = NULL;
675                 lprocfs_free_stats(stats);
676         }
677 }
678
679 int lprocfs_write_helper(const char *buffer, unsigned long count,
680                          int *val)
681 {
682         char kernbuf[20], *end;
683
684         if (count > (sizeof(kernbuf) - 1))
685                 return -EINVAL;
686
687         if (copy_from_user(kernbuf, buffer, count))
688                 return -EFAULT;
689
690         kernbuf[count] = '\0';
691
692         *val = simple_strtol(kernbuf, &end, 0);
693         if (kernbuf == end)
694                 return -EINVAL;
695
696         return 0;
697 }
698
699 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
700                              __u64 *val)
701 {
702         char kernbuf[22], *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_strtoull(kernbuf, &end, 0);
713         if (kernbuf == end)
714                 return -EINVAL;
715
716         return 0;
717 }
718
719 int lprocfs_obd_seq_create(struct obd_device *dev, char *name, mode_t mode,
720                            struct file_operations *seq_fops, void *data)
721 {
722         struct proc_dir_entry *entry;
723         ENTRY;
724
725         entry = create_proc_entry(name, mode, dev->obd_proc_entry);
726         if (entry == NULL)
727                 RETURN(-ENOMEM);
728         entry->proc_fops = seq_fops;
729         entry->data = data;
730
731         RETURN(0);
732 }
733 EXPORT_SYMBOL(lprocfs_obd_seq_create);
734
735 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
736 {
737         unsigned long flags;
738
739         if (value >= OBD_HIST_MAX)
740                 value = OBD_HIST_MAX - 1;
741
742         spin_lock_irqsave(&oh->oh_lock, flags);
743         oh->oh_buckets[value]++;
744         spin_unlock_irqrestore(&oh->oh_lock, flags);
745 }
746 EXPORT_SYMBOL(lprocfs_oh_tally);
747
748 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
749 {
750         unsigned int val;
751
752         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
753                 ;
754
755         lprocfs_oh_tally(oh, val);
756 }
757 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
758
759 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
760 {
761         unsigned long ret = 0;
762         int i;
763
764         for (i = 0; i < OBD_HIST_MAX; i++)
765                 ret +=  oh->oh_buckets[i];
766         return ret;
767 }
768 EXPORT_SYMBOL(lprocfs_oh_sum);
769
770 void lprocfs_oh_clear(struct obd_histogram *oh)
771 {
772         unsigned long flags;
773         spin_lock_irqsave(&oh->oh_lock, flags);
774         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
775         spin_unlock_irqrestore(&oh->oh_lock, flags);
776 }
777 EXPORT_SYMBOL(lprocfs_oh_clear);
778
779 #endif /* LPROCFS*/
780
781 EXPORT_SYMBOL(lprocfs_register);
782 EXPORT_SYMBOL(lprocfs_srch);
783 EXPORT_SYMBOL(lprocfs_remove);
784 EXPORT_SYMBOL(lprocfs_add_vars);
785 EXPORT_SYMBOL(lprocfs_obd_attach);
786 EXPORT_SYMBOL(lprocfs_obd_detach);
787 EXPORT_SYMBOL(lprocfs_alloc_stats);
788 EXPORT_SYMBOL(lprocfs_free_stats);
789 EXPORT_SYMBOL(lprocfs_register_stats);
790 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
791 EXPORT_SYMBOL(lprocfs_free_obd_stats);
792
793 EXPORT_SYMBOL(lprocfs_rd_u64);
794 EXPORT_SYMBOL(lprocfs_rd_uuid);
795 EXPORT_SYMBOL(lprocfs_rd_name);
796 EXPORT_SYMBOL(lprocfs_rd_fstype);
797 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
798 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
799 EXPORT_SYMBOL(lprocfs_rd_numrefs);
800
801 EXPORT_SYMBOL(lprocfs_rd_blksize);
802 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
803 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
804 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
805 EXPORT_SYMBOL(lprocfs_rd_filestotal);
806 EXPORT_SYMBOL(lprocfs_rd_filesfree);
807 EXPORT_SYMBOL(lprocfs_rd_filegroups);
808
809 EXPORT_SYMBOL(lprocfs_write_helper);
810 EXPORT_SYMBOL(lprocfs_write_u64_helper);