]> git.proxmox.com Git - mirror_spl.git/blobdiff - module/spl/spl-kstat.c
Avoid WARN() from procfs on kstat collision
[mirror_spl.git] / module / spl / spl-kstat.c
index bb6e9a5042eb75a59e05e1c527e4c790945433e6..1b6a7df9b34858cf4bc3d256da7770588c75685f 100644 (file)
-/*
- *  This file is part of the SPL: Solaris Porting Layer.
- *
- *  Copyright (c) 2008 Lawrence Livermore National Security, LLC.
- *  Produced at Lawrence Livermore National Laboratory
- *  Written by:
- *          Brian Behlendorf <behlendorf1@llnl.gov>,
- *          Herb Wartens <wartens2@llnl.gov>,
- *          Jim Garlick <garlick@llnl.gov>
+/*****************************************************************************\
+ *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
+ *  Copyright (C) 2007 The Regents of the University of California.
+ *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
+ *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
  *  UCRL-CODE-235197
  *
- *  This is free software; you can redistribute it and/or modify it
- *  under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ *  This file is part of the SPL, Solaris Porting Layer.
+ *  For details, see <http://zfsonlinux.org/>.
+ *
+ *  The SPL is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License as published by the
+ *  Free Software Foundation; either version 2 of the License, or (at your
+ *  option) any later version.
  *
- *  This is distributed in the hope that it will be useful, but WITHOUT
+ *  The SPL is distributed in the hope that it will be useful, but WITHOUT
  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  *  for more details.
  *
  *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
- */
+ *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
+ *****************************************************************************
+ *  Solaris Porting Layer (SPL) Kstat Implementation.
+\*****************************************************************************/
 
+#include <linux/seq_file.h>
 #include <sys/kstat.h>
+#include <sys/vmem.h>
+#include <sys/cmn_err.h>
 
-#ifdef DEBUG_KSTAT
+#ifndef HAVE_PDE_DATA
+#define PDE_DATA(x) (PDE(x)->data)
+#endif
 
-static spinlock_t kstat_lock;
-static struct list_head kstat_list;
+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;
+}
+
+void
+kstat_waitq_enter(kstat_io_t *kiop)
+{
+       hrtime_t new, delta;
+       ulong_t wcnt;
+
+       new = gethrtime();
+       delta = new - kiop->wlastupdate;
+       kiop->wlastupdate = new;
+       wcnt = kiop->wcnt++;
+       if (wcnt != 0) {
+               kiop->wlentime += delta * wcnt;
+               kiop->wtime += delta;
+       }
+}
+EXPORT_SYMBOL(kstat_waitq_enter);
+
+void
+kstat_waitq_exit(kstat_io_t *kiop)
+{
+       hrtime_t new, delta;
+       ulong_t wcnt;
+
+       new = gethrtime();
+       delta = new - kiop->wlastupdate;
+       kiop->wlastupdate = new;
+       wcnt = kiop->wcnt--;
+       ASSERT((int)wcnt > 0);
+       kiop->wlentime += delta * wcnt;
+       kiop->wtime += delta;
+}
+EXPORT_SYMBOL(kstat_waitq_exit);
+
+void
+kstat_runq_enter(kstat_io_t *kiop)
+{
+       hrtime_t new, delta;
+       ulong_t rcnt;
+
+       new = gethrtime();
+       delta = new - kiop->rlastupdate;
+       kiop->rlastupdate = new;
+       rcnt = kiop->rcnt++;
+       if (rcnt != 0) {
+               kiop->rlentime += delta * rcnt;
+               kiop->rtime += delta;
+       }
+}
+EXPORT_SYMBOL(kstat_runq_enter);
+
+void
+kstat_runq_exit(kstat_io_t *kiop)
+{
+       hrtime_t new, delta;
+       ulong_t rcnt;
+
+       new = gethrtime();
+       delta = new - kiop->rlastupdate;
+       kiop->rlastupdate = new;
+       rcnt = kiop->rcnt--;
+       ASSERT((int)rcnt > 0);
+       kiop->rlentime += delta * rcnt;
+       kiop->rtime += delta;
+}
+EXPORT_SYMBOL(kstat_runq_exit);
+
+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",
@@ -45,7 +131,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",
@@ -73,8 +169,10 @@ kstat_seq_show_headers(struct seq_file *f)
                                    "min", "max", "start", "stop");
                         break;
                 default:
