/contrib/bind9/lib/dns/zt.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 411 lines · 302 code · 83 blank · 26 comment · 81 complexity · 17ba027d65e08c3a192a0e7cdfe46dc8 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004-2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2002 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <isc/file.h>
  21. #include <isc/magic.h>
  22. #include <isc/mem.h>
  23. #include <isc/string.h>
  24. #include <isc/util.h>
  25. #include <dns/log.h>
  26. #include <dns/name.h>
  27. #include <dns/rbt.h>
  28. #include <dns/rdataclass.h>
  29. #include <dns/result.h>
  30. #include <dns/view.h>
  31. #include <dns/zone.h>
  32. #include <dns/zt.h>
  33. struct dns_zt {
  34. /* Unlocked. */
  35. unsigned int magic;
  36. isc_mem_t *mctx;
  37. dns_rdataclass_t rdclass;
  38. isc_rwlock_t rwlock;
  39. /* Locked by lock. */
  40. isc_uint32_t references;
  41. dns_rbt_t *table;
  42. };
  43. #define ZTMAGIC ISC_MAGIC('Z', 'T', 'b', 'l')
  44. #define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
  45. static void
  46. auto_detach(void *, void *);
  47. static isc_result_t
  48. load(dns_zone_t *zone, void *uap);
  49. static isc_result_t
  50. loadnew(dns_zone_t *zone, void *uap);
  51. static isc_result_t
  52. freezezones(dns_zone_t *zone, void *uap);
  53. isc_result_t
  54. dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp)
  55. {
  56. dns_zt_t *zt;
  57. isc_result_t result;
  58. REQUIRE(ztp != NULL && *ztp == NULL);
  59. zt = isc_mem_get(mctx, sizeof(*zt));
  60. if (zt == NULL)
  61. return (ISC_R_NOMEMORY);
  62. zt->table = NULL;
  63. result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
  64. if (result != ISC_R_SUCCESS)
  65. goto cleanup_zt;
  66. result = isc_rwlock_init(&zt->rwlock, 0, 0);
  67. if (result != ISC_R_SUCCESS)
  68. goto cleanup_rbt;
  69. zt->mctx = mctx;
  70. zt->references = 1;
  71. zt->rdclass = rdclass;
  72. zt->magic = ZTMAGIC;
  73. *ztp = zt;
  74. return (ISC_R_SUCCESS);
  75. cleanup_rbt:
  76. dns_rbt_destroy(&zt->table);
  77. cleanup_zt:
  78. isc_mem_put(mctx, zt, sizeof(*zt));
  79. return (result);
  80. }
  81. isc_result_t
  82. dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
  83. isc_result_t result;
  84. dns_zone_t *dummy = NULL;
  85. dns_name_t *name;
  86. REQUIRE(VALID_ZT(zt));
  87. name = dns_zone_getorigin(zone);
  88. RWLOCK(&zt->rwlock, isc_rwlocktype_write);
  89. result = dns_rbt_addname(zt->table, name, zone);
  90. if (result == ISC_R_SUCCESS)
  91. dns_zone_attach(zone, &dummy);
  92. RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
  93. return (result);
  94. }
  95. isc_result_t
  96. dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
  97. isc_result_t result;
  98. dns_name_t *name;
  99. REQUIRE(VALID_ZT(zt));
  100. name = dns_zone_getorigin(zone);
  101. RWLOCK(&zt->rwlock, isc_rwlocktype_write);
  102. result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
  103. RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
  104. return (result);
  105. }
  106. isc_result_t
  107. dns_zt_find(dns_zt_t *zt, dns_name_t *name, unsigned int options,
  108. dns_name_t *foundname, dns_zone_t **zonep)
  109. {
  110. isc_result_t result;
  111. dns_zone_t *dummy = NULL;
  112. unsigned int rbtoptions = 0;
  113. REQUIRE(VALID_ZT(zt));
  114. if ((options & DNS_ZTFIND_NOEXACT) != 0)
  115. rbtoptions |= DNS_RBTFIND_NOEXACT;
  116. RWLOCK(&zt->rwlock, isc_rwlocktype_read);
  117. result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
  118. (void **) (void*)&dummy);
  119. if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
  120. dns_zone_attach(dummy, zonep);
  121. RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
  122. return (result);
  123. }
  124. void
  125. dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
  126. REQUIRE(VALID_ZT(zt));
  127. REQUIRE(ztp != NULL && *ztp == NULL);
  128. RWLOCK(&zt->rwlock, isc_rwlocktype_write);
  129. INSIST(zt->references > 0);
  130. zt->references++;
  131. INSIST(zt->references != 0);
  132. RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
  133. *ztp = zt;
  134. }
  135. static isc_result_t
  136. flush(dns_zone_t *zone, void *uap) {
  137. UNUSED(uap);
  138. return (dns_zone_flush(zone));
  139. }
  140. static void
  141. zt_flushanddetach(dns_zt_t **ztp, isc_boolean_t need_flush) {
  142. isc_boolean_t destroy = ISC_FALSE;
  143. dns_zt_t *zt;
  144. REQUIRE(ztp != NULL && VALID_ZT(*ztp));
  145. zt = *ztp;
  146. RWLOCK(&zt->rwlock, isc_rwlocktype_write);
  147. INSIST(zt->references > 0);
  148. zt->references--;
  149. if (zt->references == 0)
  150. destroy = ISC_TRUE;
  151. RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
  152. if (destroy) {
  153. if (need_flush)
  154. (void)dns_zt_apply(zt, ISC_FALSE, flush, NULL);
  155. dns_rbt_destroy(&zt->table);
  156. isc_rwlock_destroy(&zt->rwlock);
  157. zt->magic = 0;
  158. isc_mem_put(zt->mctx, zt, sizeof(*zt));
  159. }
  160. *ztp = NULL;
  161. }
  162. void
  163. dns_zt_flushanddetach(dns_zt_t **ztp) {
  164. zt_flushanddetach(ztp, ISC_TRUE);
  165. }
  166. void
  167. dns_zt_detach(dns_zt_t **ztp) {
  168. zt_flushanddetach(ztp, ISC_FALSE);
  169. }
  170. isc_result_t
  171. dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
  172. isc_result_t result;
  173. REQUIRE(VALID_ZT(zt));
  174. RWLOCK(&zt->rwlock, isc_rwlocktype_read);
  175. result = dns_zt_apply(zt, stop, load, NULL);
  176. RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
  177. return (result);
  178. }
  179. static isc_result_t
  180. load(dns_zone_t *zone, void *uap) {
  181. isc_result_t result;
  182. UNUSED(uap);
  183. result = dns_zone_load(zone);
  184. if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE)
  185. result = ISC_R_SUCCESS;
  186. return (result);
  187. }
  188. isc_result_t
  189. dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
  190. isc_result_t result;
  191. REQUIRE(VALID_ZT(zt));
  192. RWLOCK(&zt->rwlock, isc_rwlocktype_read);
  193. result = dns_zt_apply(zt, stop, loadnew, NULL);
  194. RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
  195. return (result);
  196. }
  197. static isc_result_t
  198. loadnew(dns_zone_t *zone, void *uap) {
  199. isc_result_t result;
  200. UNUSED(uap);
  201. result = dns_zone_loadnew(zone);
  202. if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
  203. result == DNS_R_DYNAMIC)
  204. result = ISC_R_SUCCESS;
  205. return (result);
  206. }
  207. isc_result_t
  208. dns_zt_freezezones(dns_zt_t *zt, isc_boolean_t freeze) {
  209. isc_result_t result, tresult;
  210. REQUIRE(VALID_ZT(zt));
  211. RWLOCK(&zt->rwlock, isc_rwlocktype_read);
  212. result = dns_zt_apply2(zt, ISC_FALSE, &tresult, freezezones, &freeze);
  213. RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
  214. return ((result == ISC_R_SUCCESS) ? tresult : result);
  215. }
  216. static isc_result_t
  217. freezezones(dns_zone_t *zone, void *uap) {
  218. isc_boolean_t freeze = *(isc_boolean_t *)uap;
  219. isc_boolean_t frozen;
  220. isc_result_t result = ISC_R_SUCCESS;
  221. char classstr[DNS_RDATACLASS_FORMATSIZE];
  222. char zonename[DNS_NAME_FORMATSIZE];
  223. dns_view_t *view;
  224. char *journal;
  225. const char *vname;
  226. const char *sep;
  227. int level;
  228. if (dns_zone_gettype(zone) != dns_zone_master)
  229. return (ISC_R_SUCCESS);
  230. frozen = dns_zone_getupdatedisabled(zone);
  231. if (freeze) {
  232. if (frozen)
  233. result = DNS_R_FROZEN;
  234. if (result == ISC_R_SUCCESS)
  235. result = dns_zone_flush(zone);
  236. if (result == ISC_R_SUCCESS) {
  237. journal = dns_zone_getjournal(zone);
  238. if (journal != NULL)
  239. (void)isc_file_remove(journal);
  240. }
  241. } else {
  242. if (frozen) {
  243. result = dns_zone_load(zone);
  244. if (result == DNS_R_CONTINUE ||
  245. result == DNS_R_UPTODATE)
  246. result = ISC_R_SUCCESS;
  247. }
  248. }
  249. if (result == ISC_R_SUCCESS)
  250. dns_zone_setupdatedisabled(zone, freeze);
  251. view = dns_zone_getview(zone);
  252. if (strcmp(view->name, "_bind") == 0 ||
  253. strcmp(view->name, "_default") == 0)
  254. {
  255. vname = "";
  256. sep = "";
  257. } else {
  258. vname = view->name;
  259. sep = " ";
  260. }
  261. dns_rdataclass_format(dns_zone_getclass(zone), classstr,
  262. sizeof(classstr));
  263. dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
  264. level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
  265. isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE,
  266. level, "%s zone '%s/%s'%s%s: %s",
  267. freeze ? "freezing" : "thawing",
  268. zonename, classstr, sep, vname,
  269. isc_result_totext(result));
  270. return (result);
  271. }
  272. isc_result_t
  273. dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
  274. isc_result_t (*action)(dns_zone_t *, void *), void *uap)
  275. {
  276. return (dns_zt_apply2(zt, stop, NULL, action, uap));
  277. }
  278. isc_result_t
  279. dns_zt_apply2(dns_zt_t *zt, isc_boolean_t stop, isc_result_t *sub,
  280. isc_result_t (*action)(dns_zone_t *, void *), void *uap)
  281. {
  282. dns_rbtnode_t *node;
  283. dns_rbtnodechain_t chain;
  284. isc_result_t result, tresult = ISC_R_SUCCESS;
  285. dns_zone_t *zone;
  286. REQUIRE(VALID_ZT(zt));
  287. REQUIRE(action != NULL);
  288. dns_rbtnodechain_init(&chain, zt->mctx);
  289. result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
  290. if (result == ISC_R_NOTFOUND) {
  291. /*
  292. * The tree is empty.
  293. */
  294. result = ISC_R_NOMORE;
  295. }
  296. while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
  297. result = dns_rbtnodechain_current(&chain, NULL, NULL,
  298. &node);
  299. if (result == ISC_R_SUCCESS) {
  300. zone = node->data;
  301. if (zone != NULL)
  302. result = (action)(zone, uap);
  303. if (result != ISC_R_SUCCESS && stop) {
  304. tresult = result;
  305. goto cleanup; /* don't break */
  306. } else if (result != ISC_R_SUCCESS &&
  307. tresult == ISC_R_SUCCESS)
  308. tresult = result;
  309. }
  310. result = dns_rbtnodechain_next(&chain, NULL, NULL);
  311. }
  312. if (result == ISC_R_NOMORE)
  313. result = ISC_R_SUCCESS;
  314. cleanup:
  315. dns_rbtnodechain_invalidate(&chain);
  316. if (sub != NULL)
  317. *sub = tresult;
  318. return (result);
  319. }
  320. /***
  321. *** Private
  322. ***/
  323. static void
  324. auto_detach(void *data, void *arg) {
  325. dns_zone_t *zone = data;
  326. UNUSED(arg);
  327. dns_zone_detach(&zone);
  328. }