geom_kern.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2002 Poul-Henning Kamp
  5. * Copyright (c) 2002 Networks Associates Technology, Inc.
  6. * All rights reserved.
  7. *
  8. * This software was developed for the FreeBSD Project by Poul-Henning Kamp
  9. * and NAI Labs, the Security Research Division of Network Associates, Inc.
  10. * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
  11. * DARPA CHATS research program.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. The names of the authors may not be used to endorse or promote
  22. * products derived from this software without specific prior written
  23. * permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/kernel.h>
  40. #include <sys/eventhandler.h>
  41. #include <sys/malloc.h>
  42. #include <sys/bio.h>
  43. #include <sys/sysctl.h>
  44. #include <sys/proc.h>
  45. #include <sys/unistd.h>
  46. #include <sys/kthread.h>
  47. #include <sys/lock.h>
  48. #include <sys/mutex.h>
  49. #include <sys/sbuf.h>
  50. #include <sys/sched.h>
  51. #include <sys/sx.h>
  52. #include <geom/geom.h>
  53. #include <geom/geom_int.h>
  54. MALLOC_DEFINE(M_GEOM, "GEOM", "Geom data structures");
  55. struct sx topology_lock;
  56. static struct proc *g_proc;
  57. struct thread __read_mostly *g_up_td;
  58. struct thread __read_mostly *g_down_td;
  59. static struct thread __read_mostly *g_event_td;
  60. int __read_mostly g_debugflags;
  61. int __read_mostly g_collectstats = G_STATS_PROVIDERS;
  62. int g_shutdown;
  63. int g_notaste;
  64. /*
  65. * G_UP and G_DOWN are the two threads which push I/O through the
  66. * stack.
  67. *
  68. * Things are procesed in a FIFO order, but these threads could be
  69. * part of I/O prioritization by deciding which bios/bioqs to service
  70. * in what order.
  71. *
  72. * We have only one thread in each direction, it is believed that until
  73. * a very non-trivial workload in the UP/DOWN path this will be enough,
  74. * but more than one can actually be run without problems.
  75. *
  76. * Holding the "mymutex" is a debugging feature: It prevents people
  77. * from sleeping in the UP/DOWN I/O path by mistake or design (doing
  78. * so almost invariably result in deadlocks since it stalls all I/O
  79. * processing in the given direction.
  80. */
  81. static void
  82. g_up_procbody(void *arg)
  83. {
  84. thread_lock(g_up_td);
  85. sched_prio(g_up_td, PRIBIO);
  86. thread_unlock(g_up_td);
  87. for(;;) {
  88. g_io_schedule_up(g_up_td);
  89. }
  90. }
  91. static void
  92. g_down_procbody(void *arg)
  93. {
  94. thread_lock(g_down_td);
  95. sched_prio(g_down_td, PRIBIO);
  96. thread_unlock(g_down_td);
  97. for(;;) {
  98. g_io_schedule_down(g_down_td);
  99. }
  100. }
  101. static void
  102. g_event_procbody(void *arg)
  103. {
  104. thread_lock(g_event_td);
  105. sched_prio(g_event_td, PRIBIO);
  106. thread_unlock(g_event_td);
  107. g_run_events();
  108. /* NOTREACHED */
  109. }
  110. int
  111. g_is_geom_thread(struct thread *td)
  112. {
  113. return (td == g_up_td || td == g_down_td || td == g_event_td);
  114. }
  115. static void
  116. geom_shutdown(void *foo __unused)
  117. {
  118. g_shutdown = 1;
  119. }
  120. void
  121. g_init(void)
  122. {
  123. g_trace(G_T_TOPOLOGY, "g_ignition");
  124. sx_init(&topology_lock, "GEOM topology");
  125. g_io_init();
  126. g_event_init();
  127. g_ctl_init();
  128. kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td,
  129. RFHIGHPID, 0, "geom", "g_event");
  130. kproc_kthread_add(g_up_procbody, NULL, &g_proc, &g_up_td,
  131. RFHIGHPID, 0, "geom", "g_up");
  132. kproc_kthread_add(g_down_procbody, NULL, &g_proc, &g_down_td,
  133. RFHIGHPID, 0, "geom", "g_down");
  134. EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL,
  135. SHUTDOWN_PRI_FIRST);
  136. }
  137. static int
  138. sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
  139. {
  140. size_t len = 0;
  141. int error = 0;
  142. struct sbuf *sb;
  143. if (req->oldptr == NULL) {
  144. sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
  145. SBUF_INCLUDENUL);
  146. sbuf_set_drain(sb, sbuf_count_drain, &len);
  147. g_waitfor_event(func, sb, M_WAITOK, NULL);
  148. req->oldidx = *hint = len;
  149. } else {
  150. sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
  151. SBUF_INCLUDENUL);
  152. g_waitfor_event(func, sb, M_WAITOK, NULL);
  153. *hint = sbuf_len(sb);
  154. error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
  155. }
  156. sbuf_delete(sb);
  157. return error;
  158. }
  159. static int
  160. sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
  161. {
  162. static size_t hint = PAGE_SIZE;
  163. return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
  164. }
  165. static int
  166. sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
  167. {
  168. static size_t hint = PAGE_SIZE;
  169. return (sysctl_kern_geom_confany(req, g_confdot, &hint));
  170. }
  171. static int
  172. sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
  173. {
  174. static size_t hint = PAGE_SIZE;
  175. return (sysctl_kern_geom_confany(req, g_confxml, &hint));
  176. }
  177. SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  178. "GEOMetry management");
  179. SYSCTL_PROC(_kern_geom, OID_AUTO, confxml,
  180. CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
  181. sysctl_kern_geom_confxml, "",
  182. "Dump the GEOM config in XML");
  183. SYSCTL_PROC(_kern_geom, OID_AUTO, confdot,
  184. CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
  185. sysctl_kern_geom_confdot, "",
  186. "Dump the GEOM config in dot");
  187. SYSCTL_PROC(_kern_geom, OID_AUTO, conftxt,
  188. CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
  189. sysctl_kern_geom_conftxt, "",
  190. "Dump the GEOM config in txt");
  191. SYSCTL_INT(_kern_geom, OID_AUTO, debugflags, CTLFLAG_RWTUN,
  192. &g_debugflags, 0, "Set various trace levels for GEOM debugging");
  193. SYSCTL_INT(_kern_geom, OID_AUTO, notaste, CTLFLAG_RW,
  194. &g_notaste, 0, "Prevent GEOM tasting");
  195. SYSCTL_INT(_kern_geom, OID_AUTO, collectstats, CTLFLAG_RW,
  196. &g_collectstats, 0,
  197. "Control statistics collection on GEOM providers and consumers");
  198. SYSCTL_INT(_debug_sizeof, OID_AUTO, g_class, CTLFLAG_RD,
  199. SYSCTL_NULL_INT_PTR, sizeof(struct g_class), "sizeof(struct g_class)");
  200. SYSCTL_INT(_debug_sizeof, OID_AUTO, g_geom, CTLFLAG_RD,
  201. SYSCTL_NULL_INT_PTR, sizeof(struct g_geom), "sizeof(struct g_geom)");
  202. SYSCTL_INT(_debug_sizeof, OID_AUTO, g_provider, CTLFLAG_RD,
  203. SYSCTL_NULL_INT_PTR, sizeof(struct g_provider), "sizeof(struct g_provider)");
  204. SYSCTL_INT(_debug_sizeof, OID_AUTO, g_consumer, CTLFLAG_RD,
  205. SYSCTL_NULL_INT_PTR, sizeof(struct g_consumer), "sizeof(struct g_consumer)");
  206. SYSCTL_INT(_debug_sizeof, OID_AUTO, g_bioq, CTLFLAG_RD,
  207. SYSCTL_NULL_INT_PTR, sizeof(struct g_bioq), "sizeof(struct g_bioq)");