-                        SBUG(); /* Unreachable */
+                        PANIC("Undefined kstat type %d\n", ksp->ks_type);
         }
+
+       return -rc;
 }
 
 static int
@@ -136,7 +234,7 @@ kstat_seq_show_named(struct seq_file *f, kstat_named_t *knp)
                         seq_printf(f, "%s", KSTAT_NAMED_STR_PTR(knp));
                         break;
                 default:
-                        SBUG(); /* Unreachable */
+                        PANIC("Undefined kstat data type %d\n", knp->data_type);
         }
 
         seq_printf(f, "\n");
@@ -194,9 +292,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);
@@ -211,21 +319,34 @@ kstat_seq_show(struct seq_file *f, void *p)
                         rc = kstat_seq_show_timer(f, (kstat_timer_t *)p);
                         break;
                 default:
-                        SBUG(); /* Unreachable */
+                        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;
 }
 
 static void *
 kstat_seq_data_addr(kstat_t *ksp, loff_t n)
 {
         void *rc = NULL;
-        ENTRY;
 
        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);
@@ -240,10 +361,10 @@ kstat_seq_data_addr(kstat_t *ksp, loff_t n)
                         rc = ksp->ks_data + n * sizeof(kstat_timer_t);
                         break;
                 default:
-                        SBUG(); /* Unreachable */
+                        PANIC("Undefined kstat type %d\n", ksp->ks_type);
         }
 
-        RETURN(rc);
+        return (rc);
 }
 
 static void *
@@ -252,18 +373,26 @@ kstat_seq_start(struct seq_file *f, loff_t *pos)
         loff_t n = *pos;
         kstat_t *ksp = (kstat_t *)f->private;
         ASSERT(ksp->ks_magic == KS_MAGIC);
-        ENTRY;
 
-        spin_lock(&ksp->ks_lock);
+       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))
+               return (NULL);
 
         if (n >= ksp->ks_ndata)
-                RETURN(NULL);
+                return (NULL);
 
-        RETURN(kstat_seq_data_addr(ksp, n));
+        return (kstat_seq_data_addr(ksp, n));
 }
 
 static void *
