]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-kstat.c
Avoid WARN() from procfs on kstat collision
[mirror_spl.git] / module / spl / spl-kstat.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Kstat Implementation.
25 \*****************************************************************************/
26
27 #include <linux/seq_file.h>
28 #include <sys/kstat.h>
29 #include <sys/vmem.h>
30 #include <sys/cmn_err.h>
31
32 #ifndef HAVE_PDE_DATA
33 #define PDE_DATA(x) (PDE(x)->data)
34 #endif
35
36 static kmutex_t kstat_module_lock;
37 static struct list_head kstat_module_list;
38 static kid_t kstat_id;
39
40 static int
41 kstat_resize_raw(kstat_t *ksp)
42 {
43 if (ksp->ks_raw_bufsize == KSTAT_RAW_MAX)
44 return ENOMEM;
45
46 vmem_free(ksp->ks_raw_buf, ksp->ks_raw_bufsize);
47 ksp->ks_raw_bufsize = MIN(ksp->ks_raw_bufsize * 2, KSTAT_RAW_MAX);
48 ksp->ks_raw_buf = vmem_alloc(ksp->ks_raw_bufsize, KM_SLEEP);
49
50 return 0;
51 }
52
53 void
54 kstat_waitq_enter(kstat_io_t *kiop)
55 {
56 hrtime_t new, delta;
57 ulong_t wcnt;
58
59 new = gethrtime();
60 delta = new - kiop->wlastupdate;
61 kiop->wlastupdate = new;
62 wcnt = kiop->wcnt++;
63 if (wcnt != 0) {
64 kiop->wlentime += delta * wcnt;
65 kiop->wtime += delta;
66 }
67 }
68 EXPORT_SYMBOL(kstat_waitq_enter);
69
70 void
71 kstat_waitq_exit(kstat_io_t *kiop)
72 {
73 hrtime_t new, delta;
74 ulong_t wcnt;
75
76 new = gethrtime();
77 delta = new - kiop->wlastupdate;
78 kiop->wlastupdate = new;
79 wcnt = kiop->wcnt--;
80 ASSERT((int)wcnt > 0);
81 kiop->wlentime += delta * wcnt;
82 kiop->wtime += delta;
83 }
84 EXPORT_SYMBOL(kstat_waitq_exit);
85
86 void
87 kstat_runq_enter(kstat_io_t *kiop)
88 {
89 hrtime_t new, delta;
90 ulong_t rcnt;
91
92 new = gethrtime();
93 delta = new - kiop->rlastupdate;
94 kiop->rlastupdate = new;
95 rcnt = kiop->rcnt++;
96 if (rcnt != 0) {
97 kiop->rlentime += delta * rcnt;
98 kiop->rtime += delta;
99 }
100 }
101 EXPORT_SYMBOL(kstat_runq_enter);
102
103 void
104 kstat_runq_exit(kstat_io_t *kiop)
105 {
106 hrtime_t new, delta;
107 ulong_t rcnt;
108
109 new = gethrtime();
110 delta = new - kiop->rlastupdate;
111 kiop->rlastupdate = new;
112 rcnt = kiop->rcnt--;
113 ASSERT((int)rcnt > 0);
114 kiop->rlentime += delta * rcnt;
115 kiop->rtime += delta;
116 }
117 EXPORT_SYMBOL(kstat_runq_exit);
118
119 static int
120 kstat_seq_show_headers(struct seq_file *f)
121 {
122 kstat_t *ksp = (kstat_t *)f->private;
123 int rc = 0;
124
125 ASSERT(ksp->ks_magic == KS_MAGIC);
126
127 seq_printf(f, "%d %d 0x%02x %d %d %lld %lld\n",
128 ksp->ks_kid, ksp->ks_type, ksp->ks_flags,
129 ksp->ks_ndata, (int)ksp->ks_data_size,
130 ksp->ks_crtime, ksp->ks_snaptime);
131
132 switch (ksp->ks_type) {
133 case KSTAT_TYPE_RAW:
134 restart:
135 if (ksp->ks_raw_ops.headers) {
136 rc = ksp->ks_raw_ops.headers(
137 ksp->ks_raw_buf, ksp->ks_raw_bufsize);
138 if (rc == ENOMEM && !kstat_resize_raw(ksp))
139 goto restart;
140 if (!rc)
141 seq_puts(f, ksp->ks_raw_buf);
142 } else {
143 seq_printf(f, "raw data\n");
144 }
145 break;
146 case KSTAT_TYPE_NAMED:
147 seq_printf(f, "%-31s %-4s %s\n",
148 "name", "type", "data");
149 break;
150 case KSTAT_TYPE_INTR:
151 seq_printf(f, "%-8s %-8s %-8s %-8s %-8s\n",
152 "hard", "soft", "watchdog",
153 "spurious", "multsvc");
154 break;
155 case KSTAT_TYPE_IO:
156 seq_printf(f,
157 "%-8s %-8s %-8s %-8s %-8s %-8s "
158 "%-8s %-8s %-8s %-8s %-8s %-8s\n",
159 "nread", "nwritten", "reads", "writes",
160 "wtime", "wlentime", "wupdate",
161 "rtime", "rlentime", "rupdate",
162 "wcnt", "rcnt");
163 break;
164 case KSTAT_TYPE_TIMER:
165 seq_printf(f,
166 "%-31s %-8s "
167 "%-8s %-8s %-8s %-8s %-8s\n",
168 "name", "events", "elapsed",
169 "min", "max", "start", "stop");
170 break;
171 default:
172 PANIC("Undefined kstat type %d\n", ksp->ks_type);
173 }
174
175 return -rc;
176 }
177
178 static int
179 kstat_seq_show_raw(struct seq_file *f, unsigned char *p, int l)
180 {
181 int i, j;
182
183 for (i = 0; ; i++) {
184 seq_printf(f, "%03x:", i);
185
186 for (j = 0; j < 16; j++) {
187 if (i * 16 + j >= l) {
188 seq_printf(f, "\n");
189 goto out;
190 }
191
192 seq_printf(f, " %02x", (unsigned char)p[i * 16 + j]);
193 }
194 seq_printf(f, "\n");
195 }
196 out:
197 return 0;
198 }
199
200 static int
201 kstat_seq_show_named(struct seq_file *f, kstat_named_t *knp)
202 {
203 seq_printf(f, "%-31s %-4d ", knp->name, knp->data_type);
204
205 switch (knp->data_type) {
206 case KSTAT_DATA_CHAR:
207 knp->value.c[15] = '\0'; /* NULL terminate */
208 seq_printf(f, "%-16s", knp->value.c);
209 break;
210 /* XXX - We need to be more careful able what tokens are
211 * used for each arch, for now this is correct for x86_64.
212 */
213 case KSTAT_DATA_INT32:
214 seq_printf(f, "%d", knp->value.i32);
215 break;
216 case KSTAT_DATA_UINT32:
217 seq_printf(f, "%u", knp->value.ui32);
218 break;
219 case KSTAT_DATA_INT64:
220 seq_printf(f, "%lld", (signed long long)knp->value.i64);
221 break;
222 case KSTAT_DATA_UINT64:
223 seq_printf(f, "%llu", (unsigned long long)knp->value.ui64);
224 break;
225 case KSTAT_DATA_LONG:
226 seq_printf(f, "%ld", knp->value.l);
227 break;
228 case KSTAT_DATA_ULONG:
229 seq_printf(f, "%lu", knp->value.ul);
230 break;
231 case KSTAT_DATA_STRING:
232 KSTAT_NAMED_STR_PTR(knp)
233 [KSTAT_NAMED_STR_BUFLEN(knp)-1] = '\0';
234 seq_printf(f, "%s", KSTAT_NAMED_STR_PTR(knp));
235 break;
236 default:
237 PANIC("Undefined kstat data type %d\n", knp->data_type);
238 }
239
240 seq_printf(f, "\n");
241
242 return 0;
243 }
244
245 static int
246 kstat_seq_show_intr(struct seq_file *f, kstat_intr_t *kip)
247 {
248 seq_printf(f, "%-8u %-8u %-8u %-8u %-8u\n",
249 kip->intrs[KSTAT_INTR_HARD],
250 kip->intrs[KSTAT_INTR_SOFT],
251 kip->intrs[KSTAT_INTR_WATCHDOG],
252 kip->intrs[KSTAT_INTR_SPURIOUS],
253 kip->intrs[KSTAT_INTR_MULTSVC]);
254
255 return 0;
256 }
257
258 static int
259 kstat_seq_show_io(struct seq_file *f, kstat_io_t *kip)
260 {
261 seq_printf(f,
262 "%-8llu %-8llu %-8u %-8u %-8lld %-8lld "
263 "%-8lld %-8lld %-8lld %-8lld %-8u %-8u\n",
264 kip->nread, kip->nwritten,
265 kip->reads, kip->writes,
266 kip->wtime, kip->wlentime, kip->wlastupdate,
267 kip->rtime, kip->wlentime, kip->rlastupdate,
268 kip->wcnt, kip->rcnt);
269
270 return 0;
271 }
272
273 static int
274 kstat_seq_show_timer(struct seq_file *f, kstat_timer_t *ktp)
275 {
276 seq_printf(f,
277 "%-31s %-8llu %-8lld %-8lld %-8lld %-8lld %-8lld\n",
278 ktp->name, ktp->num_events, ktp->elapsed_time,
279 ktp->min_time, ktp->max_time,
280 ktp->start_time, ktp->stop_time);
281
282 return 0;
283 }
284
285 static int
286 kstat_seq_show(struct seq_file *f, void *p)
287 {
288 kstat_t *ksp = (kstat_t *)f->private;
289 int rc = 0;
290
291 ASSERT(ksp->ks_magic == KS_MAGIC);
292
293 switch (ksp->ks_type) {
294 case KSTAT_TYPE_RAW:
295 restart:
296 if (ksp->ks_raw_ops.data) {
297 rc = ksp->ks_raw_ops.data(
298 ksp->ks_raw_buf, ksp->ks_raw_bufsize, p);
299 if (rc == ENOMEM && !kstat_resize_raw(ksp))
300 goto restart;
301 if (!rc)
302 seq_puts(f, ksp->ks_raw_buf);
303 } else {
304 ASSERT(ksp->ks_ndata == 1);
305 rc = kstat_seq_show_raw(f, ksp->ks_data,
306 ksp->ks_data_size);
307 }
308 break;
309 case KSTAT_TYPE_NAMED:
310 rc = kstat_seq_show_named(f, (kstat_named_t *)p);
311 break;
312 case KSTAT_TYPE_INTR:
313 rc = kstat_seq_show_intr(f, (kstat_intr_t *)p);
314 break;
315 case KSTAT_TYPE_IO:
316 rc = kstat_seq_show_io(f, (kstat_io_t *)p);
317 break;
318 case KSTAT_TYPE_TIMER:
319 rc = kstat_seq_show_timer(f, (kstat_timer_t *)p);
320 break;
321 default:
322 PANIC("Undefined kstat type %d\n", ksp->ks_type);
323 }
324
325 return -rc;
326 }
327
328 int
329 kstat_default_update(kstat_t *ksp, int rw)
330 {
331 ASSERT(ksp != NULL);
332
333 if (rw == KSTAT_WRITE)
334 return (EACCES);
335
336 return 0;
337 }
338
339 static void *
340 kstat_seq_data_addr(kstat_t *ksp, loff_t n)
341 {
342 void *rc = NULL;
343
344 switch (ksp->ks_type) {
345 case KSTAT_TYPE_RAW:
346 if (ksp->ks_raw_ops.addr)
347 rc = ksp->ks_raw_ops.addr(ksp, n);
348 else
349 rc = ksp->ks_data;
350 break;
351 case KSTAT_TYPE_NAMED:
352 rc = ksp->ks_data + n * sizeof(kstat_named_t);
353 break;
354 case KSTAT_TYPE_INTR:
355 rc = ksp->ks_data + n * sizeof(kstat_intr_t);
356 break;
357 case KSTAT_TYPE_IO:
358 rc = ksp->ks_data + n * sizeof(kstat_io_t);
359 break;
360 case KSTAT_TYPE_TIMER:
361 rc = ksp->ks_data + n * sizeof(kstat_timer_t);
362 break;
363 default:
364 PANIC("Undefined kstat type %d\n", ksp->ks_type);
365 }
366
367 return (rc);
368 }
369
370 static void *
371 kstat_seq_start(struct seq_file *f, loff_t *pos)
372 {
373 loff_t n = *pos;
374 kstat_t *ksp = (kstat_t *)f->private;
375 ASSERT(ksp->ks_magic == KS_MAGIC);
376
377 mutex_enter(ksp->ks_lock);
378
379 if (ksp->ks_type == KSTAT_TYPE_RAW) {
380 ksp->ks_raw_bufsize = PAGE_SIZE;
381 ksp->ks_raw_buf = vmem_alloc(ksp->ks_raw_bufsize, KM_SLEEP);
382 }
383
384 /* Dynamically update kstat, on error existing kstats are used */
385 (void) ksp->ks_update(ksp, KSTAT_READ);
386
387 ksp->ks_snaptime = gethrtime();
388
389 if (!n && kstat_seq_show_headers(f))
390 return (NULL);
391
392 if (n >= ksp->ks_ndata)
393 return (NULL);
394
395 return (kstat_seq_data_addr(ksp, n));
396 }
397
398 static void *
399 kstat_seq_next(struct seq_file *f, void *p, loff_t *pos)
400 {
401 kstat_t *ksp = (kstat_t *)f->private;
402 ASSERT(ksp->ks_magic == KS_MAGIC);
403
404 ++*pos;
405 if (*pos >= ksp->ks_ndata)
406 return (NULL);
407
408 return (kstat_seq_data_addr(ksp, *pos));
409 }
410
411 static void
412 kstat_seq_stop(struct seq_file *f, void *v)
413 {
414 kstat_t *ksp = (kstat_t *)f->private;
415 ASSERT(ksp->ks_magic == KS_MAGIC);
416
417 if (ksp->ks_type == KSTAT_TYPE_RAW)
418 vmem_free(ksp->ks_raw_buf, ksp->ks_raw_bufsize);
419
420 mutex_exit(ksp->ks_lock);
421 }
422
423 static struct seq_operations kstat_seq_ops = {
424 .show = kstat_seq_show,
425 .start = kstat_seq_start,
426 .next = kstat_seq_next,
427 .stop = kstat_seq_stop,
428 };
429
430 static kstat_module_t *
431 kstat_find_module(char *name)
432 {
433 kstat_module_t *module;
434
435 list_for_each_entry(module, &kstat_module_list, ksm_module_list)
436 if (strncmp(name, module->ksm_name, KSTAT_STRLEN) == 0)
437 return (module);
438
439 return (NULL);
440 }
441
442 static kstat_module_t *
443 kstat_create_module(char *name)
444 {
445 kstat_module_t *module;
446 struct proc_dir_entry *pde;
447
448 pde = proc_mkdir(name, proc_spl_kstat);
449 if (pde == NULL)
450 return (NULL);
451
452 module = kmem_alloc(sizeof (kstat_module_t), KM_SLEEP);
453 module->ksm_proc = pde;
454 strlcpy(module->ksm_name, name, KSTAT_STRLEN+1);
455 INIT_LIST_HEAD(&module->ksm_kstat_list);
456 list_add_tail(&module->ksm_module_list, &kstat_module_list);
457
458 return (module);
459
460 }
461
462 static void
463 kstat_delete_module(kstat_module_t *module)
464 {
465 ASSERT(list_empty(&module->ksm_kstat_list));
466 remove_proc_entry(module->ksm_name, proc_spl_kstat);
467 list_del(&module->ksm_module_list);
468 kmem_free(module, sizeof(kstat_module_t));
469 }
470
471 static int
472 proc_kstat_open(struct inode *inode, struct file *filp)
473 {
474 struct seq_file *f;
475 int rc;
476
477 rc = seq_open(filp, &kstat_seq_ops);
478 if (rc)
479 return rc;
480
481 f = filp->private_data;
482 f->private = PDE_DATA(inode);
483
484 return rc;
485 }
486
487 static ssize_t
488 proc_kstat_write(struct file *filp, const char __user *buf,
489 size_t len, loff_t *ppos)
490 {
491 struct seq_file *f = filp->private_data;
492 kstat_t *ksp = f->private;
493 int rc;
494
495 ASSERT(ksp->ks_magic == KS_MAGIC);
496
497 mutex_enter(ksp->ks_lock);
498 rc = ksp->ks_update(ksp, KSTAT_WRITE);
499 mutex_exit(ksp->ks_lock);
500
501 if (rc)
502 return (-rc);
503
504 *ppos += len;
505 return (len);
506 }
507
508 static struct file_operations proc_kstat_operations = {
509 .open = proc_kstat_open,
510 .write = proc_kstat_write,
511 .read = seq_read,
512 .llseek = seq_lseek,
513 .release = seq_release,
514 };
515
516 void
517 __kstat_set_raw_ops(kstat_t *ksp,
518 int (*headers)(char *buf, size_t size),
519 int (*data)(char *buf, size_t size, void *data),
520 void *(*addr)(kstat_t *ksp, loff_t index))
521 {
522 ksp->ks_raw_ops.headers = headers;
523 ksp->ks_raw_ops.data = data;
524 ksp->ks_raw_ops.addr = addr;
525 }
526 EXPORT_SYMBOL(__kstat_set_raw_ops);
527
528 kstat_t *
529 __kstat_create(const char *ks_module, int ks_instance, const char *ks_name,
530 const char *ks_class, uchar_t ks_type, uint_t ks_ndata,
531 uchar_t ks_flags)
532 {
533 kstat_t *ksp;
534
535 ASSERT(ks_module);
536 ASSERT(ks_instance == 0);
537 ASSERT(ks_name);
538 ASSERT(!(ks_flags & KSTAT_FLAG_UNSUPPORTED));
539
540 if ((ks_type == KSTAT_TYPE_INTR) || (ks_type == KSTAT_TYPE_IO))
541 ASSERT(ks_ndata == 1);
542
543 ksp = kmem_zalloc(sizeof(*ksp), KM_SLEEP);
544 if (ksp == NULL)
545 return ksp;
546
547 mutex_enter(&kstat_module_lock);
548 ksp->ks_kid = kstat_id;
549 kstat_id++;
550 mutex_exit(&kstat_module_lock);
551
552 ksp->ks_magic = KS_MAGIC;
553 mutex_init(&ksp->ks_private_lock, NULL, MUTEX_DEFAULT, NULL);
554 ksp->ks_lock = &ksp->ks_private_lock;
555 INIT_LIST_HEAD(&ksp->ks_list);
556
557 ksp->ks_crtime = gethrtime();
558 ksp->ks_snaptime = ksp->ks_crtime;
559 strncpy(ksp->ks_module, ks_module, KSTAT_STRLEN);
560 ksp->ks_instance = ks_instance;
561 strncpy(ksp->ks_name, ks_name, KSTAT_STRLEN);
562 strncpy(ksp->ks_class, ks_class, KSTAT_STRLEN);
563 ksp->ks_type = ks_type;
564 ksp->ks_flags = ks_flags;
565 ksp->ks_update = kstat_default_update;
566 ksp->ks_private = NULL;
567 ksp->ks_raw_ops.headers = NULL;
568 ksp->ks_raw_ops.data = NULL;
569 ksp->ks_raw_ops.addr = NULL;
570 ksp->ks_raw_buf = NULL;
571 ksp->ks_raw_bufsize = 0;
572
573 switch (ksp->ks_type) {
574 case KSTAT_TYPE_RAW:
575 ksp->ks_ndata = 1;
576 ksp->ks_data_size = ks_ndata;
577 break;
578 case KSTAT_TYPE_NAMED:
579 ksp->ks_ndata = ks_ndata;
580 ksp->ks_data_size = ks_ndata * sizeof(kstat_named_t);
581 break;
582 case KSTAT_TYPE_INTR:
583 ksp->ks_ndata = ks_ndata;
584 ksp->ks_data_size = ks_ndata * sizeof(kstat_intr_t);
585 break;
586 case KSTAT_TYPE_IO:
587 ksp->ks_ndata = ks_ndata;
588 ksp->ks_data_size = ks_ndata * sizeof(kstat_io_t);
589 break;
590 case KSTAT_TYPE_TIMER:
591 ksp->ks_ndata = ks_ndata;
592 ksp->ks_data_size = ks_ndata * sizeof(kstat_timer_t);
593 break;
594 default:
595 PANIC("Undefined kstat type %d\n", ksp->ks_type);
596 }
597
598 if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL) {
599 ksp->ks_data = NULL;
600 } else {
601 ksp->ks_data = kmem_zalloc(ksp->ks_data_size, KM_SLEEP);
602 if (ksp->ks_data == NULL) {
603 kmem_free(ksp, sizeof(*ksp));
604 ksp = NULL;
605 }
606 }
607
608 return ksp;
609 }
610 EXPORT_SYMBOL(__kstat_create);
611
612 static int
613 kstat_detect_collision(kstat_t *ksp)
614 {
615 kstat_module_t *module;
616 kstat_t *tmp;
617 char parent[KSTAT_STRLEN+1];
618 char *cp;
619
620 (void) strlcpy(parent, ksp->ks_module, sizeof(parent));
621
622 if ((cp = strrchr(parent, '/')) == NULL)
623 return (0);
624
625 cp[0] = '\0';
626 if ((module = kstat_find_module(parent)) != NULL) {
627 list_for_each_entry(tmp, &module->ksm_kstat_list, ks_list)
628 if (strncmp(tmp->ks_name, cp+1, KSTAT_STRLEN) == 0)
629 return (EEXIST);
630 }
631
632 return (0);
633 }
634
635 void
636 __kstat_install(kstat_t *ksp)
637 {
638 kstat_module_t *module;
639 kstat_t *tmp;
640
641 ASSERT(ksp);
642
643 mutex_enter(&kstat_module_lock);
644
645 module = kstat_find_module(ksp->ks_module);
646 if (module == NULL) {
647 if (kstat_detect_collision(ksp) != 0) {
648 cmn_err(CE_WARN, "kstat_create('%s', '%s'): namespace" \
649 " collision", ksp->ks_module, ksp->ks_name);
650 goto out;
651 }
652 module = kstat_create_module(ksp->ks_module);
653 if (module == NULL)
654 goto out;
655 }
656
657 /*
658 * Only one entry by this name per-module, on failure the module
659 * shouldn't be deleted because we know it has at least one entry.
660 */
661 list_for_each_entry(tmp, &module->ksm_kstat_list, ks_list)
662 if (strncmp(tmp->ks_name, ksp->ks_name, KSTAT_STRLEN) == 0)
663 goto out;
664
665 list_add_tail(&ksp->ks_list, &module->ksm_kstat_list);
666
667 mutex_enter(ksp->ks_lock);
668 ksp->ks_owner = module;
669 ksp->ks_proc = proc_create_data(ksp->ks_name, 0644,
670 module->ksm_proc, &proc_kstat_operations, (void *)ksp);
671 if (ksp->ks_proc == NULL) {
672 list_del_init(&ksp->ks_list);
673 if (list_empty(&module->ksm_kstat_list))
674 kstat_delete_module(module);
675 }
676 mutex_exit(ksp->ks_lock);
677 out:
678 mutex_exit(&kstat_module_lock);
679 }
680 EXPORT_SYMBOL(__kstat_install);
681
682 void
683 __kstat_delete(kstat_t *ksp)
684 {
685 kstat_module_t *module = ksp->ks_owner;
686
687 mutex_enter(&kstat_module_lock);
688 list_del_init(&ksp->ks_list);
689 mutex_exit(&kstat_module_lock);
690
691 if (ksp->ks_proc) {
692 remove_proc_entry(ksp->ks_name, module->ksm_proc);
693
694 /* Remove top level module directory if it's empty */
695 if (list_empty(&module->ksm_kstat_list))
696 kstat_delete_module(module);
697 }
698
699 if (!(ksp->ks_flags & KSTAT_FLAG_VIRTUAL))
700 kmem_free(ksp->ks_data, ksp->ks_data_size);
701
702 ksp->ks_lock = NULL;
703 mutex_destroy(&ksp->ks_private_lock);
704 kmem_free(ksp, sizeof(*ksp));
705
706 return;
707 }
708 EXPORT_SYMBOL(__kstat_delete);
709
710 int
711 spl_kstat_init(void)
712 {
713 mutex_init(&kstat_module_lock, NULL, MUTEX_DEFAULT, NULL);
714 INIT_LIST_HEAD(&kstat_module_list);
715 kstat_id = 0;
716 return (0);
717 }
718
719 void
720 spl_kstat_fini(void)
721 {
722 ASSERT(list_empty(&kstat_module_list));
723 mutex_destroy(&kstat_module_lock);
724 }
725