geom_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /*
  38. * XXX: How do we in general know that objects referenced in events
  39. * have not been destroyed before we get around to handle the event ?
  40. */
  41. #include <sys/param.h>
  42. #include <sys/malloc.h>
  43. #include <sys/systm.h>
  44. #include <sys/kernel.h>
  45. #include <sys/lock.h>
  46. #include <sys/mutex.h>
  47. #include <sys/proc.h>
  48. #include <sys/errno.h>
  49. #include <sys/time.h>
  50. #include <geom/geom.h>
  51. #include <geom/geom_int.h>
  52. #include <machine/stdarg.h>
  53. TAILQ_HEAD(event_tailq_head, g_event);
  54. static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
  55. static u_int g_pending_events;
  56. static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
  57. static struct mtx g_eventlock;
  58. static int g_wither_work;
  59. #define G_N_EVENTREFS 20
  60. struct g_event {
  61. TAILQ_ENTRY(g_event) events;
  62. g_event_t *func;
  63. void *arg;
  64. int flag;
  65. void *ref[G_N_EVENTREFS];
  66. };
  67. #define EV_DONE 0x80000
  68. #define EV_WAKEUP 0x40000
  69. #define EV_CANCELED 0x20000
  70. #define EV_INPROGRESS 0x10000
  71. void
  72. g_waitidle(struct thread *td)
  73. {
  74. g_topology_assert_not();
  75. mtx_lock(&g_eventlock);
  76. TSWAIT("GEOM events");
  77. while (!TAILQ_EMPTY(&g_events))
  78. msleep(&g_pending_events, &g_eventlock, PPAUSE,
  79. "g_waitidle", 0);
  80. TSUNWAIT("GEOM events");
  81. mtx_unlock(&g_eventlock);
  82. td->td_pflags &= ~TDP_GEOM;
  83. }
  84. static void
  85. ast_geom(struct thread *td, int tda __unused)
  86. {
  87. /*
  88. * If this thread tickled GEOM, we need to wait for the giggling to
  89. * stop before we return to userland.
  90. */
  91. g_waitidle(td);
  92. }
  93. static void
  94. geom_event_init(void *arg __unused)
  95. {
  96. ast_register(TDA_GEOM, ASTR_ASTF_REQUIRED | ASTR_TDP | ASTR_KCLEAR,
  97. TDP_GEOM, ast_geom);
  98. }
  99. SYSINIT(geom_event, SI_SUB_INTRINSIC, SI_ORDER_ANY, geom_event_init, NULL);
  100. struct g_attrchanged_args {
  101. struct g_provider *pp;
  102. const char *attr;
  103. };
  104. static void
  105. g_attr_changed_event(void *arg, int flag)
  106. {
  107. struct g_attrchanged_args *args;
  108. struct g_provider *pp;
  109. struct g_consumer *cp;
  110. struct g_consumer *next_cp;
  111. args = arg;
  112. pp = args->pp;
  113. g_topology_assert();
  114. if (flag != EV_CANCEL && g_shutdown == 0) {
  115. /*
  116. * Tell all consumers of the change.
  117. */
  118. LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
  119. if (cp->geom->attrchanged != NULL)
  120. cp->geom->attrchanged(cp, args->attr);
  121. }
  122. }
  123. g_free(args);
  124. }
  125. int
  126. g_attr_changed(struct g_provider *pp, const char *attr, int flag)
  127. {
  128. struct g_attrchanged_args *args;
  129. int error;
  130. args = g_malloc(sizeof *args, flag);
  131. if (args == NULL)
  132. return (ENOMEM);
  133. args->pp = pp;
  134. args->attr = attr;
  135. error = g_post_event(g_attr_changed_event, args, flag, pp, NULL);
  136. if (error != 0)
  137. g_free(args);
  138. return (error);
  139. }
  140. void
  141. g_orphan_provider(struct g_provider *pp, int error)
  142. {
  143. /* G_VALID_PROVIDER(pp) We likely lack topology lock */
  144. g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
  145. pp, pp->name, error);
  146. KASSERT(error != 0,
  147. ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
  148. pp, pp->name));
  149. pp->error = error;
  150. mtx_lock(&g_eventlock);
  151. KASSERT(!(pp->flags & G_PF_ORPHAN),
  152. ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
  153. pp->flags |= G_PF_ORPHAN;
  154. TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
  155. mtx_unlock(&g_eventlock);
  156. wakeup(&g_wait_event);
  157. }
  158. /*
  159. * This function is called once on each provider which the event handler
  160. * finds on its g_doorstep.
  161. */
  162. static void
  163. g_orphan_register(struct g_provider *pp)
  164. {
  165. struct g_consumer *cp, *cp2;
  166. int wf;
  167. g_topology_assert();
  168. G_VALID_PROVIDER(pp);
  169. g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
  170. g_cancel_event(pp);
  171. wf = pp->flags & G_PF_WITHER;
  172. pp->flags &= ~G_PF_WITHER;
  173. /*
  174. * Tell all consumers the bad news.
  175. * Don't be surprised if they self-destruct.
  176. */
  177. LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
  178. KASSERT(cp->geom->orphan != NULL,
  179. ("geom %s has no orphan, class %s",
  180. cp->geom->name, cp->geom->class->name));
  181. /*
  182. * XXX: g_dev_orphan method does deferred destroying
  183. * and it is possible, that other event could already
  184. * call the orphan method. Check consumer's flags to
  185. * do not schedule it twice.
  186. */
  187. if (cp->flags & G_CF_ORPHAN)
  188. continue;
  189. cp->flags |= G_CF_ORPHAN;
  190. cp->geom->orphan(cp);
  191. }
  192. if (LIST_EMPTY(&pp->consumers) && wf)
  193. g_destroy_provider(pp);
  194. else
  195. pp->flags |= wf;
  196. #ifdef notyet
  197. cp = LIST_FIRST(&pp->consumers);
  198. if (cp != NULL)
  199. return;
  200. if (pp->geom->flags & G_GEOM_WITHER)
  201. g_destroy_provider(pp);
  202. #endif
  203. }
  204. static int
  205. one_event(void)
  206. {
  207. struct g_event *ep;
  208. struct g_provider *pp;
  209. g_topology_assert();
  210. mtx_lock(&g_eventlock);
  211. pp = TAILQ_FIRST(&g_doorstep);
  212. if (pp != NULL) {
  213. G_VALID_PROVIDER(pp);
  214. TAILQ_REMOVE(&g_doorstep, pp, orphan);
  215. mtx_unlock(&g_eventlock);
  216. g_orphan_register(pp);
  217. return (1);
  218. }
  219. ep = TAILQ_FIRST(&g_events);
  220. if (ep == NULL) {
  221. wakeup(&g_pending_events);
  222. return (0);
  223. }
  224. ep->flag |= EV_INPROGRESS;
  225. mtx_unlock(&g_eventlock);
  226. g_topology_assert();
  227. ep->func(ep->arg, 0);
  228. g_topology_assert();
  229. mtx_lock(&g_eventlock);
  230. TSRELEASE("GEOM events");
  231. TAILQ_REMOVE(&g_events, ep, events);
  232. ep->flag &= ~EV_INPROGRESS;
  233. if (ep->flag & EV_WAKEUP) {
  234. ep->flag |= EV_DONE;
  235. wakeup(ep);
  236. mtx_unlock(&g_eventlock);
  237. } else {
  238. mtx_unlock(&g_eventlock);
  239. g_free(ep);
  240. }
  241. return (1);
  242. }
  243. void
  244. g_run_events(void)
  245. {
  246. for (;;) {
  247. g_topology_lock();
  248. while (one_event())
  249. ;
  250. mtx_assert(&g_eventlock, MA_OWNED);
  251. if (g_wither_work) {
  252. g_wither_work = 0;
  253. mtx_unlock(&g_eventlock);
  254. g_wither_washer();
  255. g_topology_unlock();
  256. } else {
  257. g_topology_unlock();
  258. msleep(&g_wait_event, &g_eventlock, PRIBIO | PDROP,
  259. "-", 0);
  260. }
  261. }
  262. /* NOTREACHED */
  263. }
  264. void
  265. g_cancel_event(void *ref)
  266. {
  267. struct g_event *ep, *epn;
  268. struct g_provider *pp;
  269. u_int n;
  270. mtx_lock(&g_eventlock);
  271. TAILQ_FOREACH(pp, &g_doorstep, orphan) {
  272. if (pp != ref)
  273. continue;
  274. TAILQ_REMOVE(&g_doorstep, pp, orphan);
  275. break;
  276. }
  277. TAILQ_FOREACH_SAFE(ep, &g_events, events, epn) {
  278. if (ep->flag & EV_INPROGRESS)
  279. continue;
  280. for (n = 0; n < G_N_EVENTREFS; n++) {
  281. if (ep->ref[n] == NULL)
  282. break;
  283. if (ep->ref[n] != ref)
  284. continue;
  285. TSRELEASE("GEOM events");
  286. TAILQ_REMOVE(&g_events, ep, events);
  287. ep->func(ep->arg, EV_CANCEL);
  288. mtx_assert(&g_eventlock, MA_OWNED);
  289. if (ep->flag & EV_WAKEUP) {
  290. ep->flag |= (EV_DONE|EV_CANCELED);
  291. wakeup(ep);
  292. } else {
  293. g_free(ep);
  294. }
  295. break;
  296. }
  297. }
  298. if (TAILQ_EMPTY(&g_events))
  299. wakeup(&g_pending_events);
  300. mtx_unlock(&g_eventlock);
  301. }
  302. struct g_event *
  303. g_alloc_event(int flag)
  304. {
  305. KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
  306. ("Wrong flag to g_alloc_event"));
  307. return (g_malloc(sizeof(struct g_event), flag | M_ZERO));
  308. }
  309. static void
  310. g_post_event_ep_va(g_event_t *func, void *arg, int wuflag,
  311. struct g_event *ep, va_list ap)
  312. {
  313. void *p;
  314. u_int n;
  315. ep->flag = wuflag;
  316. for (n = 0; n < G_N_EVENTREFS; n++) {
  317. p = va_arg(ap, void *);
  318. if (p == NULL)
  319. break;
  320. g_trace(G_T_TOPOLOGY, " ref %p", p);
  321. ep->ref[n] = p;
  322. }
  323. KASSERT(p == NULL, ("Too many references to event"));
  324. ep->func = func;
  325. ep->arg = arg;
  326. mtx_lock(&g_eventlock);
  327. TSHOLD("GEOM events");
  328. TAILQ_INSERT_TAIL(&g_events, ep, events);
  329. mtx_unlock(&g_eventlock);
  330. wakeup(&g_wait_event);
  331. curthread->td_pflags |= TDP_GEOM;
  332. ast_sched(curthread, TDA_GEOM);
  333. }
  334. void
  335. g_post_event_ep(g_event_t *func, void *arg, struct g_event *ep, ...)
  336. {
  337. va_list ap;
  338. va_start(ap, ep);
  339. g_post_event_ep_va(func, arg, 0, ep, ap);
  340. va_end(ap);
  341. }
  342. static int
  343. g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
  344. {
  345. struct g_event *ep;
  346. g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
  347. func, arg, flag, wuflag);
  348. KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
  349. ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
  350. ep = g_alloc_event(flag);
  351. if (ep == NULL)
  352. return (ENOMEM);
  353. if (epp != NULL)
  354. *epp = ep;
  355. g_post_event_ep_va(func, arg, wuflag, ep, ap);
  356. return (0);
  357. }
  358. int
  359. g_post_event(g_event_t *func, void *arg, int flag, ...)
  360. {
  361. va_list ap;
  362. int i;
  363. KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
  364. ("Wrong flag to g_post_event"));
  365. va_start(ap, flag);
  366. i = g_post_event_x(func, arg, flag, 0, NULL, ap);
  367. va_end(ap);
  368. return (i);
  369. }
  370. void
  371. g_do_wither(void)
  372. {
  373. mtx_lock(&g_eventlock);
  374. g_wither_work = 1;
  375. mtx_unlock(&g_eventlock);
  376. wakeup(&g_wait_event);
  377. }
  378. /*
  379. * XXX: It might actually be useful to call this function with topology held.
  380. * XXX: This would ensure that the event gets created before anything else
  381. * XXX: changes. At present all users have a handle on things in some other
  382. * XXX: way, so this remains an XXX for now.
  383. */
  384. int
  385. g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
  386. {
  387. va_list ap;
  388. struct g_event *ep;
  389. int error;
  390. g_topology_assert_not();
  391. KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
  392. ("Wrong flag to g_post_event"));
  393. va_start(ap, flag);
  394. error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
  395. va_end(ap);
  396. if (error)
  397. return (error);
  398. mtx_lock(&g_eventlock);
  399. while (!(ep->flag & EV_DONE))
  400. msleep(ep, &g_eventlock, PRIBIO, "g_waitfor_event", 0);
  401. if (ep->flag & EV_CANCELED)
  402. error = EAGAIN;
  403. mtx_unlock(&g_eventlock);
  404. g_free(ep);
  405. return (error);
  406. }
  407. void
  408. g_event_init(void)
  409. {
  410. mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);
  411. }