kernel - Incidental MPLOCK removal
[dragonfly.git] / sys / kern / kern_sysctl.c
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Karels at Berkeley Software Design, Inc.
7  *
8  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9  * project, to make these variables more userfriendly.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)kern_sysctl.c       8.4 (Berkeley) 4/14/94
36  * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/buf.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/priv.h>
47 #include <sys/sysproto.h>
48 #include <sys/lock.h>
49 #include <sys/sbuf.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_extern.h>
53
54 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
55 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
56
57 /*
58  * The sysctllock protects the MIB tree.  It also protects sysctl
59  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
60  * sysctl_unregister_oid() routines require the sysctllock to already
61  * be held, so the sysctl_lock() and sysctl_unlock() routines are
62  * provided for the few places in the kernel which need to use that
63  * API rather than using the dynamic API.  Use of the dynamic API is
64  * strongly encouraged for most code.
65  *
66  * The sysctlmemlock is used to limit the amount of user memory wired for
67  * sysctl requests.  This is implemented by serializing any userland
68  * sysctl requests larger than a single page via an exclusive lock.
69  */
70 struct lock sysctllock;
71 static struct lock sysctlmemlock;
72
73 #define SYSCTL_INIT()           lockinit(&sysctllock,                   \
74                                     "sysctl lock", 0, LK_CANRECURSE)
75 #define SYSCTL_SLEEP(ch, wmesg, timo)                                   \
76                                 lksleep(ch, &sysctllock, 0, wmesg, timo)
77
78 static int      sysctl_root(SYSCTL_HANDLER_ARGS);
79 static void     sysctl_register_oid_int(struct sysctl_oid *oipd);
80 static void     sysctl_unregister_oid_int(struct sysctl_oid *oipd);
81
82 struct sysctl_oid_list sysctl__children; /* root list */
83
84 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
85                     int recurse);
86
87 static struct sysctl_oid *
88 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
89 {
90         struct sysctl_oid *oidp;
91
92         SLIST_FOREACH(oidp, list, oid_link) {
93                 if (strcmp(oidp->oid_name, name) == 0) {
94                         break;
95                 }
96         }
97         return (oidp);
98 }
99
100 /*
101  * Initialization of the MIB tree.
102  *
103  * Order by number in each list.
104  */
105
106 void
107 sysctl_register_oid(struct sysctl_oid *oidp)
108 {
109         SYSCTL_XLOCK();
110         sysctl_register_oid_int(oidp);
111         SYSCTL_XUNLOCK();
112 }
113
114 static void
115 sysctl_register_oid_int(struct sysctl_oid *oidp)
116 {
117         struct sysctl_oid_list *parent = oidp->oid_parent;
118         struct sysctl_oid *p;
119         struct sysctl_oid *q;
120
121         /*
122          * Finish initialization from sysctl_set or add.
123          */
124         lockinit(&oidp->oid_lock, "oidlk", 0, LK_CANRECURSE);
125
126         /*
127          * First check if another oid with the same name already
128          * exists in the parent's list.
129          */
130         p = sysctl_find_oidname(oidp->oid_name, parent, 0);
131         if (p != NULL) {
132                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
133                         p->oid_refcnt++;
134                 else
135                         kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
136                 return;
137         }
138
139         /*
140          * If this oid has a number OID_AUTO, give it a number which
141          * is greater than any current oid.  Make sure it is at least
142          * 256 to leave space for pre-assigned oid numbers.
143          */
144         if (oidp->oid_number == OID_AUTO) {
145                 int newoid = 0x100;     /* minimum AUTO oid */
146
147                 /*
148                  * Adjust based on highest oid in parent list
149                  */
150                 SLIST_FOREACH(p, parent, oid_link) {
151                         if (newoid <= p->oid_number)
152                                 newoid = p->oid_number + 1;
153                 }
154                 oidp->oid_number = newoid;
155         }
156
157         /*
158          * Insert the oid into the parent's list in order.
159          */
160         q = NULL;
161         SLIST_FOREACH(p, parent, oid_link) {
162                 if (oidp->oid_number < p->oid_number)
163                         break;
164                 q = p;
165         }
166         if (q)
167                 SLIST_INSERT_AFTER(q, oidp, oid_link);
168         else
169                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
170 }
171
172 void
173 sysctl_unregister_oid(struct sysctl_oid *oidp)
174 {
175         SYSCTL_XLOCK();
176         sysctl_unregister_oid_int(oidp);
177         SYSCTL_XUNLOCK();
178 }
179
180 static void
181 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
182 {
183         struct sysctl_oid *p;
184
185         if (oidp->oid_number == OID_AUTO)
186                 panic("Trying to unregister OID_AUTO entry: %p", oidp);
187
188         SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
189                 if (p != oidp)
190                         continue;
191                 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
192                 return;
193         }
194
195         /*
196          * This can happen when a module fails to register and is
197          * being unloaded afterwards.  It should not be a panic()
198          * for normal use.
199          */
200         kprintf("%s: failed to unregister sysctl\n", __func__);
201 }
202
203 /* Initialize a new context to keep track of dynamically added sysctls. */
204 int
205 sysctl_ctx_init(struct sysctl_ctx_list *c)
206 {
207         if (c == NULL)
208                 return(EINVAL);
209         TAILQ_INIT(c);
210         return(0);
211 }
212
213 /* Free the context, and destroy all dynamic oids registered in this context */
214 int
215 sysctl_ctx_free(struct sysctl_ctx_list *clist)
216 {
217         struct sysctl_ctx_entry *e, *e1;
218         int error;
219
220         error = 0;
221         /*
222          * First perform a "dry run" to check if it's ok to remove oids.
223          * XXX FIXME
224          * XXX This algorithm is a hack. But I don't know any
225          * XXX better solution for now...
226          */
227         SYSCTL_XLOCK();
228         TAILQ_FOREACH(e, clist, link) {
229                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
230                 if (error)
231                         break;
232         }
233         /*
234          * Restore deregistered entries, either from the end,
235          * or from the place where error occured.
236          * e contains the entry that was not unregistered
237          */
238         if (error)
239                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
240         else
241                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
242         while (e1 != NULL) {
243                 sysctl_register_oid(e1->entry);
244                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
245         }
246         if (error) {
247                 SYSCTL_XUNLOCK();
248                 return(EBUSY);
249         }
250         /* Now really delete the entries */
251         e = TAILQ_FIRST(clist);
252         while (e != NULL) {
253                 e1 = TAILQ_NEXT(e, link);
254                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
255                 if (error)
256                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
257                             e->entry->oid_name);
258                 kfree(e, M_SYSCTLOID);
259                 e = e1;
260         }
261         SYSCTL_XUNLOCK();
262         return (error);
263 }
264
265 /* Add an entry to the context */
266 struct sysctl_ctx_entry *
267 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
268 {
269         struct sysctl_ctx_entry *e;
270
271         SYSCTL_ASSERT_LOCKED();
272         if (clist == NULL || oidp == NULL)
273                 return(NULL);
274         e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
275         e->entry = oidp;
276         TAILQ_INSERT_HEAD(clist, e, link);
277         return (e);
278 }
279
280 /* Find an entry in the context */
281 struct sysctl_ctx_entry *
282 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
283 {
284         struct sysctl_ctx_entry *e;
285
286         SYSCTL_ASSERT_LOCKED();
287         if (clist == NULL || oidp == NULL)
288                 return(NULL);
289         TAILQ_FOREACH(e, clist, link) {
290                 if(e->entry == oidp)
291                         return(e);
292         }
293         return (e);
294 }
295
296 /*
297  * Delete an entry from the context.
298  * NOTE: this function doesn't free oidp! You have to remove it
299  * with sysctl_remove_oid().
300  */
301 int
302 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
303 {
304         struct sysctl_ctx_entry *e;
305
306         if (clist == NULL || oidp == NULL)
307                 return (EINVAL);
308         SYSCTL_XLOCK();
309         e = sysctl_ctx_entry_find(clist, oidp);
310         if (e != NULL) {
311                 TAILQ_REMOVE(clist, e, link);
312                 SYSCTL_XUNLOCK();
313                 kfree(e, M_SYSCTLOID);
314                 return (0);
315         } else {
316                 SYSCTL_XUNLOCK();
317                 return (ENOENT);
318         }
319 }
320
321 /*
322  * Remove dynamically created sysctl trees.
323  * oidp - top of the tree to be removed
324  * del - if 0 - just deregister, otherwise free up entries as well
325  * recurse - if != 0 traverse the subtree to be deleted
326  */
327 int
328 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
329 {
330         int error;
331
332         SYSCTL_XLOCK();
333         error = sysctl_remove_oid_locked(oidp, del, recurse);
334         SYSCTL_XUNLOCK();
335         return (error);
336 }
337
338 static int
339 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
340 {
341         struct sysctl_oid *p, *tmp;
342         int error;
343
344         SYSCTL_ASSERT_LOCKED();
345         if (oidp == NULL)
346                 return(EINVAL);
347         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
348                 kprintf("can't remove non-dynamic nodes!\n");
349                 return (EINVAL);
350         }
351         /*
352          * WARNING: normal method to do this should be through
353          * sysctl_ctx_free(). Use recursing as the last resort
354          * method to purge your sysctl tree of leftovers...
355          * However, if some other code still references these nodes,
356          * it will panic.
357          */
358         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
359                 if (oidp->oid_refcnt == 1) {
360                         SLIST_FOREACH_MUTABLE(p,
361                             SYSCTL_CHILDREN(oidp), oid_link, tmp) {
362                                 if (!recurse) {
363                                         kprintf("Warning: failed attempt to "
364                                             "remove oid %s with child %s\n",
365                                             oidp->oid_name, p->oid_name);
366                                         return (ENOTEMPTY);
367                                 }
368                                 error = sysctl_remove_oid_locked(p, del,
369                                     recurse);
370                                 if (error)
371                                         return (error);
372                         }
373                         if (del)
374                                 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
375                 }
376         }
377         if (oidp->oid_refcnt > 1 ) {
378                 oidp->oid_refcnt--;
379         } else {
380                 if (oidp->oid_refcnt == 0) {
381                         kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
382                                 oidp->oid_refcnt, oidp->oid_name);
383                         return (EINVAL);
384                 }
385                 sysctl_unregister_oid(oidp);
386                 if (del) {
387                         /*
388                          * Wait for all threads running the handler to drain.
389                          * This preserves the previous behavior when the
390                          * sysctl lock was held across a handler invocation,
391                          * and is necessary for module unload correctness.
392                          */
393                         while (oidp->oid_running > 0) {
394                                 oidp->oid_kind |= CTLFLAG_DYING;
395                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
396                         }
397                         if (oidp->oid_descr)
398                                 kfree(__DECONST(char *, oidp->oid_descr),
399                                     M_SYSCTLOID);
400                         kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
401                         lockuninit(&oidp->oid_lock);
402                         kfree(oidp, M_SYSCTLOID);
403                 }
404         }
405         return (0);
406 }
407
408 int
409 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
410     int del, int recurse)
411 {
412         struct sysctl_oid *p, *tmp;
413         int error;
414
415         error = ENOENT;
416         SYSCTL_XLOCK();
417         SLIST_FOREACH_MUTABLE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
418                 if (strcmp(p->oid_name, name) == 0) {
419                         error = sysctl_remove_oid_locked(p, del, recurse);
420                         break;
421                 }
422         }
423         SYSCTL_XUNLOCK();
424
425         return (error);
426 }
427
428 /*
429  * Create new sysctls at run time.
430  * clist may point to a valid context initialized with sysctl_ctx_init().
431  */
432 struct sysctl_oid *
433 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
434         int number, const char *name, int kind, void *arg1, int arg2,
435         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
436 {
437         struct sysctl_oid *oidp;
438         ssize_t len;
439         char *newname;
440
441         /* You have to hook up somewhere.. */
442         if (parent == NULL)
443                 return(NULL);
444         SYSCTL_XLOCK();
445         /* Check if the node already exists, otherwise create it */
446         oidp = sysctl_find_oidname(name, parent, 0);
447         if (oidp != NULL) {
448                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
449                         oidp->oid_refcnt++;
450                         /* Update the context */
451                         if (clist != NULL)
452                                 sysctl_ctx_entry_add(clist, oidp);
453                         SYSCTL_XUNLOCK();
454                         return (oidp);
455                 } else {
456                         kprintf("can't re-use a leaf (%s)!\n", name);
457                         SYSCTL_XUNLOCK();
458                         return (NULL);
459                 }
460         }
461         oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID,
462                        M_WAITOK | M_ZERO);
463         oidp->oid_parent = parent;
464         SLIST_NEXT(oidp, oid_link) = NULL;
465         oidp->oid_number = number;
466         oidp->oid_refcnt = 1;
467         len = strlen(name);
468         newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
469         bcopy(name, newname, len + 1);
470         newname[len] = '\0';
471         oidp->oid_name = newname;
472         oidp->oid_handler = handler;
473         oidp->oid_kind = CTLFLAG_DYN | kind;
474         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
475                 struct sysctl_oid_list *children;
476
477                 /* Allocate space for children */
478                 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
479                 SYSCTL_SET_CHILDREN(oidp, children);
480                 SLIST_INIT(children);
481         } else {
482                 oidp->oid_arg1 = arg1;
483                 oidp->oid_arg2 = arg2;
484         }
485         oidp->oid_fmt = fmt;
486         if (descr) {
487                 int len = strlen(descr) + 1;
488                 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
489                 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
490         };
491         /* Update the context, if used */
492         if (clist != NULL)
493                 sysctl_ctx_entry_add(clist, oidp);
494         /* Register this oid */
495         sysctl_register_oid_int(oidp);
496         SYSCTL_XUNLOCK();
497         return (oidp);
498 }
499
500 /*
501  * Rename an existing oid.
502  */
503 void
504 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
505 {
506         char *newname;
507         char *oldname;
508
509         newname = kstrdup(name, M_SYSCTLOID);
510         SYSCTL_XLOCK();
511         oldname = __DECONST(char *, oidp->oid_name);
512         oidp->oid_name = newname;
513         SYSCTL_XUNLOCK();
514         kfree(oldname, M_SYSCTLOID);
515 }
516
517 /*
518  * Register the kernel's oids on startup.
519  */
520 SET_DECLARE(sysctl_set, struct sysctl_oid);
521
522 static void
523 sysctl_register_all(void *arg)
524 {
525         struct sysctl_oid **oidp;
526
527         lockinit(&sysctlmemlock, "sysctl mem", 0, LK_CANRECURSE);
528         SYSCTL_INIT();
529         SYSCTL_XLOCK();
530         SET_FOREACH(oidp, sysctl_set)
531                 sysctl_register_oid(*oidp);
532         SYSCTL_XUNLOCK();
533 }
534 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
535
536 /*
537  * "Staff-functions"
538  *
539  * These functions implement a presently undocumented interface 
540  * used by the sysctl program to walk the tree, and get the type
541  * so it can print the value.
542  * This interface is under work and consideration, and should probably
543  * be killed with a big axe by the first person who can find the time.
544  * (be aware though, that the proper interface isn't as obvious as it
545  * may seem, there are various conflicting requirements.
546  *
547  * {0,0}        kprintf the entire MIB-tree.
548  * {0,1,...}    return the name of the "..." OID.
549  * {0,2,...}    return the next OID.
550  * {0,3}        return the OID of the name in "new"
551  * {0,4,...}    return the kind & format info for the "..." OID.
552  */
553
554 static void
555 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
556 {
557         int k;
558         struct sysctl_oid *oidp;
559
560         SLIST_FOREACH(oidp, l, oid_link) {
561
562                 for (k=0; k<i; k++)
563                         kprintf(" ");
564
565                 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
566
567                 kprintf("%c%c",
568                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
569                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
570
571                 if (oidp->oid_handler)
572                         kprintf(" *Handler");
573
574                 switch (oidp->oid_kind & CTLTYPE) {
575                         case CTLTYPE_NODE:
576                                 kprintf(" Node\n");
577                                 if (!oidp->oid_handler) {
578                                         sysctl_sysctl_debug_dump_node(
579                                                 oidp->oid_arg1, i+2);
580                                 }
581                                 break;
582                         case CTLTYPE_INT:    kprintf(" Int\n"); break;
583                         case CTLTYPE_STRING: kprintf(" String\n"); break;
584                         case CTLTYPE_QUAD:   kprintf(" Quad\n"); break;
585                         case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
586                         default:             kprintf("\n");
587                 }
588
589         }
590 }
591
592 static int
593 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
594 {
595         int error;
596
597         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
598         if (error)
599                 return (error);
600         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
601
602         return (ENOENT);
603 }
604
605 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
606         0, 0, sysctl_sysctl_debug, "-", "");
607
608 static int
609 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
610 {
611         int *name = (int *) arg1;
612         u_int namelen = arg2;
613         int error = 0;
614         struct sysctl_oid *oid;
615         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
616         char buf[10];
617
618         while (namelen) {
619                 if (!lsp) {
620                         ksnprintf(buf, sizeof(buf), "%d",  *name);
621                         if (req->oldidx)
622                                 error = SYSCTL_OUT(req, ".", 1);
623                         if (!error)
624                                 error = SYSCTL_OUT(req, buf, strlen(buf));
625                         if (error)
626                                 goto out;
627                         namelen--;
628                         name++;
629                         continue;
630                 }
631                 lsp2 = NULL;
632                 SLIST_FOREACH(oid, lsp, oid_link) {
633                         if (oid->oid_number != *name)
634                                 continue;
635
636                         if (req->oldidx)
637                                 error = SYSCTL_OUT(req, ".", 1);
638                         if (!error)
639                                 error = SYSCTL_OUT(req, oid->oid_name,
640                                         strlen(oid->oid_name));
641                         if (error)
642                                 goto out;
643
644                         namelen--;
645                         name++;
646
647                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
648                                 break;
649
650                         if (oid->oid_handler)
651                                 break;
652
653                         lsp2 = SYSCTL_CHILDREN(oid);
654                         break;
655                 }
656                 lsp = lsp2;
657         }
658         error = SYSCTL_OUT(req, "", 1);
659  out:
660         return (error);
661 }
662
663 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
664
665 static int
666 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
667         int *next, int *len, int level, struct sysctl_oid **oidpp)
668 {
669         struct sysctl_oid *oidp;
670
671         *len = level;
672         SLIST_FOREACH(oidp, lsp, oid_link) {
673                 *next = oidp->oid_number;
674                 *oidpp = oidp;
675
676                 if (oidp->oid_kind & CTLFLAG_SKIP)
677                         continue;
678
679                 if (!namelen) {
680                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
681                                 return (0);
682                         if (oidp->oid_handler) 
683                                 /* We really should call the handler here...*/
684                                 return (0);
685                         lsp = SYSCTL_CHILDREN(oidp);
686                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
687                                 len, level+1, oidpp))
688                                 return (0);
689                         goto emptynode;
690                 }
691
692                 if (oidp->oid_number < *name)
693                         continue;
694
695                 if (oidp->oid_number > *name) {
696                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
697                                 return (0);
698                         if (oidp->oid_handler)
699                                 return (0);
700                         lsp = SYSCTL_CHILDREN(oidp);
701                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
702                                 next+1, len, level+1, oidpp))
703                                 return (0);
704                         goto next;
705                 }
706                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
707                         continue;
708
709                 if (oidp->oid_handler)
710                         continue;
711
712                 lsp = SYSCTL_CHILDREN(oidp);
713                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
714                         len, level+1, oidpp))
715                         return (0);
716         next:
717                 namelen = 1;
718         emptynode:
719                 *len = level;
720         }
721         return (1);
722 }
723
724 static int
725 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
726 {
727         int *name = (int *) arg1;
728         u_int namelen = arg2;
729         int i, j, error;
730         struct sysctl_oid *oid;
731         struct sysctl_oid_list *lsp = &sysctl__children;
732         int newoid[CTL_MAXNAME];
733
734         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
735         if (i)
736                 return ENOENT;
737         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
738
739         return (error);
740 }
741
742 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
743
744 static int
745 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
746 {
747         struct sysctl_oid *oidp;
748         struct sysctl_oid_list *lsp = &sysctl__children;
749         char *p;
750
751         SYSCTL_ASSERT_LOCKED();
752
753         for (*len = 0; *len < CTL_MAXNAME;) {
754                 p = strsep(&name, ".");
755
756                 oidp = SLIST_FIRST(lsp);
757                 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
758                         if (oidp == NULL)
759                                 return (ENOENT);
760                         if (strcmp(p, oidp->oid_name) == 0)
761                                 break;
762                 }
763                 *oid++ = oidp->oid_number;
764                 (*len)++;
765
766                 if (name == NULL || *name == '\0') {
767                         if (oidpp)
768                                 *oidpp = oidp;
769                         return (0);
770                 }
771
772                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
773                         break;
774
775                 if (oidp->oid_handler)
776                         break;
777
778                 lsp = SYSCTL_CHILDREN(oidp);
779         }
780         return (ENOENT);
781 }
782
783 static int
784 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
785 {
786         char *p;
787         int error, oid[CTL_MAXNAME], len;
788         struct sysctl_oid *op = NULL;
789
790         if (!req->newlen) 
791                 return ENOENT;
792         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
793                 return (ENAMETOOLONG);
794
795         p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
796
797         error = SYSCTL_IN(req, p, req->newlen);
798         if (error) {
799                 kfree(p, M_SYSCTL);
800                 return (error);
801         }
802
803         p [req->newlen] = '\0';
804
805         error = name2oid(p, oid, &len, &op);
806
807         kfree(p, M_SYSCTL);
808
809         if (error)
810                 return (error);
811
812         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
813         return (error);
814 }
815
816 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_SHLOCK,
817             0, 0, sysctl_sysctl_name2oid, "I", "");
818
819 static int
820 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
821 {
822         struct sysctl_oid *oid;
823         int error;
824
825         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
826         if (error)
827                 return (error);
828
829         if (!oid->oid_fmt)
830                 return (ENOENT);
831         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
832         if (error)
833                 return (error);
834         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
835         return (error);
836 }
837
838
839 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
840
841 static int
842 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
843 {
844         struct sysctl_oid *oid;
845         int error;
846
847         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
848         if (error)
849                 return (error);
850         
851         if (!oid->oid_descr)
852                 return (ENOENT);
853         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
854         return (error);
855 }
856
857 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
858
859 /*
860  * Default "handler" functions.
861  */
862
863 /*
864  * Handle an int, signed or unsigned.
865  * Two cases:
866  *     a variable:  point arg1 at it.
867  *     a constant:  pass it in arg2.
868  */
869
870 int
871 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
872 {
873         int error = 0;
874
875         if (arg1)
876                 error = SYSCTL_OUT(req, arg1, sizeof(int));
877         else
878                 error = SYSCTL_OUT(req, &arg2, sizeof(int));
879
880         if (error || !req->newptr)
881                 return (error);
882
883         if (!arg1)
884                 error = EPERM;
885         else
886                 error = SYSCTL_IN(req, arg1, sizeof(int));
887         return (error);
888 }
889
890 /*
891  * Handle a long, signed or unsigned.  arg1 points to it.
892  */
893
894 int
895 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
896 {
897         int error = 0;
898
899         if (!arg1)
900                 return (EINVAL);
901         error = SYSCTL_OUT(req, arg1, sizeof(long));
902
903         if (error || !req->newptr)
904                 return (error);
905
906         error = SYSCTL_IN(req, arg1, sizeof(long));
907         return (error);
908 }
909
910 /*
911  * Handle a quad, signed or unsigned.  arg1 points to it.
912  */
913
914 int
915 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
916 {
917         int error = 0;
918
919         if (!arg1)
920                 return (EINVAL);
921         error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
922
923         if (error || !req->newptr)
924                 return (error);
925
926         error = SYSCTL_IN(req, arg1, sizeof(quad_t));
927         return (error);
928 }
929
930 /*
931  * Handle our generic '\0' terminated 'C' string.
932  * Two cases:
933  *      a variable string:  point arg1 at it, arg2 is max length.
934  *      a constant string:  point arg1 at it, arg2 is zero.
935  */
936
937 int
938 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
939 {
940         int error=0;
941
942         error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
943
944         if (error || !req->newptr)
945                 return (error);
946
947         if ((req->newlen - req->newidx) >= arg2) {
948                 error = EINVAL;
949         } else {
950                 arg2 = (req->newlen - req->newidx);
951                 error = SYSCTL_IN(req, arg1, arg2);
952                 ((char *)arg1)[arg2] = '\0';
953         }
954
955         return (error);
956 }
957
958 /*
959  * Handle any kind of opaque data.
960  * arg1 points to it, arg2 is the size.
961  */
962
963 int
964 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
965 {
966         int error;
967
968         error = SYSCTL_OUT(req, arg1, arg2);
969
970         if (error || !req->newptr)
971                 return (error);
972
973         error = SYSCTL_IN(req, arg1, arg2);
974
975         return (error);
976 }
977
978 /*
979  * Transfer functions to/from kernel space.
980  * XXX: rather untested at this point
981  */
982 static int
983 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
984 {
985         size_t i = 0;
986
987         if (req->oldptr) {
988                 i = l;
989                 if (i > req->oldlen - req->oldidx)
990                         i = req->oldlen - req->oldidx;
991                 if (i > 0)
992                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
993         }
994         req->oldidx += l;
995         if (req->oldptr && i != l)
996                 return (ENOMEM);
997         return (0);
998 }
999
1000 static int
1001 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1002 {
1003
1004         if (!req->newptr)
1005                 return 0;
1006         if (req->newlen - req->newidx < l)
1007                 return (EINVAL);
1008         bcopy((char *)req->newptr + req->newidx, p, l);
1009         req->newidx += l;
1010         return (0);
1011 }
1012
1013 int
1014 kernel_sysctl(int *name, u_int namelen,
1015               void *old, size_t *oldlenp,
1016               void *new, size_t newlen, size_t *retval)
1017 {
1018         int error = 0;
1019         struct sysctl_req req;
1020
1021         bzero(&req, sizeof req);
1022
1023         req.td = curthread;
1024
1025         if (oldlenp) {
1026                 req.oldlen = *oldlenp;
1027         }
1028         req.validlen = req.oldlen;
1029
1030         if (old) {
1031                 req.oldptr= old;
1032         }
1033
1034         if (new != NULL) {
1035                 req.newlen = newlen;
1036                 req.newptr = new;
1037         }
1038
1039         req.oldfunc = sysctl_old_kernel;
1040         req.newfunc = sysctl_new_kernel;
1041 #if 0
1042         req.lock = REQ_UNWIRED;
1043 #endif
1044
1045         SYSCTL_SLOCK();
1046         error = sysctl_root(0, name, namelen, &req);
1047         SYSCTL_SUNLOCK();
1048
1049 #if 0
1050         if (req.lock == REQ_WIRED && req.validlen > 0)
1051                 vsunlock(req.oldptr, req.validlen);
1052 #endif
1053
1054         if (error && error != ENOMEM)
1055                 return (error);
1056
1057         if (retval) {
1058                 if (req.oldptr && req.oldidx > req.validlen)
1059                         *retval = req.validlen;
1060                 else
1061                         *retval = req.oldidx;
1062         }
1063         return (error);
1064 }
1065
1066 int
1067 kernel_sysctlbyname(char *name,
1068                     void *old, size_t *oldlenp,
1069                     void *new, size_t newlen, size_t *retval)
1070 {
1071         int oid[CTL_MAXNAME];
1072         size_t oidlen, plen;
1073         int error;
1074
1075         oid[0] = 0;             /* sysctl internal magic */
1076         oid[1] = 3;             /* name2oid */
1077         oidlen = sizeof(oid);
1078
1079         error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1080         if (error)
1081                 return (error);
1082
1083         error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1084             new, newlen, retval);
1085         return (error);
1086 }
1087
1088 /*
1089  * Transfer function to/from user space.
1090  */
1091 static int
1092 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1093 {
1094         int error = 0;
1095         size_t i = 0;
1096
1097 #if 0
1098         if (req->lock == 1 && req->oldptr) {
1099                 vslock(req->oldptr, req->oldlen);
1100                 req->lock = 2;
1101         }
1102 #endif
1103         if (req->oldptr) {
1104                 i = l;
1105                 if (i > req->oldlen - req->oldidx)
1106                         i = req->oldlen - req->oldidx;
1107                 if (i > 0)
1108                         error = copyout(p, (char *)req->oldptr + req->oldidx,
1109                                         i);
1110         }
1111         req->oldidx += l;
1112         if (error)
1113                 return (error);
1114         if (req->oldptr && i < l)
1115                 return (ENOMEM);
1116         return (0);
1117 }
1118
1119 static int
1120 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1121 {
1122         int error;
1123
1124         if (!req->newptr)
1125                 return 0;
1126         if (req->newlen - req->newidx < l)
1127                 return (EINVAL);
1128         error = copyin((char *)req->newptr + req->newidx, p, l);
1129         req->newidx += l;
1130         return (error);
1131 }
1132
1133 int
1134 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1135                 int *nindx, struct sysctl_req *req)
1136 {
1137         struct sysctl_oid_list *lsp;
1138         struct sysctl_oid *oid;
1139         int indx;
1140
1141         lsp = &sysctl__children;
1142         indx = 0;
1143         while (indx < CTL_MAXNAME) {
1144                 SLIST_FOREACH(oid, lsp, oid_link) {
1145                         if (oid->oid_number == name[indx])
1146                                 break;
1147                 }
1148                 if (oid == NULL)
1149                         return (ENOENT);
1150
1151                 indx++;
1152                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1153                         if (oid->oid_handler != NULL || indx == namelen) {
1154                                 *noid = oid;
1155                                 if (nindx != NULL)
1156                                         *nindx = indx;
1157                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1158                                     ("%s found DYING node %p", __func__, oid));
1159                                 return (0);
1160                         }
1161                         lsp = SYSCTL_CHILDREN(oid);
1162                 } else if (indx == namelen) {
1163                         *noid = oid;
1164                         if (nindx != NULL)
1165                                 *nindx = indx;
1166                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1167                             ("%s found DYING node %p", __func__, oid));
1168                         return (0);
1169                 } else {
1170                         return (ENOTDIR);
1171                 }
1172         }
1173         return (ENOENT);
1174 }
1175
1176 /*
1177  * Traverse our tree, and find the right node, execute whatever it points
1178  * to, and return the resulting error code.
1179  */
1180 static int
1181 sysctl_root(SYSCTL_HANDLER_ARGS)
1182 {
1183         struct thread *td = req->td;
1184         struct proc *p = td ? td->td_proc : NULL;
1185         struct sysctl_oid *oid;
1186         int error, indx;
1187         int lktype;
1188
1189         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1190         if (error)
1191                 return (error);
1192
1193         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1194                 /*
1195                  * You can't call a sysctl when it's a node, but has
1196                  * no handler.  Inform the user that it's a node.
1197                  * The indx may or may not be the same as namelen.
1198                  */
1199                 if (oid->oid_handler == NULL)
1200                         return (EISDIR);
1201         }
1202
1203         /* If writing isn't allowed */
1204         if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1205             ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1206                 return (EPERM);
1207
1208         /* Most likely only root can write */
1209         if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1210             (error = priv_check_cred(td->td_ucred,
1211              (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1212                                                 PRIV_SYSCTL_WRITE, 0)))
1213                 return (error);
1214
1215         if (oid->oid_handler == NULL)
1216                 return EINVAL;
1217
1218         /*
1219          * Default oid locking is exclusive when modifying (newptr),
1220          * shared otherwise, unless overridden with a control flag.
1221          */
1222         lktype = (req->newptr != NULL) ? LK_EXCLUSIVE : LK_SHARED;
1223         if (oid->oid_kind & CTLFLAG_SHLOCK)
1224                 lktype = LK_SHARED;
1225         if (oid->oid_kind & CTLFLAG_EXLOCK)
1226                 lktype = LK_EXCLUSIVE;
1227         lockmgr(&oid->oid_lock, lktype);
1228
1229         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1230                 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1231                                          req);
1232         else
1233                 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1234                                          req);
1235         lockmgr(&oid->oid_lock, LK_RELEASE);
1236
1237         return (error);
1238 }
1239
1240 int
1241 sys___sysctl(struct sysctl_args *uap)
1242 {
1243         int error, i, name[CTL_MAXNAME];
1244         size_t j;
1245
1246         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1247                 return (EINVAL);
1248
1249         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1250         if (error)
1251                 return (error);
1252
1253         error = userland_sysctl(name, uap->namelen,
1254                                 uap->old, uap->oldlenp, 0,
1255                                 uap->new, uap->newlen, &j);
1256         if (error && error != ENOMEM)
1257                 return (error);
1258         if (uap->oldlenp) {
1259                 i = copyout(&j, uap->oldlenp, sizeof(j));
1260                 if (i)
1261                         return (i);
1262         }
1263         return (error);
1264 }
1265
1266 /*
1267  * This is used from various compatibility syscalls too.  That's why name
1268  * must be in kernel space.
1269  */
1270 int
1271 userland_sysctl(int *name, u_int namelen,
1272                 void *old, size_t *oldlenp, int inkernel,
1273                 void *new, size_t newlen, size_t *retval)
1274 {
1275         int error = 0, memlocked;
1276         struct sysctl_req req;
1277
1278         bzero(&req, sizeof req);
1279
1280         req.td = curthread;
1281         req.flags = 0;
1282
1283         if (oldlenp) {
1284                 if (inkernel) {
1285                         req.oldlen = *oldlenp;
1286                 } else {
1287                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1288                         if (error)
1289                                 return (error);
1290                 }
1291         }
1292         req.validlen = req.oldlen;
1293
1294         if (old) {
1295                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1296                         return (EFAULT);
1297                 req.oldptr= old;
1298         }
1299
1300         if (new != NULL) {
1301                 if (!useracc(new, newlen, VM_PROT_READ))
1302                         return (EFAULT);
1303                 req.newlen = newlen;
1304                 req.newptr = new;
1305         }
1306
1307         req.oldfunc = sysctl_old_user;
1308         req.newfunc = sysctl_new_user;
1309 #if 0
1310         req.lock = REQ_UNWIRED;
1311 #endif
1312
1313 #ifdef KTRACE
1314         if (KTRPOINT(curthread, KTR_SYSCTL))
1315                 ktrsysctl(name, namelen);
1316 #endif
1317
1318         if (req.oldlen > PAGE_SIZE) {
1319                 memlocked = 1;
1320                 lockmgr(&sysctlmemlock, LK_EXCLUSIVE);
1321         } else
1322                 memlocked = 0;
1323
1324         for (;;) {
1325                 req.oldidx = 0;
1326                 req.newidx = 0;
1327                 SYSCTL_SLOCK();
1328                 error = sysctl_root(0, name, namelen, &req);
1329                 SYSCTL_SUNLOCK();
1330                 if (error != EAGAIN)
1331                         break;
1332                 lwkt_yield();
1333         }
1334
1335 #if 0
1336         if (req.lock == REQ_WIRED && req.validlen > 0)
1337                 vsunlock(req.oldptr, req.validlen);
1338 #endif
1339         if (memlocked)
1340                 lockmgr(&sysctlmemlock, LK_RELEASE);
1341
1342         if (error && error != ENOMEM)
1343                 return (error);
1344
1345         if (retval) {
1346                 if (req.oldptr && req.oldidx > req.validlen)
1347                         *retval = req.validlen;
1348                 else
1349                         *retval = req.oldidx;
1350         }
1351         return (error);
1352 }
1353
1354 int
1355 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1356 {
1357         int error, value;
1358
1359         value = *(int *)arg1;
1360         error = sysctl_handle_int(oidp, &value, 0, req);
1361         if (error || !req->newptr)
1362                 return (error);
1363         if (value < low || value > high)
1364                 return (EINVAL);
1365         *(int *)arg1 = value;
1366         return (0);
1367 }
1368
1369 /*
1370  * Drain into a sysctl struct.  The user buffer should be wired if a page
1371  * fault would cause issue.
1372  */
1373 static int
1374 sbuf_sysctl_drain(void *arg, const char *data, int len)
1375 {
1376         struct sysctl_req *req = arg;
1377         int error;
1378
1379         error = SYSCTL_OUT(req, data, len);
1380         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1381         return (error == 0 ? len : -error);
1382 }
1383
1384 struct sbuf *
1385 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1386     struct sysctl_req *req)
1387 {
1388
1389         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1390         sbuf_set_drain(s, sbuf_sysctl_drain, req);
1391         return (s);
1392 }