1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <sys/param.h>
23 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/systm.h>
26 #include <sys/sysctl.h>
28 #include <sys/unistd.h>
29 #include <mach/mach_types.h>
31 #define DEBUG_SUBSYSTEM S_LNET
33 #include <libcfs/libcfs.h>
35 #define LIBCFS_SYSCTL "libcfs"
36 #define LIBCFS_SYSCTL_SPRITE "sprite"
37 #define LIBCFS_SYSCTL_MAGIC 0xbabeface
39 static struct libcfs_sysctl_sprite {
41 struct sysctl_oid_list *ss_link;
42 } libcfs_sysctl_sprite = { 0, NULL };
44 static cfs_sysctl_table_header_t *libcfs_table_header = NULL;
45 extern unsigned int libcfs_debug;
46 extern unsigned int libcfs_subsystem_debug;
47 extern unsigned int libcfs_printk;
48 extern unsigned int libcfs_console_ratelimit;
49 extern unsigned int libcfs_catastrophe;
50 extern atomic_t libcfs_kmemory;
52 extern long max_debug_mb;
53 extern int cfs_trace_daemon SYSCTL_HANDLER_ARGS;
54 extern int cfs_debug_mb SYSCTL_HANDLER_ARGS;
56 * sysctl table for lnet
59 SYSCTL_NODE (, OID_AUTO, lnet, CTLFLAG_RW,
60 0, "lnet sysctl top");
62 SYSCTL_INT(_lnet, OID_AUTO, debug,
63 CTLTYPE_INT | CTLFLAG_RW , &libcfs_debug,
65 SYSCTL_INT(_lnet, OID_AUTO, subsystem_debug,
66 CTLTYPE_INT | CTLFLAG_RW, &libcfs_subsystem_debug,
67 0, "subsystem debug");
68 SYSCTL_INT(_lnet, OID_AUTO, printk,
69 CTLTYPE_INT | CTLFLAG_RW, &libcfs_printk,
71 SYSCTL_INT(_lnet, OID_AUTO, console_ratelimit,
72 CTLTYPE_INT | CTLFLAG_RW, &libcfs_console_ratelimit,
73 0, "console_ratelimit");
74 SYSCTL_STRING(_lnet, OID_AUTO, debug_path,
75 CTLTYPE_STRING | CTLFLAG_RW, debug_file_path,
77 SYSCTL_INT(_lnet, OID_AUTO, memused,
78 CTLTYPE_INT | CTLFLAG_RW, (int *)&libcfs_kmemory.counter,
80 SYSCTL_INT(_lnet, OID_AUTO, catastrophe,
81 CTLTYPE_INT | CTLFLAG_RW, (int *)&libcfs_catastrophe,
83 SYSCTL_PROC(_lnet, OID_AUTO, trace_daemon,
84 CTLTYPE_STRING | CTLFLAG_RW, 0,
85 0, &cfs_trace_daemon, "A", "trace daemon");
86 SYSCTL_PROC(_lnet, OID_AUTO, debug_mb,
87 CTLTYPE_INT | CTLFLAG_RW, &max_debug_mb,
88 0, &cfs_debug_mb, "L", "max debug size");
91 static cfs_sysctl_table_t top_table[] = {
94 &sysctl__lnet_subsystem_debug,
96 &sysctl__lnet_console_ratelimit,
97 &sysctl__lnet_debug_path,
98 &sysctl__lnet_memused,
99 &sysctl__lnet_catastrophe,
100 &sysctl__lnet_trace_daemon,
101 &sysctl__lnet_debug_mb,
106 * Register sysctl table
108 cfs_sysctl_table_header_t *
109 cfs_register_sysctl_table (cfs_sysctl_table_t *table, int arg)
111 cfs_sysctl_table_t item;
114 while ((item = table[i++]) != NULL)
115 sysctl_register_oid(item);
120 * Unregister sysctl table
123 cfs_unregister_sysctl_table (cfs_sysctl_table_header_t *table) {
125 cfs_sysctl_table_t item;
127 while ((item = table[i++]) != NULL)
128 sysctl_unregister_oid(item);
133 * Allocate a sysctl oid.
135 static struct sysctl_oid *
136 cfs_alloc_sysctl(struct sysctl_oid_list *parent, int nbr, int access,
137 const char *name, void *arg1, int arg2, const char *fmt,
138 int (*handler) SYSCTL_HANDLER_ARGS)
140 struct sysctl_oid *oid;
144 if (strlen(name) + 1 > CTL_MAXNAME) {
145 printf("libcfs: sysctl name: %s is too long.\n", name);
148 oid = (struct sysctl_oid*)_MALLOC(sizeof(struct sysctl_oid),
149 M_TEMP, M_WAITOK | M_ZERO);
153 sname = (char *)_MALLOC(sizeof(CTL_MAXNAME),
154 M_TEMP, M_WAITOK | M_ZERO);
159 sfmt = (char *)_MALLOC(4, M_TEMP, M_WAITOK | M_ZERO);
165 oid->oid_parent = &sysctl__children;
167 oid->oid_parent = parent;
168 oid->oid_number = nbr;
169 oid->oid_kind = access;
170 oid->oid_name = sname;
171 oid->oid_handler = handler;
174 if ((access & CTLTYPE) == CTLTYPE_NODE){
175 /* It's a sysctl node */
176 struct sysctl_oid_list *link;
178 link = (struct sysctl_oid_list *)_MALLOC(sizeof(struct sysctl_oid_list),
179 M_TEMP, M_WAITOK | M_ZERO);
182 oid->oid_arg1 = link;
185 oid->oid_arg1 = arg1;
186 oid->oid_arg2 = arg2;
194 _FREE(sname, M_TEMP);
200 void cfs_free_sysctl(struct sysctl_oid *oid)
202 if (oid->oid_name != NULL)
203 _FREE((void *)oid->oid_name, M_TEMP);
204 if (oid->oid_fmt != NULL)
205 _FREE((void *)oid->oid_fmt, M_TEMP);
206 if ((oid->oid_kind & CTLTYPE_NODE != 0) && oid->oid_arg1)
207 /* XXX Liang: need to assert the list is empty */
208 _FREE(oid->oid_arg1, M_TEMP);
212 #define CFS_SYSCTL_ISVALID ((libcfs_sysctl_sprite.ss_magic == LIBCFS_SYSCTL_MAGIC) && \
213 (libcfs_sysctl_sprite.ss_link != NULL))
216 cfs_sysctl_isvalid(void)
218 return CFS_SYSCTL_ISVALID;
222 cfs_alloc_sysctl_node(struct sysctl_oid_list *parent, int nbr, int access,
223 const char *name, int (*handler) SYSCTL_HANDLER_ARGS)
225 if (parent == NULL && CFS_SYSCTL_ISVALID)
226 parent = libcfs_sysctl_sprite.ss_link;
227 return cfs_alloc_sysctl(parent, nbr, CTLTYPE_NODE | access, name,
228 NULL, 0, "N", handler);
232 cfs_alloc_sysctl_int(struct sysctl_oid_list *parent, int nbr, int access,
233 const char *name, int *ptr, int val)
235 if (parent == NULL && CFS_SYSCTL_ISVALID)
236 parent = libcfs_sysctl_sprite.ss_link;
237 return cfs_alloc_sysctl(parent, nbr, CTLTYPE_INT | access, name,
238 ptr, val, "I", sysctl_handle_int);
242 cfs_alloc_sysctl_long(struct sysctl_oid_list *parent, int nbr, int access,
243 const char *name, int *ptr, int val)
245 if (parent == NULL && CFS_SYSCTL_ISVALID)
246 parent = libcfs_sysctl_sprite.ss_link;
247 return cfs_alloc_sysctl(parent, nbr, CTLTYPE_INT | access, name,
248 ptr, val, "L", sysctl_handle_long);
252 cfs_alloc_sysctl_string(struct sysctl_oid_list *parent, int nbr, int access,
253 const char *name, char *ptr, int len)
255 if (parent == NULL && CFS_SYSCTL_ISVALID)
256 parent = libcfs_sysctl_sprite.ss_link;
257 return cfs_alloc_sysctl(parent, nbr, CTLTYPE_STRING | access, name,
258 ptr, len, "A", sysctl_handle_string);
262 cfs_alloc_sysctl_struct(struct sysctl_oid_list *parent, int nbr, int access,
263 const char *name, void *ptr, int size)
265 if (parent == NULL && CFS_SYSCTL_ISVALID)
266 parent = libcfs_sysctl_sprite.ss_link;
267 return cfs_alloc_sysctl(parent, nbr, CTLTYPE_OPAQUE | access, name,
268 ptr, size, "S", sysctl_handle_opaque);
272 cfs_proc_dir_entry_t *
273 cfs_create_proc_entry(char *name, int mod, cfs_proc_dir_entry_t *parent)
275 cfs_proc_dir_entry_t *entry;
276 MALLOC(entry, cfs_proc_dir_entry_t *, sizeof(cfs_proc_dir_entry_t), M_TEMP, M_WAITOK|M_ZERO);
282 cfs_free_proc_entry(cfs_proc_dir_entry_t *de){
288 cfs_remove_proc_entry(char *name, cfs_proc_dir_entry_t *entry)
290 cfs_free_proc_entry(entry);
298 if (!libcfs_table_header)
299 libcfs_table_header = cfs_register_sysctl_table(top_table, 0);
308 if (libcfs_table_header != NULL)
309 cfs_unregister_sysctl_table(libcfs_table_header);
310 libcfs_table_header = NULL;
316 cfs_sysctl_init(void)
318 struct sysctl_oid *oid_root;
319 struct sysctl_oid *oid_sprite;
320 struct libcfs_sysctl_sprite *sprite;
324 len = sizeof(struct libcfs_sysctl_sprite);
325 rc = sysctlbyname("libcfs.sprite",
326 (void *)&libcfs_sysctl_sprite, &len, NULL, 0);
329 * XXX Liang: assert (rc == 0 || rc == ENOENT)
331 * libcfs.sprite has been registered by previous
334 if (libcfs_sysctl_sprite.ss_magic != LIBCFS_SYSCTL_MAGIC) {
335 printf("libcfs: magic number of libcfs.sprite "
336 "is not right (%lx, %lx)\n",
337 libcfs_sysctl_sprite.ss_magic,
338 LIBCFS_SYSCTL_MAGIC);
341 assert(libcfs_sysctl_sprite.ss_link != NULL);
342 printf("libcfs: registered libcfs.sprite found.\n");
345 oid_root = cfs_alloc_sysctl_node(NULL, OID_AUTO, CTLFLAG_RD | CTLFLAG_KERN,
347 if (oid_root == NULL)
349 sysctl_register_oid(oid_root);
351 sprite = (struct libcfs_sysctl_sprite *)_MALLOC(sizeof(struct libcfs_sysctl_sprite),
352 M_TEMP, M_WAITOK | M_ZERO);
353 if (sprite == NULL) {
354 sysctl_unregister_oid(oid_root);
355 cfs_free_sysctl(oid_root);
358 sprite->ss_magic = LIBCFS_SYSCTL_MAGIC;
359 sprite->ss_link = (struct sysctl_oid_list *)oid_root->oid_arg1;
360 oid_sprite = cfs_alloc_sysctl_struct((struct sysctl_oid_list *)oid_root->oid_arg1,
361 OID_AUTO, CTLFLAG_RD | CTLFLAG_KERN,
362 LIBCFS_SYSCTL_SPRITE, sprite,
363 sizeof(struct libcfs_sysctl_sprite));
364 if (oid_sprite == NULL) {
365 cfs_free_sysctl(oid_sprite);
366 sysctl_unregister_oid(oid_root);
367 cfs_free_sysctl(oid_root);
370 sysctl_register_oid(oid_sprite);
372 libcfs_sysctl_sprite.ss_magic = sprite->ss_magic;
373 libcfs_sysctl_sprite.ss_link = sprite->ss_link;
379 cfs_sysctl_fini(void)
381 libcfs_sysctl_sprite.ss_magic = 0;
382 libcfs_sysctl_sprite.ss_link = NULL;