diff options
author | Prakash Surya <[email protected]> | 2013-09-12 16:14:51 -0700 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2013-10-16 14:48:35 -0700 |
commit | 56d40a686b0504042a088943154ba7b0dde36d51 (patch) | |
tree | 5d6d397d8e310874592909a7c17b453bb375b52c /module/spl/spl-kstat.c | |
parent | 4768c0d0a61497f2f3a52678154f2742a355618f (diff) |
Add callbacks for displaying KSTAT_TYPE_RAW kstats
The current implementation for displaying kstats of type KSTAT_TYPE_RAW
is rather crude. This patch attempts to enhance this handling by
allowing a kstat user to register formatting callbacks which can
optionally be used.
The callbacks allow the user to implement functions for interpreting
their data and transposing it into a character buffer. This buffer,
containing a string representation of the raw data, is then be displayed
through the current /proc textual interface.
Additionally the kstats are made writable because it's now possible
to provide a useful handler via the existing ks_update() interface.
Signed-off-by: Prakash Surya <[email protected]>
Signed-off-by: Brian Behlendorf <[email protected]>
Issue #296
Diffstat (limited to 'module/spl/spl-kstat.c')
-rw-r--r-- | module/spl/spl-kstat.c | 119 |
1 files changed, 105 insertions, 14 deletions
diff --git a/module/spl/spl-kstat.c b/module/spl/spl-kstat.c index 4e900c066..57aabe2d0 100644 --- a/module/spl/spl-kstat.c +++ b/module/spl/spl-kstat.c @@ -41,10 +41,25 @@ static kmutex_t kstat_module_lock; static struct list_head kstat_module_list; static kid_t kstat_id; -static void +static int +kstat_resize_raw(kstat_t *ksp) +{ + if (ksp->ks_raw_bufsize == KSTAT_RAW_MAX) + return ENOMEM; + + vmem_free(ksp->ks_raw_buf, ksp->ks_raw_bufsize); + ksp->ks_raw_bufsize = MIN(ksp->ks_raw_bufsize * 2, KSTAT_RAW_MAX); + ksp->ks_raw_buf = vmem_alloc(ksp->ks_raw_bufsize, KM_SLEEP); + + return 0; +} + +static int kstat_seq_show_headers(struct seq_file *f) { kstat_t *ksp = (kstat_t *)f->private; + int rc = 0; + ASSERT(ksp->ks_magic == KS_MAGIC); seq_printf(f, "%d %d 0x%02x %d %d %lld %lld\n", @@ -54,7 +69,17 @@ kstat_seq_show_headers(struct seq_file *f) switch (ksp->ks_type) { case KSTAT_TYPE_RAW: - seq_printf(f, "raw data"); +restart: + if (ksp->ks_raw_ops.headers) { + rc = ksp->ks_raw_ops.headers( + ksp->ks_raw_buf, ksp->ks_raw_bufsize); + if (rc == ENOMEM && !kstat_resize_raw(ksp)) + goto restart; + if (!rc) + seq_puts(f, ksp->ks_raw_buf); + } else { + seq_printf(f, "raw data\n"); + } break; case KSTAT_TYPE_NAMED: seq_printf(f, "%-31s %-4s %s\n", @@ -92,6 +117,8 @@ kstat_seq_show_headers(struct seq_file *f) default: PANIC("Undefined kstat type %d\n", ksp->ks_type); } + + return -rc; } static int @@ -232,9 +259,19 @@ kstat_seq_show(struct seq_file *f, void *p) switch (ksp->ks_type) { case KSTAT_TYPE_RAW: - ASSERT(ksp->ks_ndata == 1); - rc = kstat_seq_show_raw(f, ksp->ks_data, - ksp->ks_data_size); +restart: + if (ksp->ks_raw_ops.data) { + rc = ksp->ks_raw_ops.data( + ksp->ks_raw_buf, ksp->ks_raw_bufsize, p); + if (rc == ENOMEM && !kstat_resize_raw(ksp)) + goto restart; + if (!rc) + seq_puts(f, ksp->ks_raw_buf); + } else { + ASSERT(ksp->ks_ndata == 1); + rc = kstat_seq_show_raw(f, ksp->ks_data, + ksp->ks_data_size); + } break; case KSTAT_TYPE_NAMED: rc = kstat_seq_show_named(f, (kstat_named_t *)p); @@ -255,13 +292,17 @@ kstat_seq_show(struct seq_file *f, void *p) PANIC("Undefined kstat type %d\n", ksp->ks_type); } - return rc; + return -rc; } int kstat_default_update(kstat_t *ksp, int rw) { ASSERT(ksp != NULL); + + if (rw == KSTAT_WRITE) + return (EACCES); + return 0; } @@ -273,7 +314,10 @@ kstat_seq_data_addr(kstat_t *ksp, loff_t n) switch (ksp->ks_type) { case KSTAT_TYPE_RAW: - rc = ksp->ks_data; + if (ksp->ks_raw_ops.addr) + rc = ksp->ks_raw_ops.addr(ksp, n); + else + rc = ksp->ks_data; break; case KSTAT_TYPE_NAMED: rc = ksp->ks_data + n * sizeof(kstat_named_t); @@ -307,13 +351,18 @@ kstat_seq_start(struct seq_file *f, loff_t *pos) mutex_enter(&ksp->ks_lock); + if (ksp->ks_type == KSTAT_TYPE_RAW) { + ksp->ks_raw_bufsize = PAGE_SIZE; + ksp->ks_raw_buf = vmem_alloc(ksp->ks_raw_bufsize, KM_SLEEP); + } + /* Dynamically update kstat, on error existing kstats are used */ (void) ksp->ks_update(ksp, KSTAT_READ); ksp->ks_snaptime = gethrtime(); - if (!n) - kstat_seq_show_headers(f); + if (!n && kstat_seq_show_headers(f)) + SRETURN(NULL); if (n >= ksp->ks_ndata) SRETURN(NULL); @@ -341,6 +390,9 @@ kstat_seq_stop(struct seq_file *f, void *v) kstat_t *ksp = (kstat_t *)f->private; ASSERT(ksp->ks_magic == KS_MAGIC); + if (ksp->ks_type == KSTAT_TYPE_RAW) + vmem_free(ksp->ks_raw_buf, ksp->ks_raw_bufsize); + mutex_exit(&ksp->ks_lock); } @@ -408,13 +460,47 @@ proc_kstat_open(struct inode *inode, struct file *filp) return rc; } +static ssize_t +proc_kstat_write(struct file *filp, const char __user *buf, + size_t len, loff_t *ppos) +{ + struct seq_file *f = filp->private_data; + kstat_t *ksp = f->private; + int rc; + + ASSERT(ksp->ks_magic == KS_MAGIC); + + mutex_enter(ksp->ks_lock); + rc = ksp->ks_update(ksp, KSTAT_WRITE); + mutex_exit(ksp->ks_lock); + + if (rc) + return (-rc); + + *ppos += len; + return (len); +} + static struct file_operations proc_kstat_operations = { - .open = proc_kstat_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, + .open = proc_kstat_open, + .write = proc_kstat_write, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, }; +void +__kstat_set_raw_ops(kstat_t *ksp, + int (*headers)(char *buf, size_t size), + int (*data)(char *buf, size_t size, void *data), + void *(*addr)(kstat_t *ksp, loff_t index)) +{ + ksp->ks_raw_ops.headers = headers; + ksp->ks_raw_ops.data = data; + ksp->ks_raw_ops.addr = addr; +} +EXPORT_SYMBOL(__kstat_set_raw_ops); + kstat_t * __kstat_create(const char *ks_module, int ks_instance, const char *ks_name, const char *ks_class, uchar_t ks_type, uint_t ks_ndata, @@ -453,6 +539,11 @@ __kstat_create(const char *ks_module, int ks_instance, const char *ks_name, ksp->ks_flags = ks_flags; ksp->ks_update = kstat_default_update; ksp->ks_private = NULL; + ksp->ks_raw_ops.headers = NULL; + ksp->ks_raw_ops.data = NULL; + ksp->ks_raw_ops.addr = NULL; + ksp->ks_raw_buf = NULL; + ksp->ks_raw_bufsize = 0; switch (ksp->ks_type) { case KSTAT_TYPE_RAW: @@ -526,7 +617,7 @@ __kstat_install(kstat_t *ksp) mutex_enter(&ksp->ks_lock); ksp->ks_owner = module; - ksp->ks_proc = proc_create_data(ksp->ks_name, 0444, + ksp->ks_proc = proc_create_data(ksp->ks_name, 0644, module->ksm_proc, &proc_kstat_operations, (void *)ksp); if (ksp->ks_proc == NULL) { list_del_init(&ksp->ks_list); |