Whamcloud - gitweb
land b1_4_smallfix on b1_4(20050202_1817)
[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
149                 /* Memory corruption once caused this to fail, and
150                    without this LASSERT we would loop here forever. */
151                 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
152                          "0x%p  %s/%s len %d\n", rm_entry, temp->name,
153                          rm_entry->name, (int)strlen(rm_entry->name));
154
155                 remove_proc_entry(rm_entry->name, rm_entry->parent);
156                 if (temp == parent)
157                         break;
158         }
159 }
160
161 struct proc_dir_entry *lprocfs_register(const char *name,
162                                         struct proc_dir_entry *parent,
163                                         struct lprocfs_vars *list, void *data)
164 {
165         struct proc_dir_entry *newchild;
166
167         newchild = lprocfs_srch(parent, name);
168         if (newchild != NULL) {
169                 CERROR(" Lproc: Attempting to register %s more than once \n",
170                        name);
171                 return ERR_PTR(-EALREADY);
172         }
173
174         newchild = proc_mkdir(name, parent);
175         if (newchild != NULL && list != NULL) {
176                 int rc = lprocfs_add_vars(newchild, list, data);
177                 if (rc) {
178                         lprocfs_remove(newchild);
179                         return ERR_PTR(rc);
180                 }
181         }
182         return newchild;
183 }
184
185 /* Generic callbacks */
186
187 int lprocfs_rd_u64(char *page, char **start, off_t off,
188                    int count, int *eof, void *data)
189 {
190         LASSERT(data != NULL);
191         *eof = 1;
192         return snprintf(page, count, LPU64"\n", *(__u64 *)data);
193 }
194
195 int lprocfs_rd_uuid(char *page, char **start, off_t off, int count,
196                     int *eof, void *data)
197 {
198         struct obd_device *dev = (struct obd_device*)data;
199
200         LASSERT(dev != NULL);
201         *eof = 1;
202         return snprintf(page, count, "%s\n", dev->obd_uuid.uuid);
203 }
204
205 int lprocfs_rd_name(char *page, char **start, off_t off, int count,
206                     int *eof, void* data)
207 {
208         struct obd_device *dev = (struct obd_device *)data;
209
210         LASSERT(dev != NULL);
211         LASSERT(dev->obd_name != NULL);
212         *eof = 1;
213         return snprintf(page, count, "%s\n", dev->obd_name);
214 }
215
216 int lprocfs_rd_fstype(char *page, char **start, off_t off, int count, int *eof,
217                       void *data)
218 {
219         struct obd_device *obd = (struct obd_device *)data;
220
221         LASSERT(obd != NULL);
222         LASSERT(obd->obd_fsops != NULL);
223         LASSERT(obd->obd_fsops->fs_type != NULL);
224         return snprintf(page, count, "%s\n", obd->obd_fsops->fs_type);
225 }
226
227 int lprocfs_rd_blksize(char *page, char **start, off_t off, int count,
228                        int *eof, void *data)
229 {
230         struct obd_statfs osfs;
231         int rc = obd_statfs(data, &osfs, jiffies - HZ);
232         if (!rc) {
233                 *eof = 1;
234                 rc = snprintf(page, count, "%u\n", osfs.os_bsize);
235         }
236         return rc;
237 }
238
239 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off, int count,
240                            int *eof, void *data)
241 {
242         struct obd_statfs osfs;
243         int rc = obd_statfs(data, &osfs, jiffies - HZ);
244         if (!rc) {
245                 __u32 blk_size = osfs.os_bsize >> 10;
246                 __u64 result = osfs.os_blocks;
247
248                 while (blk_size >>= 1)
249                         result <<= 1;
250
251                 *eof = 1;
252                 rc = snprintf(page, count, LPU64"\n", result);
253         }
254         return rc;
255 }
256
257 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off, int count,
258                           int *eof, void *data)
259 {
260         struct obd_statfs osfs;
261         int rc = obd_statfs(data, &osfs, jiffies - HZ);
262         if (!rc) {
263                 __u32 blk_size = osfs.os_bsize >> 10;
264                 __u64 result = osfs.os_bfree;
265
266                 while (blk_size >>= 1)
267                         result <<= 1;
268
269                 *eof = 1;
270                 rc = snprintf(page, count, LPU64"\n", result);
271         }
272         return rc;
273 }
274
275 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off, int count,
276                            int *eof, void *data)
277 {
278         struct obd_statfs osfs;
279         int rc = obd_statfs(data, &osfs, jiffies - HZ);
280         if (!rc) {
281                 __u32 blk_size = osfs.os_bsize >> 10;
282                 __u64 result = osfs.os_bavail;
283
284                 while (blk_size >>= 1)
285                         result <<= 1;
286
287                 *eof = 1;
288                 rc = snprintf(page, count, LPU64"\n", result);
289         }
290         return rc;
291 }
292
293 int lprocfs_rd_filestotal(char *page, char **start, off_t off, int count,
294                           int *eof, void *data)
295 {
296         struct obd_statfs osfs;
297         int rc = obd_statfs(data, &osfs, jiffies - HZ);
298         if (!rc) {
299                 *eof = 1;
300                 rc = snprintf(page, count, LPU64"\n", osfs.os_files);
301         }
302
303         return rc;
304 }
305
306 int lprocfs_rd_filesfree(char *page, char **start, off_t off, int count,
307                          int *eof, void *data)
308 {
309         struct obd_statfs osfs;
310         int rc = obd_statfs(data, &osfs, jiffies - HZ);
311         if (!rc) {
312                 *eof = 1;
313                 rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
314         }
315         return rc;
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_num_exports(char *page, char **start, off_t off, int count,
347                            int *eof,  void *data)
348 {
349         struct obd_device *obd = (struct obd_device*)data;
350
351         LASSERT(obd != NULL);
352         *eof = 1;
353         return snprintf(page, count, "%u\n", obd->obd_num_exports);
354 }
355
356 int lprocfs_rd_numrefs(char *page, char **start, off_t off, int count,
357                        int *eof, void *data)
358 {
359         struct obd_type *class = (struct obd_type*) data;
360
361         LASSERT(class != NULL);
362         *eof = 1;
363         return snprintf(page, count, "%d\n", class->typ_refcnt);
364 }
365
366 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list)
367 {
368         int rc = 0;
369
370         LASSERT(obd != NULL);
371         LASSERT(obd->obd_type != NULL);
372         LASSERT(obd->obd_type->typ_procroot != NULL);
373
374         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
375                                                obd->obd_type->typ_procroot,
376                                                list, obd);
377         if (IS_ERR(obd->obd_proc_entry)) {
378                 rc = PTR_ERR(obd->obd_proc_entry);
379                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
380                 obd->obd_proc_entry = NULL;
381         }
382         return rc;
383 }
384
385 int lprocfs_obd_cleanup(struct obd_device *obd)
386 {
387         if (obd && obd->obd_proc_entry) {
388                 lprocfs_remove(obd->obd_proc_entry);
389                 obd->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(notify) +
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, postrecov);
616         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
617         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
618         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
619         LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
620         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
621         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
622         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
623         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
624         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
625         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
626         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
627         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
628         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw_async);
629         LPROCFS_OBD_OP_INIT(num_private_stats, stats, prep_async_page);
630         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_async_io);
631         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_group_io);
632         LPROCFS_OBD_OP_INIT(num_private_stats, stats, trigger_group_io);
633         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_async_flags);
634         LPROCFS_OBD_OP_INIT(num_private_stats, stats, teardown_async_page);
635         LPROCFS_OBD_OP_INIT(num_private_stats, stats, increase_kms);
636         LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
637         LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
638         LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
639         LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
640         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
641         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
642         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
643         LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
644         LPROCFS_OBD_OP_INIT(num_private_stats, stats, match);
645         LPROCFS_OBD_OP_INIT(num_private_stats, stats, change_cbdata);
646         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
647         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
648         LPROCFS_OBD_OP_INIT(num_private_stats, stats, san_preprw);
649         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
650         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
651         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_init);
652         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_finish);
653         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin);
654         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
655         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
656         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
657
658         for (i = num_private_stats; i < num_stats; i++) {
659                 /* If this LBUGs, it is likely that an obd
660                  * operation was added to struct obd_ops in
661                  * <linux/obd.h>, and that the corresponding line item
662                  * LPROCFS_OBD_OP_INIT(.., .., opname)
663                  * is missing from the list above. */
664                 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
665                         CERROR("Missing obd_stat initializer obd_op "
666                                "operation at offset %d. Aborting.\n",
667                                i - num_private_stats);
668                         LBUG();
669                 }
670         }
671         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
672         if (rc < 0) {
673                 lprocfs_free_stats(stats);
674         } else {
675                 obd->obd_stats  = stats;
676                 obd->obd_cntr_base = num_private_stats;
677         }
678         return rc;
679 }
680
681 void lprocfs_free_obd_stats(struct obd_device *obd)
682 {
683         struct lprocfs_stats *stats = obd->obd_stats;
684
685         if (stats != NULL) {
686                 obd->obd_stats = NULL;
687                 lprocfs_free_stats(stats);
688         }
689 }
690
691 int lprocfs_write_helper(const char *buffer, unsigned long count,
692                          int *val)
693 {
694         char kernbuf[20], *end;
695
696         if (count > (sizeof(kernbuf) - 1))
697                 return -EINVAL;
698
699         if (copy_from_user(kernbuf, buffer, count))
700                 return -EFAULT;
701
702         kernbuf[count] = '\0';
703
704         *val = simple_strtol(kernbuf, &end, 0);
705         if (kernbuf == end)
706                 return -EINVAL;
707
708         return 0;
709 }
710
711 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,__u64 *val)
712 {
713         char kernbuf[22], *end;
714
715         if (count > (sizeof(kernbuf) - 1))
716                 return -EINVAL;
717
718         if (copy_from_user(kernbuf, buffer, count))
719                 return -EFAULT;
720
721         kernbuf[count] = '\0';
722
723         if (kernbuf[0] == '-')
724                 *val = -simple_strtoull(kernbuf + 1, &end, 0);
725         else
726                 *val = simple_strtoull(kernbuf, &end, 0);
727         if (kernbuf == end)
728                 return -EINVAL;
729
730         return 0;
731 }
732
733 int lprocfs_obd_seq_create(struct obd_device *dev, char *name, mode_t mode,
734                            struct file_operations *seq_fops, void *data)
735 {
736         struct proc_dir_entry *entry;
737         ENTRY;
738
739         entry = create_proc_entry(name, mode, dev->obd_proc_entry);
740         if (entry == NULL)
741                 RETURN(-ENOMEM);
742         entry->proc_fops = seq_fops;
743         entry->data = data;
744
745         RETURN(0);
746 }
747 EXPORT_SYMBOL(lprocfs_obd_seq_create);
748
749 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
750 {
751         unsigned long flags;
752
753         if (value >= OBD_HIST_MAX)
754                 value = OBD_HIST_MAX - 1;
755
756         spin_lock_irqsave(&oh->oh_lock, flags);
757         oh->oh_buckets[value]++;
758         spin_unlock_irqrestore(&oh->oh_lock, flags);
759 }
760 EXPORT_SYMBOL(lprocfs_oh_tally);
761
762 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
763 {
764         unsigned int val;
765
766         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
767                 ;
768
769         lprocfs_oh_tally(oh, val);
770 }
771 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
772
773 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
774 {
775         unsigned long ret = 0;
776         int i;
777
778         for (i = 0; i < OBD_HIST_MAX; i++)
779                 ret +=  oh->oh_buckets[i];
780         return ret;
781 }
782 EXPORT_SYMBOL(lprocfs_oh_sum);
783
784 void lprocfs_oh_clear(struct obd_histogram *oh)
785 {
786         unsigned long flags;
787         spin_lock_irqsave(&oh->oh_lock, flags);
788         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
789         spin_unlock_irqrestore(&oh->oh_lock, flags);
790 }
791 EXPORT_SYMBOL(lprocfs_oh_clear);
792
793 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
794                                           int count, int *eof, void *data)
795 {
796         struct obd_device *obd = data;
797         int len = 0, n,
798                 connected = obd->obd_connected_clients,
799                 max_recoverable = obd->obd_max_recoverable_clients,
800                 recoverable = obd->obd_recoverable_clients,
801                 completed = max_recoverable - recoverable,
802                 queue_len = obd->obd_requests_queued_for_recovery,
803                 replayed = obd->obd_replayed_requests;
804         __u64 next_transno = obd->obd_next_recovery_transno;
805
806         LASSERT(obd != NULL);
807         *eof = 1;
808
809         n = snprintf(page, count, "status: ");
810         page += n; len += n; count -= n;
811         if (obd->obd_max_recoverable_clients == 0) {
812                 n = snprintf(page, count, "INACTIVE\n");
813                 return len + n;
814         }
815
816         /* sampled unlocked, but really... */
817         if (obd->obd_recovering == 0) {
818                 n = snprintf(page, count, "COMPLETE\n");
819                 page += n; len += n; count -= n;
820
821                 n = snprintf(page, count, "recovery_start: %lu\n",
822                              obd->obd_recovery_start);
823                 page += n; len += n; count -= n;
824                 n = snprintf(page, count, "recovery_end: %lu\n",
825                              obd->obd_recovery_end);
826                 page += n; len += n; count -= n;
827                 n = snprintf(page, count, "recovered_clients: %d\n",
828                              completed);
829                 page += n; len += n; count -= n;
830                 n = snprintf(page, count, "unrecovered_clients: %d\n",
831                              obd->obd_recoverable_clients);
832                 page += n; len += n; count -= n;
833                 n = snprintf(page, count, "last_transno: "LPD64"\n",
834                              next_transno - 1);
835                 page += n; len += n; count -= n;
836                 n = snprintf(page, count, "replayed_requests: %d\n", replayed);
837                 return len + n;
838         }
839
840         n = snprintf(page, count, "RECOVERING\n");
841         page += n; len += n; count -= n;
842         n = snprintf(page, count, "recovery_start: %lu\n",
843                      obd->obd_recovery_start);
844         page += n; len += n; count -= n;
845         n = snprintf(page, count, "connected_clients: %d/%d\n",
846                      connected, max_recoverable);
847         page += n; len += n; count -= n;
848         n = snprintf(page, count, "completed_clients: %d/%d\n",
849                      completed, max_recoverable);
850         page += n; len += n; count -= n;
851         n = snprintf(page, count, "replayed_requests: %d/??\n", replayed);
852         page += n; len += n; count -= n;
853         n = snprintf(page, count, "queued_requests: %d\n", queue_len);
854         page += n; len += n; count -= n;
855         n = snprintf(page, count, "next_transno: "LPD64"\n", next_transno);
856         return len + n;
857 }
858 EXPORT_SYMBOL(lprocfs_obd_rd_recovery_status);
859
860 #endif /* LPROCFS*/
861
862 EXPORT_SYMBOL(lprocfs_register);
863 EXPORT_SYMBOL(lprocfs_srch);
864 EXPORT_SYMBOL(lprocfs_remove);
865 EXPORT_SYMBOL(lprocfs_add_vars);
866 EXPORT_SYMBOL(lprocfs_obd_setup);
867 EXPORT_SYMBOL(lprocfs_obd_cleanup);
868 EXPORT_SYMBOL(lprocfs_alloc_stats);
869 EXPORT_SYMBOL(lprocfs_free_stats);
870 EXPORT_SYMBOL(lprocfs_register_stats);
871 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
872 EXPORT_SYMBOL(lprocfs_free_obd_stats);
873
874 EXPORT_SYMBOL(lprocfs_rd_u64);
875 EXPORT_SYMBOL(lprocfs_rd_uuid);
876 EXPORT_SYMBOL(lprocfs_rd_name);
877 EXPORT_SYMBOL(lprocfs_rd_fstype);
878 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
879 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
880 EXPORT_SYMBOL(lprocfs_rd_num_exports);
881 EXPORT_SYMBOL(lprocfs_rd_numrefs);
882
883 EXPORT_SYMBOL(lprocfs_rd_blksize);
884 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
885 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
886 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
887 EXPORT_SYMBOL(lprocfs_rd_filestotal);
888 EXPORT_SYMBOL(lprocfs_rd_filesfree);
889
890 EXPORT_SYMBOL(lprocfs_write_helper);
891 EXPORT_SYMBOL(lprocfs_write_u64_helper);