@@ -271,22 +400,24 @@ kstat_seq_next(struct seq_file *f, void *p, loff_t *pos)
 {
         kstat_t *ksp = (kstat_t *)f->private;
         ASSERT(ksp->ks_magic == KS_MAGIC);
-        ENTRY;
 
         ++*pos;
         if (*pos >= ksp->ks_ndata)
-                RETURN(NULL);
+                return (NULL);
 
-        RETURN(kstat_seq_data_addr(ksp, *pos));
+        return (kstat_seq_data_addr(ksp, *pos));
 }
 
 static void
 kstat_seq_stop(struct seq_file *f, void *v)
 {
-        kstat_t *ksp = (kstat_t *)f->private;
-        ASSERT(ksp->ks_magic == KS_MAGIC);
+       kstat_t *ksp = (kstat_t *)f->private;
+       ASSERT(ksp->ks_magic == KS_MAGIC);
 
-        spin_unlock(&ksp->ks_lock);
+       if (ksp->ks_type == KSTAT_TYPE_RAW)
+               vmem_free(ksp->ks_raw_buf, ksp->ks_raw_bufsize);
+
+       mutex_exit(ksp->ks_lock);
 }
 
 static struct seq_operations kstat_seq_ops = {
@@ -296,6 +427,47 @@ static struct seq_operations kstat_seq_ops = {
         .stop  = kstat_seq_stop,
 };
 
+static kstat_module_t *
+kstat_find_module(char *name)
+{
+       kstat_module_t *module;
+
+       list_for_each_entry(module, &kstat_module_list, ksm_module_list)
+               if (strncmp(name, module->ksm_name, KSTAT_STRLEN) == 0)
+                       return (module);
+
+       return (NULL);
+}
+
+static kstat_module_t *
+kstat_create_module(char *name)
+{
+       kstat_module_t *module;
+       struct proc_dir_entry *pde;
+
+       pde = proc_mkdir(name, proc_spl_kstat);
+       if (pde == NULL)
+               return (NULL);
+
+       module = kmem_alloc(sizeof (kstat_module_t), KM_SLEEP);
+       module->ksm_proc = pde;
+       strlcpy(module->ksm_name, name, KSTAT_STRLEN+1);
+       INIT_LIST_HEAD(&module->ksm_kstat_list);
+       list_add_tail(&module->ksm_module_list, &kstat_module_list);
+
+       return (module);
+
+}
+
+static void
+kstat_delete_module(kstat_module_t *module)
+{
+       ASSERT(list_empty(&module->ksm_kstat_list));
+       remove_proc_entry(module->ksm_name, proc_spl_kstat);
+       list_del(&module->ksm_module_list);
+       kmem_free(module, sizeof(kstat_module_t));
+}
+
 static int
 proc_kstat_open(struct inode *inode, struct file *filp)
 {
@@ -307,18 +479,52 @@ proc_kstat_open(struct inode *inode, struct file *filp)
                 return rc;
 
         f = filp->private_data;
-        f->private = PDE(inode)->data;
+        f->private = PDE_DATA(inode);
 
         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,
@@ -338,13 +544,14 @@ __kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
        if (ksp == NULL)
                return ksp;
 
-       spin_lock(&kstat_lock);
+       mutex_enter(&kstat_module_lock);
        ksp->ks_kid = kstat_id;
         kstat_id++;
-       spin_unlock(&kstat_lock);
+       mutex_exit(&kstat_module_lock);
 
         ksp->ks_magic = KS_MAGIC;
-       spin_lock_init(&ksp->ks_lock);
+       mutex_init(&ksp->ks_private_lock, NULL, MUTEX_DEFAULT, NULL);
+       ksp->ks_lock = &ksp->ks_private_lock;
        INIT_LIST_HEAD(&ksp->ks_list);
 
        ksp->ks_crtime = gethrtime();
@@ -355,6 +562,13 @@ __kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
        strncpy(ksp->ks_class, ks_class, KSTAT_STRLEN);
        ksp->ks_type = ks_type;
        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:
@@ -378,13 +592,13 @@ __kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
                         ksp->ks_data_size = ks_ndata * sizeof(kstat_timer_t);
                         break;
                 default:
-                        SBUG(); /* Unreachable */
+                        PANIC("Undefined kstat type %d\n", ksp->ks_type);
         }
 
        if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL) {
                 ksp->ks_data = NULL;
         } else {
-                ksp->ks_data = kmem_alloc(ksp->ks_data_size, KM_SLEEP);
+                ksp->ks_data = kmem_zalloc(ksp->ks_data_size, KM_SLEEP);
                 if (ksp->ks_data == NULL) {
                         kmem_free(ksp, sizeof(*ksp));
                         ksp = NULL;
@@ -395,102 +609,117 @@ __kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
 }
 EXPORT_SYMBOL(__kstat_create);
 
-void
-__kstat_install(kstat_t *ksp)
+static int
+kstat_detect_collision(kstat_t *ksp)
 {
-       struct proc_dir_entry *de_module, *de_name;
+       kstat_module_t *module;
        kstat_t *tmp;
-       int rc = 0;
-       ENTRY;
+       char parent[KSTAT_STRLEN+1];
+       char *cp;
 
-       spin_lock(&kstat_lock);
+       (void) strlcpy(parent, ksp->ks_module, sizeof(parent));
 
-       /* Item may only be added to the list once */
-        list_for_each_entry(tmp, &kstat_list, ks_list) {
-                if (tmp == ksp) {
-                       spin_unlock(&kstat_lock);
-                       GOTO(out, rc = -EEXIST);
-               }
+       if ((cp = strrchr(parent, '/')) == NULL)
+               return (0);
+
+       cp[0] = '\0';
+       if ((module = kstat_find_module(parent)) != NULL) {
+               list_for_each_entry(tmp, &module->ksm_kstat_list, ks_list)
+                       if (strncmp(tmp->ks_name, cp+1, KSTAT_STRLEN) == 0)
+                               return (EEXIST);
        }
 
-        list_add_tail(&ksp->ks_list, &kstat_list);
-       spin_unlock(&kstat_lock);
+       return (0);
+}
 
-       de_module = proc_dir_entry_find(proc_spl_kstat, ksp->ks_module);
-       if (de_module == NULL) {
-                de_module = proc_mkdir(ksp->ks_module, proc_spl_kstat);
-               if (de_module == NULL)
-                       GOTO(out, rc = -EUNATCH);
-       }
+void
+__kstat_install(kstat_t *ksp)
+{
+       kstat_module_t *module;
+       kstat_t *tmp;
 
-       de_name = create_proc_entry(ksp->ks_name, 0444, de_module);
-       if (de_name == NULL)
-               GOTO(out, rc = -EUNATCH);
+       ASSERT(ksp);
 
-       spin_lock(&ksp->ks_lock);
-       ksp->ks_proc = de_name;
-       de_name->proc_fops = &proc_kstat_operations;
-        de_name->data = (void *)ksp;
-       spin_unlock(&ksp->ks_lock);
-out:
-       if (rc) {
-               spin_lock(&kstat_lock);
-               list_del_init(&ksp->ks_list);
-               spin_unlock(&kstat_lock);
+       mutex_enter(&kstat_module_lock);
+
+       module = kstat_find_module(ksp->ks_module);
+       if (module == NULL) {
+               if (kstat_detect_collision(ksp) != 0) {
+                       cmn_err(CE_WARN, "kstat_create('%s', '%s'): namespace" \
+                           " collision", ksp->ks_module, ksp->ks_name);
+                       goto out;
+               }
+               module = kstat_create_module(ksp->ks_module);
+               if (module == NULL)
+                       goto out;
        }
 
-       EXIT;
+       /*
+        * Only one entry by this name per-module, on failure the module
+        * shouldn't be deleted because we know it has at least one entry.
+        */
+       list_for_each_entry(tmp, &module->ksm_kstat_list, ks_list)
+               if (strncmp(tmp->ks_name, ksp->ks_name, KSTAT_STRLEN) == 0)
+                       goto out;
+
+       list_add_tail(&ksp->ks_list, &module->ksm_kstat_list);
+
+       mutex_enter(ksp->ks_lock);
+       ksp->ks_owner = module;
+       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);
+               if (list_empty(&module->ksm_kstat_list))
+                       kstat_delete_module(module);
+       }
+       mutex_exit(ksp->ks_lock);
+out:
+       mutex_exit(&kstat_module_lock);
 }
 EXPORT_SYMBOL(__kstat_install);
 
 void
 __kstat_delete(kstat_t *ksp)
 {
-       struct proc_dir_entry *de_module;
+       kstat_module_t *module = ksp->ks_owner;
 
-       spin_lock(&kstat_lock);
-        list_del_init(&ksp->ks_list);
-       spin_unlock(&kstat_lock);
+       mutex_enter(&kstat_module_lock);
+       list_del_init(&ksp->ks_list);
+       mutex_exit(&kstat_module_lock);
 
-        if (ksp->ks_proc) {
-               de_module = ksp->ks_proc->parent;
-               remove_proc_entry(ksp->ks_name, de_module);
+       if (ksp->ks_proc) {
+               remove_proc_entry(ksp->ks_name, module->ksm_proc);
 
-               /* Remove top level module directory if it's empty */
-               if (proc_dir_entries(de_module) == 0)
-                       remove_proc_entry(de_module->name, de_module->parent);
+               /* Remove top level module directory if it's empty */
+               if (list_empty(&module->ksm_kstat_list))
+                       kstat_delete_module(module);
        }
 
        if (!(ksp->ks_flags & KSTAT_FLAG_VIRTUAL))
-                kmem_free(ksp->ks_data, ksp->ks_data_size);
+               kmem_free(ksp->ks_data, ksp->ks_data_size);
 
+       ksp->ks_lock = NULL;
+       mutex_destroy(&ksp->ks_private_lock);
        kmem_free(ksp, sizeof(*ksp));
 
        return;
 }
 EXPORT_SYMBOL(__kstat_delete);
 
-#endif /* DEBUG_KSTAT */
-
 int
-kstat_init(void)
+spl_kstat_init(void)
 {
-       ENTRY;
-#ifdef DEBUG_KSTAT
-       spin_lock_init(&kstat_lock);
-       INIT_LIST_HEAD(&kstat_list);
+       mutex_init(&kstat_module_lock, NULL, MUTEX_DEFAULT, NULL);
+       INIT_LIST_HEAD(&kstat_module_list);
         kstat_id = 0;
-#endif /* DEBUG_KSTAT */
-       RETURN(0);
+       return (0);
 }
 
 void
-kstat_fini(void)
+spl_kstat_fini(void)
 {
-       ENTRY;
-#ifdef DEBUG_KSTAT
-       ASSERT(list_empty(&kstat_list));
-#endif /* DEBUG_KSTAT */
-       EXIT;
+       ASSERT(list_empty(&kstat_module_list));
+       mutex_destroy(&kstat_module_lock);
 }