Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / libcfs / libcfs / darwin / darwin-proc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
42 #include <sys/proc.h>
43 #include <sys/unistd.h>
44 #include <mach/mach_types.h>
45
46 #define DEBUG_SUBSYSTEM S_LNET
47
48 #include <libcfs/libcfs.h>
49
50 #define LIBCFS_SYSCTL           "libcfs"
51 #define LIBCFS_SYSCTL_SPRITE    "sprite"
52 #define LIBCFS_SYSCTL_MAGIC     0xbabeface
53
54 static struct libcfs_sysctl_sprite {
55         int                     ss_magic;
56         struct sysctl_oid_list  *ss_link;
57 } libcfs_sysctl_sprite = { 0, NULL };
58
59 static cfs_sysctl_table_header_t *libcfs_table_header = NULL;
60 extern unsigned int libcfs_debug;
61 extern unsigned int libcfs_subsystem_debug;
62 extern unsigned int libcfs_printk;
63 extern unsigned int libcfs_console_ratelimit;
64 extern unsigned int libcfs_catastrophe;
65 extern atomic_t libcfs_kmemory;
66
67 static int sysctl_debug_kernel SYSCTL_HANDLER_ARGS
68 {
69 #error "Check me"
70         const int  maxstr = 1024;
71         char      *str;
72         int        error;
73
74         if (req->newptr == USER_ADDR_NULL) {
75                 /* read request */
76                 return -EINVAL;
77         }
78
79         /* write request */
80         error = trace_allocate_string_buffer(&str, maxstr + 1);
81         if (error != 0)
82                 return error;
83
84         error = SYSCTL_IN(req, str, maxstr);
85
86         /* NB str guaranteed terminted */
87         if (error == 0)
88                 error = tracefile_dump_all_pages(str);
89
90         trace_free_string_buffer(str, maxstr + 1);
91         return error;
92 }
93
94 static int sysctl_daemon_file SYSCTL_HANDLER_ARGS
95 {
96 #error "Check me"
97         int   error;
98         char *str;
99
100         if (req->newptr == USER_ADDR_NULL) {
101                 /* a read */
102                 tracefile_read_lock();
103
104                 /* include terminating '\0' */
105                 error = SYSCTL_OUT(req, tracefile, strlen(tracefile) + 1);
106
107                 tracefile_read_unlock();
108                 return error;
109         }
110         
111         /* write request */
112         error = trace_allocate_string_buffer(&str, TRACEFILE_NAME_SIZE);
113         if (error != 0)
114                 return error;
115
116         error = SYSCTL_IN(req, str, TRACEFILE_NAME_SIZE - 1);
117
118         /* NB str guaranteed terminted */
119         if (error == 0)
120                 error = trace_daemon_command(str);
121
122         trace_free_string_buffer(str, TRACEFILE_NAME_SIZE);
123         return error;
124 }
125
126
127 static int sysctl_debug_mb SYSCTL_HANDLER_ARGS
128 {
129 #error "Check me"
130         long mb;
131         int  error;
132         
133         if (req->newptr == USER_ADDR_NULL) {
134                 /* read */
135                 mb = trace_get_debug_mb();
136                 error = SYSCTL_OUT(req, &mb, sizeof(mb));
137         } else {
138                 /* write */
139                 error = SYSCTL_IN(req, &mb, sizeof(mb));
140                 if (error == 0)
141                         error = trace_set_debug_mb(mb);
142         }
143         
144         return error;
145 }
146
147 /*
148  * sysctl table for lnet
149  */
150
151 SYSCTL_NODE (,                  OID_AUTO,       lnet,   CTLFLAG_RW,
152              0,                 "lnet sysctl top");
153
154 SYSCTL_INT(_lnet,                       OID_AUTO,       debug,
155              CTLTYPE_INT | CTLFLAG_RW ,                 &libcfs_debug,
156              0,         "debug");
157 SYSCTL_INT(_lnet,                       OID_AUTO,       subsystem_debug,
158              CTLTYPE_INT | CTLFLAG_RW,                  &libcfs_subsystem_debug,
159              0,         "subsystem debug");
160 SYSCTL_INT(_lnet,                       OID_AUTO,       printk,
161              CTLTYPE_INT | CTLFLAG_RW,                  &libcfs_printk,
162              0,         "printk");
163 SYSCTL_INT(_lnet,                       OID_AUTO,       console_ratelimit,
164              CTLTYPE_INT | CTLFLAG_RW,                  &libcfs_console_ratelimit,
165              0,         "console_ratelimit");
166 SYSCTL_STRING(_lnet,                    OID_AUTO,       debug_path,
167              CTLTYPE_STRING | CTLFLAG_RW,               debug_file_path,
168              1024,      "debug path");
169 SYSCTL_INT(_lnet,                       OID_AUTO,       memused,
170              CTLTYPE_INT | CTLFLAG_RW,                  (int *)&libcfs_kmemory.counter,
171              0,         "memused");
172 SYSCTL_INT(_lnet,                       OID_AUTO,       catastrophe,
173              CTLTYPE_INT | CTLFLAG_RW,                  (int *)&libcfs_catastrophe,
174              0,         "catastrophe");
175
176 #error "check me"
177 SYSCTL_PROC(_lnet,                      OID_AUTO,       debug_kernel,
178              CTLTYPE_STRING | CTLFLAG_W,                0,
179              0,         &sysctl_debug_kernel,           "A",    "debug_kernel");
180 SYSCTL_PROC(_lnet,                      OID_AUTO,       daemon_file,
181              CTLTYPE_STRING | CTLFLAG_RW,               0,
182              0,         &sysctl_daemon_file,            "A",    "daemon_file");
183 SYSCTL_PROC(_lnet,                      OID_AUTO,       debug_mb,
184              CTLTYPE_INT | CTLFLAG_RW,                  0,
185              0,         &sysctl_debug_mb,               "L",    "debug_mb");
186
187
188 static cfs_sysctl_table_t       top_table[] = {
189         &sysctl__lnet,
190         &sysctl__lnet_debug,
191         &sysctl__lnet_subsystem_debug,
192         &sysctl__lnet_printk,
193         &sysctl__lnet_console_ratelimit,
194         &sysctl__lnet_debug_path,
195         &sysctl__lnet_memused,
196         &sysctl__lnet_catastrophe,
197         &sysctl__lnet_debug_kernel,
198         &sysctl__lnet_daemon_file,
199         &sysctl__lnet_debug_mb,
200         NULL
201 };
202
203 /*
204  * Register sysctl table
205  */
206 cfs_sysctl_table_header_t *
207 cfs_register_sysctl_table (cfs_sysctl_table_t *table, int arg)
208 {
209         cfs_sysctl_table_t      item;
210         int i = 0;
211
212         while ((item = table[i++]) != NULL) 
213                 sysctl_register_oid(item);
214         return table;
215 }
216
217 /*
218  * Unregister sysctl table
219  */
220 void
221 cfs_unregister_sysctl_table (cfs_sysctl_table_header_t *table) {
222         int i = 0;
223         cfs_sysctl_table_t      item;
224
225         while ((item = table[i++]) != NULL) 
226                 sysctl_unregister_oid(item);
227         return;
228 }
229
230 /*
231  * Allocate a sysctl oid. 
232  */
233 static struct sysctl_oid *
234 cfs_alloc_sysctl(struct sysctl_oid_list *parent, int nbr, int access,
235                  const char *name, void *arg1, int arg2, const char *fmt,
236                  int (*handler) SYSCTL_HANDLER_ARGS)
237 {
238         struct sysctl_oid *oid;
239         char    *sname = NULL;
240         char    *sfmt = NULL;
241
242         if (strlen(name) + 1 > CTL_MAXNAME) {
243                 printf("libcfs: sysctl name: %s is too long.\n", name);
244                 return NULL;
245         }
246         oid = (struct sysctl_oid*)_MALLOC(sizeof(struct sysctl_oid), 
247                                           M_TEMP, M_WAITOK | M_ZERO);
248         if (oid == NULL) 
249                 return NULL;
250
251         sname = (char *)_MALLOC(sizeof(CTL_MAXNAME), 
252                                 M_TEMP, M_WAITOK | M_ZERO);
253         if (sname == NULL) 
254                 goto error;
255         strcpy(sname, name);
256
257         sfmt = (char *)_MALLOC(4, M_TEMP, M_WAITOK | M_ZERO);
258         if (sfmt == NULL) 
259                 goto error;
260         strcpy(sfmt, fmt);
261
262         if (parent == NULL)
263                 oid->oid_parent = &sysctl__children;
264         else
265                 oid->oid_parent = parent;
266         oid->oid_number = nbr;
267         oid->oid_kind = access;
268         oid->oid_name = sname;
269         oid->oid_handler = handler;
270         oid->oid_fmt = sfmt;
271
272         if ((access & CTLTYPE) == CTLTYPE_NODE){
273                 /* It's a sysctl node */
274                 struct sysctl_oid_list *link;
275
276                 link = (struct sysctl_oid_list *)_MALLOC(sizeof(struct sysctl_oid_list), 
277                                                          M_TEMP, M_WAITOK | M_ZERO);
278                 if (link == NULL)
279                         goto error;
280                 oid->oid_arg1 = link;
281                 oid->oid_arg2 = 0;
282         } else {
283                 oid->oid_arg1 = arg1;
284                 oid->oid_arg2 = arg2;
285         }
286
287         return oid;
288 error:
289         if (sfmt != NULL)
290                 _FREE(sfmt, M_TEMP);
291         if (sname != NULL)
292                 _FREE(sname, M_TEMP);
293         if (oid != NULL)
294                 _FREE(oid, M_TEMP);
295         return NULL;
296 }
297
298 void cfs_free_sysctl(struct sysctl_oid *oid)
299 {
300         if (oid->oid_name != NULL)
301                 _FREE((void *)oid->oid_name, M_TEMP);
302         if (oid->oid_fmt != NULL)
303                 _FREE((void *)oid->oid_fmt, M_TEMP);
304         if ((oid->oid_kind & CTLTYPE_NODE != 0) && oid->oid_arg1)
305                 /* XXX Liang: need to assert the list is empty */
306                 _FREE(oid->oid_arg1, M_TEMP);
307         _FREE(oid, M_TEMP);
308 }
309
310 #define CFS_SYSCTL_ISVALID ((libcfs_sysctl_sprite.ss_magic == LIBCFS_SYSCTL_MAGIC) && \
311                             (libcfs_sysctl_sprite.ss_link != NULL))       
312
313 int
314 cfs_sysctl_isvalid(void)
315 {
316         return CFS_SYSCTL_ISVALID;
317 }
318
319 struct sysctl_oid *
320 cfs_alloc_sysctl_node(struct sysctl_oid_list *parent, int nbr, int access,
321                       const char *name, int (*handler) SYSCTL_HANDLER_ARGS)
322 {
323         if (parent == NULL && CFS_SYSCTL_ISVALID)
324                 parent = libcfs_sysctl_sprite.ss_link;
325         return cfs_alloc_sysctl(parent, nbr, CTLTYPE_NODE | access, name,
326                                 NULL, 0, "N", handler);
327 }
328
329 struct sysctl_oid *
330 cfs_alloc_sysctl_int(struct sysctl_oid_list *parent, int nbr, int access,
331                      const char *name, int *ptr, int val)
332 {
333         if (parent == NULL && CFS_SYSCTL_ISVALID)
334                 parent = libcfs_sysctl_sprite.ss_link;
335         return cfs_alloc_sysctl(parent, nbr, CTLTYPE_INT | access, name, 
336                                 ptr, val, "I", sysctl_handle_int);
337 }
338
339 struct sysctl_oid *
340 cfs_alloc_sysctl_long(struct sysctl_oid_list *parent, int nbr, int access,
341                       const char *name, int *ptr, int val)
342 {
343         if (parent == NULL && CFS_SYSCTL_ISVALID)
344                 parent = libcfs_sysctl_sprite.ss_link;
345         return cfs_alloc_sysctl(parent, nbr, CTLTYPE_INT | access, name, 
346                                 ptr, val, "L", sysctl_handle_long);
347 }
348
349 struct sysctl_oid *
350 cfs_alloc_sysctl_string(struct sysctl_oid_list *parent, int nbr, int access,
351                         const char *name, char *ptr, int len)
352 {
353         if (parent == NULL && CFS_SYSCTL_ISVALID)
354                 parent = libcfs_sysctl_sprite.ss_link;
355         return cfs_alloc_sysctl(parent, nbr, CTLTYPE_STRING | access, name, 
356                                 ptr, len, "A", sysctl_handle_string);
357 }
358
359 struct sysctl_oid *
360 cfs_alloc_sysctl_struct(struct sysctl_oid_list *parent, int nbr, int access,
361                         const char *name, void *ptr, int size)
362 {
363         if (parent == NULL && CFS_SYSCTL_ISVALID)
364                 parent = libcfs_sysctl_sprite.ss_link;
365         return cfs_alloc_sysctl(parent, nbr, CTLTYPE_OPAQUE | access, name,
366                                 ptr, size, "S", sysctl_handle_opaque);
367 }
368
369 /* no proc in osx */
370 cfs_proc_dir_entry_t *
371 cfs_create_proc_entry(char *name, int mod, cfs_proc_dir_entry_t *parent)
372 {
373         cfs_proc_dir_entry_t *entry;
374         MALLOC(entry, cfs_proc_dir_entry_t *, sizeof(cfs_proc_dir_entry_t), M_TEMP, M_WAITOK|M_ZERO);
375
376         return  entry;
377 }
378
379 void
380 cfs_free_proc_entry(cfs_proc_dir_entry_t *de){
381         FREE(de, M_TEMP);
382         return;
383 };
384
385 void
386 cfs_remove_proc_entry(char *name, cfs_proc_dir_entry_t *entry)
387 {
388         cfs_free_proc_entry(entry);
389         return;
390 }
391
392 int
393 insert_proc(void)
394 {
395 #if 1
396         if (!libcfs_table_header) 
397                 libcfs_table_header = cfs_register_sysctl_table(top_table, 0);
398 #endif
399         return 0;
400 }
401
402 void
403 remove_proc(void)
404 {
405 #if 1
406         if (libcfs_table_header != NULL) 
407                 cfs_unregister_sysctl_table(libcfs_table_header); 
408         libcfs_table_header = NULL;
409 #endif
410         return;
411 }
412
413 int
414 cfs_sysctl_init(void)
415 {
416         struct sysctl_oid               *oid_root;
417         struct sysctl_oid               *oid_sprite;
418         struct libcfs_sysctl_sprite     *sprite;
419         size_t  len; 
420         int     rc;
421
422         len = sizeof(struct libcfs_sysctl_sprite);
423         rc = sysctlbyname("libcfs.sprite", 
424                           (void *)&libcfs_sysctl_sprite, &len, NULL, 0);
425         if (rc == 0) {
426                 /* 
427                  * XXX Liang: assert (rc == 0 || rc == ENOENT)
428                  *
429                  * libcfs.sprite has been registered by previous 
430                  * loading of libcfs 
431                  */
432                 if (libcfs_sysctl_sprite.ss_magic != LIBCFS_SYSCTL_MAGIC) {
433                         printf("libcfs: magic number of libcfs.sprite "
434                                "is not right (%lx, %lx)\n", 
435                                libcfs_sysctl_sprite.ss_magic,
436                                LIBCFS_SYSCTL_MAGIC);
437                         return -1;
438                 }
439                 assert(libcfs_sysctl_sprite.ss_link != NULL);
440                 printf("libcfs: registered libcfs.sprite found.\n");
441                 return 0;
442         }
443         oid_root = cfs_alloc_sysctl_node(NULL, OID_AUTO, CTLFLAG_RD | CTLFLAG_KERN,
444                                          LIBCFS_SYSCTL, 0);
445         if (oid_root == NULL)
446                 return -1;
447         sysctl_register_oid(oid_root);
448
449         sprite = (struct libcfs_sysctl_sprite *)_MALLOC(sizeof(struct libcfs_sysctl_sprite), 
450                                                         M_TEMP, M_WAITOK | M_ZERO);
451         if (sprite == NULL) {
452                 sysctl_unregister_oid(oid_root);
453                 cfs_free_sysctl(oid_root);
454                 return -1;
455         }
456         sprite->ss_magic = LIBCFS_SYSCTL_MAGIC;
457         sprite->ss_link = (struct sysctl_oid_list *)oid_root->oid_arg1;
458         oid_sprite = cfs_alloc_sysctl_struct((struct sysctl_oid_list *)oid_root->oid_arg1, 
459                                              OID_AUTO, CTLFLAG_RD | CTLFLAG_KERN, 
460                                              LIBCFS_SYSCTL_SPRITE, sprite, 
461                                              sizeof(struct libcfs_sysctl_sprite));
462         if (oid_sprite == NULL) {
463                 cfs_free_sysctl(oid_sprite);
464                 sysctl_unregister_oid(oid_root);
465                 cfs_free_sysctl(oid_root);
466                 return -1;
467         }
468         sysctl_register_oid(oid_sprite);
469
470         libcfs_sysctl_sprite.ss_magic = sprite->ss_magic;
471         libcfs_sysctl_sprite.ss_link = sprite->ss_link;
472
473         return 0;
474 }
475
476 void
477 cfs_sysctl_fini(void)
478 {
479         libcfs_sysctl_sprite.ss_magic = 0;
480         libcfs_sysctl_sprite.ss_link = NULL;
481 }