Whamcloud - gitweb
b=3048
[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_atomic(char *page, char **start, off_t off,
196                    int count, int *eof, void *data)
197 {
198         atomic_t *atom = (atomic_t *)data;
199         LASSERT(atom != NULL);
200         *eof = 1;
201         return snprintf(page, count, "%d\n", atomic_read(atom));
202 }
203
204 int lprocfs_rd_uuid(char *page, char **start, off_t off, int count,
205                     int *eof, void *data)
206 {
207         struct obd_device *dev = (struct obd_device*)data;
208
209         LASSERT(dev != NULL);
210         *eof = 1;
211         return snprintf(page, count, "%s\n", dev->obd_uuid.uuid);
212 }
213
214 int lprocfs_rd_name(char *page, char **start, off_t off, int count,
215                     int *eof, void* data)
216 {
217         struct obd_device *dev = (struct obd_device *)data;
218
219         LASSERT(dev != NULL);
220         LASSERT(dev->obd_name != NULL);
221         *eof = 1;
222         return snprintf(page, count, "%s\n", dev->obd_name);
223 }
224
225 int lprocfs_rd_fstype(char *page, char **start, off_t off, int count, int *eof,
226                       void *data)
227 {
228         struct obd_device *obd = (struct obd_device *)data;
229
230         LASSERT(obd != NULL);
231         LASSERT(obd->obd_fsops != NULL);
232         LASSERT(obd->obd_fsops->fs_type != NULL);
233         return snprintf(page, count, "%s\n", obd->obd_fsops->fs_type);
234 }
235
236 int lprocfs_rd_blksize(char *page, char **start, off_t off, int count,
237                        int *eof, void *data)
238 {
239         struct obd_statfs osfs;
240         int rc = obd_statfs(data, &osfs, jiffies - HZ);
241         if (!rc) {
242                 *eof = 1;
243                 rc = snprintf(page, count, "%u\n", osfs.os_bsize);
244         }
245         return rc;
246 }
247
248 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off, int count,
249                            int *eof, void *data)
250 {
251         struct obd_statfs osfs;
252         int rc = obd_statfs(data, &osfs, jiffies - HZ);
253         if (!rc) {
254                 __u32 blk_size = osfs.os_bsize >> 10;
255                 __u64 result = osfs.os_blocks;
256
257                 while (blk_size >>= 1)
258                         result <<= 1;
259
260                 *eof = 1;
261                 rc = snprintf(page, count, LPU64"\n", result);
262         }
263         return rc;
264 }
265
266 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off, int count,
267                           int *eof, void *data)
268 {
269         struct obd_statfs osfs;
270         int rc = obd_statfs(data, &osfs, jiffies - HZ);
271         if (!rc) {
272                 __u32 blk_size = osfs.os_bsize >> 10;
273                 __u64 result = osfs.os_bfree;
274
275                 while (blk_size >>= 1)
276                         result <<= 1;
277
278                 *eof = 1;
279                 rc = snprintf(page, count, LPU64"\n", result);
280         }
281         return rc;
282 }
283
284 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off, int count,
285                            int *eof, void *data)
286 {
287         struct obd_statfs osfs;
288         int rc = obd_statfs(data, &osfs, jiffies - HZ);
289         if (!rc) {
290                 __u32 blk_size = osfs.os_bsize >> 10;
291                 __u64 result = osfs.os_bavail;
292
293                 while (blk_size >>= 1)
294                         result <<= 1;
295
296                 *eof = 1;
297                 rc = snprintf(page, count, LPU64"\n", result);
298         }
299         return rc;
300 }
301
302 int lprocfs_rd_filestotal(char *page, char **start, off_t off, int count,
303                           int *eof, void *data)
304 {
305         struct obd_statfs osfs;
306         int rc = obd_statfs(data, &osfs, jiffies - HZ);
307         if (!rc) {
308                 *eof = 1;
309                 rc = snprintf(page, count, LPU64"\n", osfs.os_files);
310         }
311
312         return rc;
313 }
314
315 int lprocfs_rd_filesfree(char *page, char **start, off_t off, int count,
316                          int *eof, void *data)
317 {
318         struct obd_statfs osfs;
319         int rc = obd_statfs(data, &osfs, jiffies - HZ);
320         if (!rc) {
321                 *eof = 1;
322                 rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
323         }
324         return rc;
325 }
326
327 int lprocfs_rd_server_uuid(char *page, char **start, off_t off, int count,
328                            int *eof, void *data)
329 {
330         struct obd_device *obd = (struct obd_device *)data;
331         struct obd_import *imp;
332         char *imp_state_name = NULL;
333
334         LASSERT(obd != NULL);
335         imp = obd->u.cli.cl_import;
336         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
337         *eof = 1;
338         return snprintf(page, count, "%s\t%s\n",
339                         imp->imp_target_uuid.uuid, imp_state_name);
340 }
341
342 int lprocfs_rd_conn_uuid(char *page, char **start, off_t off, int count,
343                          int *eof,  void *data)
344 {
345         struct obd_device *obd = (struct obd_device*)data;
346         struct ptlrpc_connection *conn;
347
348         LASSERT(obd != NULL);
349         conn = obd->u.cli.cl_import->imp_connection;
350         LASSERT(conn != NULL);
351         *eof = 1;
352         return snprintf(page, count, "%s\n", conn->c_remote_uuid.uuid);
353 }
354
355 int lprocfs_rd_num_exports(char *page, char **start, off_t off, int count,
356                            int *eof,  void *data)
357 {
358         struct obd_device *obd = (struct obd_device*)data;
359
360         LASSERT(obd != NULL);
361         *eof = 1;
362         return snprintf(page, count, "%u\n", obd->obd_num_exports);
363 }
364
365 int lprocfs_rd_numrefs(char *page, char **start, off_t off, int count,
366                        int *eof, void *data)
367 {
368         struct obd_type *class = (struct obd_type*) data;
369
370         LASSERT(class != NULL);
371         *eof = 1;
372         return snprintf(page, count, "%d\n", class->typ_refcnt);
373 }
374
375 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list)
376 {
377         int rc = 0;
378
379         LASSERT(obd != NULL);
380         LASSERT(obd->obd_type != NULL);
381         LASSERT(obd->obd_type->typ_procroot != NULL);
382
383         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
384                                                obd->obd_type->typ_procroot,
385                                                list, obd);
386         if (IS_ERR(obd->obd_proc_entry)) {
387                 rc = PTR_ERR(obd->obd_proc_entry);
388                 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
389                 obd->obd_proc_entry = NULL;
390         }
391         return rc;
392 }
393
394 int lprocfs_obd_cleanup(struct obd_device *obd)
395 {
396         if (obd && obd->obd_proc_entry) {
397                 lprocfs_remove(obd->obd_proc_entry);
398                 obd->obd_proc_entry = NULL;
399         }
400         return 0;
401 }
402
403 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num)
404 {
405         struct lprocfs_stats *stats;
406         struct lprocfs_percpu *percpu;
407         unsigned int percpusize;
408         unsigned int i;
409
410         if (num == 0)
411                 return NULL;
412
413         OBD_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
414         if (stats == NULL)
415                 return NULL;
416
417         percpusize = L1_CACHE_ALIGN(offsetof(typeof(*percpu), lp_cntr[num]));
418         stats->ls_percpu_size = num_online_cpus() * percpusize;
419         OBD_ALLOC(stats->ls_percpu[0], stats->ls_percpu_size);
420         if (stats->ls_percpu[0] == NULL) {
421                 OBD_FREE(stats, offsetof(typeof(*stats),
422                                          ls_percpu[num_online_cpus()]));
423                 return NULL;
424         }
425
426         stats->ls_num = num;
427         for (i = 1; i < num_online_cpus(); i++)
428                 stats->ls_percpu[i] = (void *)(stats->ls_percpu[i - 1]) +
429                         percpusize;
430
431         return stats;
432 }
433
434 void lprocfs_free_stats(struct lprocfs_stats *stats)
435 {
436         if (stats->ls_num == 0)
437                 return;
438
439         OBD_FREE(stats->ls_percpu[0], stats->ls_percpu_size);
440         OBD_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
441 }
442
443 /* Reset counter under lock */
444 int lprocfs_counter_write(struct file *file, const char *buffer,
445                           unsigned long count, void *data)
446 {
447         /* not supported */
448         return 0;
449 }
450
451 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
452 {
453         struct lprocfs_stats *stats = p->private;
454         /* return 1st cpu location */
455         return (*pos >= stats->ls_num) ? NULL :
456                 &(stats->ls_percpu[0]->lp_cntr[*pos]);
457 }
458
459 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
460 {
461 }
462
463 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
464 {
465         struct lprocfs_stats *stats = p->private;
466         ++*pos;
467         return (*pos >= stats->ls_num) ? NULL :
468                 &(stats->ls_percpu[0]->lp_cntr[*pos]);
469 }
470
471 /* seq file export of one lprocfs counter */
472 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
473 {
474        struct lprocfs_stats *stats = p->private;
475        struct lprocfs_counter  *cntr = v;
476        struct lprocfs_counter  t, ret = { .lc_min = ~(__u64)0 };
477        int i, idx, rc;
478
479        if (cntr == &(stats->ls_percpu[0])->lp_cntr[0]) {
480                struct timeval now;
481                do_gettimeofday(&now);
482                rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
483                                "snapshot_time", now.tv_sec, now.tv_usec);
484                if (rc < 0)
485                        return rc;
486        }
487        idx = cntr - &(stats->ls_percpu[0])->lp_cntr[0];
488
489        for (i = 0; i < num_online_cpus(); i++) {
490                struct lprocfs_counter *percpu_cntr =
491                        &(stats->ls_percpu[i])->lp_cntr[idx];
492                int centry;
493
494                do {
495                        centry = atomic_read(&percpu_cntr->lc_cntl.la_entry);
496                        t.lc_count = percpu_cntr->lc_count;
497                        t.lc_sum = percpu_cntr->lc_sum;
498                        t.lc_min = percpu_cntr->lc_min;
499                        t.lc_max = percpu_cntr->lc_max;
500                        t.lc_sumsquare = percpu_cntr->lc_sumsquare;
501                } while (centry != atomic_read(&percpu_cntr->lc_cntl.la_entry) &&
502                         centry != atomic_read(&percpu_cntr->lc_cntl.la_exit));
503                ret.lc_count += t.lc_count;
504                ret.lc_sum += t.lc_sum;
505                if (t.lc_min < ret.lc_min)
506                        ret.lc_min = t.lc_min;
507                if (t.lc_max > ret.lc_max)
508                        ret.lc_max = t.lc_max;
509                ret.lc_sumsquare += t.lc_sumsquare;
510        }
511
512        rc = seq_printf(p, "%-25s "LPU64" samples [%s]", cntr->lc_name,
513                        ret.lc_count, cntr->lc_units);
514        if (rc < 0)
515                goto out;
516
517        if ((cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) && (ret.lc_count > 0)) {
518                rc = seq_printf(p, " "LPU64" "LPU64" "LPU64,
519                                ret.lc_min, ret.lc_max, ret.lc_sum);
520                if (rc < 0)
521                        goto out;
522                if (cntr->lc_config & LPROCFS_CNTR_STDDEV)
523                        rc = seq_printf(p, " "LPU64, ret.lc_sumsquare);
524                if (rc < 0)
525                        goto out;
526        }
527        rc = seq_printf(p, "\n");
528  out:
529        return (rc < 0) ? rc : 0;
530 }
531
532 struct seq_operations lprocfs_stats_seq_sops = {
533         start: lprocfs_stats_seq_start,
534         stop:  lprocfs_stats_seq_stop,
535         next:  lprocfs_stats_seq_next,
536         show:  lprocfs_stats_seq_show,
537 };
538
539 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
540 {
541         struct proc_dir_entry *dp = PDE(inode);
542         struct seq_file *seq;
543         int rc;
544
545         rc = seq_open(file, &lprocfs_stats_seq_sops);
546         if (rc)
547                 return rc;
548         seq = file->private_data;
549         seq->private = dp->data;
550         return 0;
551 }
552
553 struct file_operations lprocfs_stats_seq_fops = {
554         .owner   = THIS_MODULE,
555         .open    = lprocfs_stats_seq_open,
556         .read    = seq_read,
557         .llseek  = seq_lseek,
558         .release = seq_release,
559 };
560
561 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
562                            struct lprocfs_stats *stats)
563 {
564         struct proc_dir_entry *entry;
565         LASSERT(root != NULL);
566
567         entry = create_proc_entry(name, 0444, root);
568         if (entry == NULL)
569                 return -ENOMEM;
570         entry->proc_fops = &lprocfs_stats_seq_fops;
571         entry->data = (void *)stats;
572         entry->write_proc = lprocfs_counter_write;
573         return 0;
574 }
575
576 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
577                           unsigned conf, const char *name, const char *units)
578 {
579         struct lprocfs_counter *c;
580         int i;
581
582         LASSERT(stats != NULL);
583         for (i = 0; i < num_online_cpus(); i++) {
584                 c = &(stats->ls_percpu[i]->lp_cntr[index]);
585                 c->lc_config = conf;
586                 c->lc_min = ~(__u64)0;
587                 c->lc_name = name;
588                 c->lc_units = units;
589         }
590 }
591 EXPORT_SYMBOL(lprocfs_counter_init);
592
593 #define LPROCFS_OBD_OP_INIT(base, stats, op)                               \
594 do {                                                                       \
595         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);              \
596         LASSERT(coffset < stats->ls_num);                                  \
597         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");              \
598 } while (0)
599
600 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
601 {
602         struct lprocfs_stats *stats;
603         unsigned int num_stats;
604         int rc, i;
605
606         LASSERT(obd->obd_stats == NULL);
607         LASSERT(obd->obd_proc_entry != NULL);
608         LASSERT(obd->obd_cntr_base == 0);
609
610         num_stats = 1 + OBD_COUNTER_OFFSET(notify) +
611                 num_private_stats;
612         stats = lprocfs_alloc_stats(num_stats);
613         if (stats == NULL)
614                 return -ENOMEM;
615
616         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
617         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
618         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info);
619         LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
620         LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
621         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
622         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
623         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
624         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
625         LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
626         LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
627         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
628         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
629         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
630         LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
631         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
632         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
633         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
634         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
635         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
636         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
637         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
638         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
639         LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw_async);
640         LPROCFS_OBD_OP_INIT(num_private_stats, stats, prep_async_page);
641         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_async_io);
642         LPROCFS_OBD_OP_INIT(num_private_stats, stats, queue_group_io);
643         LPROCFS_OBD_OP_INIT(num_private_stats, stats, trigger_group_io);
644         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_async_flags);
645         LPROCFS_OBD_OP_INIT(num_private_stats, stats, teardown_async_page);
646         LPROCFS_OBD_OP_INIT(num_private_stats, stats, adjust_kms);
647         LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
648         LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
649         LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
650         LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
651         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
652         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
653         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
654         LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
655         LPROCFS_OBD_OP_INIT(num_private_stats, stats, match);
656         LPROCFS_OBD_OP_INIT(num_private_stats, stats, change_cbdata);
657         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
658         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
659         LPROCFS_OBD_OP_INIT(num_private_stats, stats, join_lru);
660         LPROCFS_OBD_OP_INIT(num_private_stats, stats, san_preprw);
661         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
662         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
663         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_init);
664         LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_finish);
665         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin);
666         LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
667         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
668         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
669
670         for (i = num_private_stats; i < num_stats; i++) {
671                 /* If this LBUGs, it is likely that an obd
672                  * operation was added to struct obd_ops in
673                  * <linux/obd.h>, and that the corresponding line item
674                  * LPROCFS_OBD_OP_INIT(.., .., opname)
675                  * is missing from the list above. */
676                 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
677                         CERROR("Missing obd_stat initializer obd_op "
678                                "operation at offset %d. Aborting.\n",
679                                i - num_private_stats);
680                         LBUG();
681                 }
682         }
683         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
684         if (rc < 0) {
685                 lprocfs_free_stats(stats);
686         } else {
687                 obd->obd_stats  = stats;
688                 obd->obd_cntr_base = num_private_stats;
689         }
690         return rc;
691 }
692
693 void lprocfs_free_obd_stats(struct obd_device *obd)
694 {
695         struct lprocfs_stats *stats = obd->obd_stats;
696
697         if (stats != NULL) {
698                 obd->obd_stats = NULL;
699                 lprocfs_free_stats(stats);
700         }
701 }
702
703 int lprocfs_write_helper(const char *buffer, unsigned long count,
704                          int *val)
705 {
706         char kernbuf[20], *end;
707
708         if (count > (sizeof(kernbuf) - 1))
709                 return -EINVAL;
710
711         if (copy_from_user(kernbuf, buffer, count))
712                 return -EFAULT;
713
714         kernbuf[count] = '\0';
715
716         *val = simple_strtol(kernbuf, &end, 0);
717         if (kernbuf == end)
718                 return -EINVAL;
719
720         return 0;
721 }
722
723 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,__u64 *val)
724 {
725         char kernbuf[22], *end;
726
727         if (count > (sizeof(kernbuf) - 1))
728                 return -EINVAL;
729
730         if (copy_from_user(kernbuf, buffer, count))
731                 return -EFAULT;
732
733         kernbuf[count] = '\0';
734
735         if (kernbuf[0] == '-')
736                 *val = -simple_strtoull(kernbuf + 1, &end, 0);
737         else
738                 *val = simple_strtoull(kernbuf, &end, 0);
739         if (kernbuf == end)
740                 return -EINVAL;
741
742         return 0;
743 }
744
745 int lprocfs_obd_seq_create(struct obd_device *dev, char *name, mode_t mode,
746                            struct file_operations *seq_fops, void *data)
747 {
748         struct proc_dir_entry *entry;
749         ENTRY;
750
751         entry = create_proc_entry(name, mode, dev->obd_proc_entry);
752         if (entry == NULL)
753                 RETURN(-ENOMEM);
754         entry->proc_fops = seq_fops;
755         entry->data = data;
756
757         RETURN(0);
758 }
759 EXPORT_SYMBOL(lprocfs_obd_seq_create);
760
761 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
762 {
763         unsigned long flags;
764
765         if (value >= OBD_HIST_MAX)
766                 value = OBD_HIST_MAX - 1;
767
768         spin_lock_irqsave(&oh->oh_lock, flags);
769         oh->oh_buckets[value]++;
770         spin_unlock_irqrestore(&oh->oh_lock, flags);
771 }
772 EXPORT_SYMBOL(lprocfs_oh_tally);
773
774 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
775 {
776         unsigned int val;
777
778         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
779                 ;
780
781         lprocfs_oh_tally(oh, val);
782 }
783 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
784
785 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
786 {
787         unsigned long ret = 0;
788         int i;
789
790         for (i = 0; i < OBD_HIST_MAX; i++)
791                 ret +=  oh->oh_buckets[i];
792         return ret;
793 }
794 EXPORT_SYMBOL(lprocfs_oh_sum);
795
796 void lprocfs_oh_clear(struct obd_histogram *oh)
797 {
798         unsigned long flags;
799         spin_lock_irqsave(&oh->oh_lock, flags);
800         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
801         spin_unlock_irqrestore(&oh->oh_lock, flags);
802 }
803 EXPORT_SYMBOL(lprocfs_oh_clear);
804
805 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
806                                           int count, int *eof, void *data)
807 {
808         struct obd_device *obd = data;
809         int len = 0, n,
810                 connected = obd->obd_connected_clients,
811                 max_recoverable = obd->obd_max_recoverable_clients,
812                 recoverable = obd->obd_recoverable_clients,
813                 completed = max_recoverable - recoverable,
814                 queue_len = obd->obd_requests_queued_for_recovery,
815                 replayed = obd->obd_replayed_requests;
816         __u64 next_transno = obd->obd_next_recovery_transno;
817
818         LASSERT(obd != NULL);
819         *eof = 1;
820
821         n = snprintf(page, count, "status: ");
822         page += n; len += n; count -= n;
823         if (obd->obd_max_recoverable_clients == 0) {
824                 n = snprintf(page, count, "INACTIVE\n");
825                 return len + n;
826         }
827
828         /* sampled unlocked, but really... */
829         if (obd->obd_recovering == 0) {
830                 n = snprintf(page, count, "COMPLETE\n");
831                 page += n; len += n; count -= n;
832
833                 n = snprintf(page, count, "recovery_start: %lu\n",
834                              obd->obd_recovery_start);
835                 page += n; len += n; count -= n;
836                 n = snprintf(page, count, "recovery_end: %lu\n",
837                              obd->obd_recovery_end);
838                 page += n; len += n; count -= n;
839                 n = snprintf(page, count, "recovered_clients: %d\n",
840                              completed);
841                 page += n; len += n; count -= n;
842                 n = snprintf(page, count, "unrecovered_clients: %d\n",
843                              obd->obd_recoverable_clients);
844                 page += n; len += n; count -= n;
845                 n = snprintf(page, count, "last_transno: "LPD64"\n",
846                              next_transno - 1);
847                 page += n; len += n; count -= n;
848                 n = snprintf(page, count, "replayed_requests: %d\n", replayed);
849                 return len + n;
850         }
851
852         n = snprintf(page, count, "RECOVERING\n");
853         page += n; len += n; count -= n;
854         n = snprintf(page, count, "recovery_start: %lu\n",
855                      obd->obd_recovery_start);
856         page += n; len += n; count -= n;
857         n = snprintf(page, count, "connected_clients: %d/%d\n",
858                      connected, max_recoverable);
859         page += n; len += n; count -= n;
860         n = snprintf(page, count, "completed_clients: %d/%d\n",
861                      completed, max_recoverable);
862         page += n; len += n; count -= n;
863         n = snprintf(page, count, "replayed_requests: %d/??\n", replayed);
864         page += n; len += n; count -= n;
865         n = snprintf(page, count, "queued_requests: %d\n", queue_len);
866         page += n; len += n; count -= n;
867         n = snprintf(page, count, "next_transno: "LPD64"\n", next_transno);
868         return len + n;
869 }
870 EXPORT_SYMBOL(lprocfs_obd_rd_recovery_status);
871
872 #endif /* LPROCFS*/
873
874 EXPORT_SYMBOL(lprocfs_register);
875 EXPORT_SYMBOL(lprocfs_srch);
876 EXPORT_SYMBOL(lprocfs_remove);
877 EXPORT_SYMBOL(lprocfs_add_vars);
878 EXPORT_SYMBOL(lprocfs_obd_setup);
879 EXPORT_SYMBOL(lprocfs_obd_cleanup);
880 EXPORT_SYMBOL(lprocfs_alloc_stats);
881 EXPORT_SYMBOL(lprocfs_free_stats);
882 EXPORT_SYMBOL(lprocfs_register_stats);
883 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
884 EXPORT_SYMBOL(lprocfs_free_obd_stats);
885
886 EXPORT_SYMBOL(lprocfs_rd_u64);
887 EXPORT_SYMBOL(lprocfs_rd_atomic);
888 EXPORT_SYMBOL(lprocfs_rd_uuid);
889 EXPORT_SYMBOL(lprocfs_rd_name);
890 EXPORT_SYMBOL(lprocfs_rd_fstype);
891 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
892 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
893 EXPORT_SYMBOL(lprocfs_rd_num_exports);
894 EXPORT_SYMBOL(lprocfs_rd_numrefs);
895
896 EXPORT_SYMBOL(lprocfs_rd_blksize);
897 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
898 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
899 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
900 EXPORT_SYMBOL(lprocfs_rd_filestotal);
901 EXPORT_SYMBOL(lprocfs_rd_filesfree);
902
903 EXPORT_SYMBOL(lprocfs_write_helper);
904 EXPORT_SYMBOL(lprocfs_write_u64_helper);