instance.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "back-ldbm.h"
  13. /* Forward declarations */
  14. static void ldbm_instance_destructor(void **arg);
  15. Slapi_Entry *ldbm_instance_init_config_entry(char *cn_val, char *v1, char *v2, char *v3, char *v4);
  16. /* Creates and initializes a new ldbm_instance structure.
  17. * Also sets up some default indexes for the new instance.
  18. */
  19. int ldbm_instance_create(backend *be, char *name)
  20. {
  21. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  22. ldbm_instance *inst = NULL;
  23. int rc = 0;
  24. /* Allocate storage for the ldbm_instance structure. Information specific
  25. * to this instance of the ldbm backend will be held here. */
  26. inst = (ldbm_instance *) slapi_ch_calloc(1, sizeof(ldbm_instance));
  27. /* Record the name of this instance. */
  28. inst->inst_name = slapi_ch_strdup(name);
  29. /* initialize the entry cache */
  30. if (! cache_init(&(inst->inst_cache), DEFAULT_CACHE_SIZE,
  31. DEFAULT_CACHE_ENTRIES, CACHE_TYPE_ENTRY)) {
  32. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_create", "cache_init failed\n");
  33. rc = -1;
  34. goto error;
  35. }
  36. /*
  37. * initialize the dn cache
  38. * We do so, regardless of the subtree-rename value.
  39. * It is needed when converting the db from DN to RDN format.
  40. */
  41. if (! cache_init(&(inst->inst_dncache), DEFAULT_DNCACHE_SIZE,
  42. DEFAULT_DNCACHE_MAXCOUNT, CACHE_TYPE_DN)) {
  43. slapi_log_err(SLAPI_LOG_ERR,
  44. "ldbm_instance_create", "dn cache_init failed\n");
  45. rc = -1;
  46. goto error;
  47. }
  48. /* Lock for the list of open db handles */
  49. inst->inst_handle_list_mutex = PR_NewLock();
  50. if (NULL == inst->inst_handle_list_mutex) {
  51. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_create", "PR_NewLock failed\n");
  52. rc = -1;
  53. goto error;
  54. }
  55. /* Lock used to synchronize modify operations. */
  56. inst->inst_db_mutex = PR_NewMonitor();
  57. if (NULL == inst->inst_db_mutex) {
  58. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_create", "PR_NewMonitor failed\n");
  59. rc = -1;
  60. goto error;
  61. }
  62. if ((inst->inst_config_mutex = PR_NewLock()) == NULL) {
  63. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_create", "PR_NewLock failed\n");
  64. rc = -1;
  65. goto error;
  66. }
  67. if ((inst->inst_nextid_mutex = PR_NewLock()) == NULL) {
  68. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_create", "PR_NewLock failed\n");
  69. rc = -1;
  70. goto error;
  71. }
  72. if ((inst->inst_indexer_cv = PR_NewCondVar(inst->inst_nextid_mutex)) == NULL) {
  73. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_create", "PR_NewCondVar failed\n");
  74. rc = -1;
  75. goto error;
  76. }
  77. /* Keeps track of how many operations are currently using this instance */
  78. inst->inst_ref_count = slapi_counter_new();
  79. inst->inst_be = be;
  80. inst->inst_li = li;
  81. be->be_instance_info = inst;
  82. /* Initialize the fields with some default values. */
  83. ldbm_instance_config_setup_default(inst);
  84. /* Add this new instance to the the set of instances */
  85. {
  86. Object *instance_obj;
  87. instance_obj = object_new((void *) inst, &ldbm_instance_destructor);
  88. objset_add_obj(li->li_instance_set, instance_obj);
  89. object_release(instance_obj);
  90. }
  91. goto done;
  92. error:
  93. slapi_ch_free_string(&inst->inst_name);
  94. slapi_ch_free((void**)&inst);
  95. done:
  96. return rc;
  97. }
  98. /*
  99. * Take a bunch of strings, and create a index config entry
  100. */
  101. Slapi_Entry *
  102. ldbm_instance_init_config_entry(char *cn_val, char *val1, char *val2, char *val3, char *val4){
  103. Slapi_Entry *e = slapi_entry_alloc();
  104. struct berval *vals[2];
  105. struct berval val;
  106. vals[0] = &val;
  107. vals[1] = NULL;
  108. slapi_entry_set_dn(e,slapi_ch_strdup("cn=indexContainer"));
  109. val.bv_val = cn_val;
  110. val.bv_len = strlen(cn_val);
  111. slapi_entry_add_values(e,"cn",vals);
  112. val.bv_val = val1;
  113. val.bv_len = strlen(val1);
  114. slapi_entry_add_values(e,"nsIndexType",vals);
  115. if(val2){
  116. val.bv_val = val2;
  117. val.bv_len = strlen(val2);
  118. slapi_entry_add_values(e,"nsIndexType",vals);
  119. }
  120. if(val3){
  121. val.bv_val = val3;
  122. val.bv_len = strlen(val3);
  123. slapi_entry_add_values(e,"nsIndexType",vals);
  124. }
  125. if(val4){
  126. val.bv_val = val4;
  127. val.bv_len = strlen(val4);
  128. slapi_entry_add_values(e,"nsIndexType",vals);
  129. }
  130. return e;
  131. }
  132. /* create the default indexes separately
  133. * (because when we're creating a new backend while the server is running,
  134. * the DSE needs to be pre-seeded first.)
  135. */
  136. int ldbm_instance_create_default_indexes(backend *be)
  137. {
  138. Slapi_Entry *e;
  139. ldbm_instance *inst = (ldbm_instance *)be->be_instance_info;
  140. /* write the dse file only on the final index */
  141. int flags = LDBM_INSTANCE_CONFIG_DONT_WRITE;
  142. /*
  143. * Always index (entrydn or entryrdn), parentid, objectclass,
  144. * subordinatecount, copiedFrom, and aci,
  145. * since they are used by some searches, replication and the
  146. * ACL routines.
  147. */
  148. if (entryrdn_get_switch()) { /* subtree-rename: on */
  149. e = ldbm_instance_init_config_entry(LDBM_ENTRYRDN_STR,"subtree", 0, 0, 0);
  150. ldbm_instance_config_add_index_entry(inst, e, flags);
  151. slapi_entry_free(e);
  152. } else {
  153. e = ldbm_instance_init_config_entry(LDBM_ENTRYDN_STR,"eq", 0, 0, 0);
  154. ldbm_instance_config_add_index_entry(inst, e, flags);
  155. slapi_entry_free(e);
  156. }
  157. e = ldbm_instance_init_config_entry(LDBM_PARENTID_STR,"eq", 0, 0, 0);
  158. ldbm_instance_config_add_index_entry(inst, e, flags);
  159. slapi_entry_free(e);
  160. e = ldbm_instance_init_config_entry("objectclass","eq", 0, 0, 0);
  161. ldbm_instance_config_add_index_entry(inst, e, flags);
  162. slapi_entry_free(e);
  163. e = ldbm_instance_init_config_entry("aci","pres", 0, 0, 0);
  164. ldbm_instance_config_add_index_entry(inst, e, flags);
  165. slapi_entry_free(e);
  166. #if 0 /* don't need copiedfrom */
  167. e = ldbm_instance_init_config_entry("copiedfrom","pres",0 ,0);
  168. ldbm_instance_config_add_index_entry(inst, e, flags);
  169. slapi_entry_free(e);
  170. #endif
  171. e = ldbm_instance_init_config_entry(LDBM_NUMSUBORDINATES_STR,"pres", 0, 0, 0);
  172. ldbm_instance_config_add_index_entry(inst, e, flags);
  173. slapi_entry_free(e);
  174. e = ldbm_instance_init_config_entry(SLAPI_ATTR_UNIQUEID,"eq", 0, 0, 0);
  175. ldbm_instance_config_add_index_entry(inst, e, flags);
  176. slapi_entry_free(e);
  177. /* For MMR, we need this attribute (to replace use of dncomp in delete). */
  178. e = ldbm_instance_init_config_entry(ATTR_NSDS5_REPLCONFLICT,"eq", "pres", 0, 0);
  179. ldbm_instance_config_add_index_entry(inst, e, flags);
  180. slapi_entry_free(e);
  181. /* write the dse file only on the final index */
  182. e = ldbm_instance_init_config_entry(SLAPI_ATTR_NSCP_ENTRYDN,"eq", 0, 0, 0);
  183. ldbm_instance_config_add_index_entry(inst, e, flags);
  184. slapi_entry_free(e);
  185. /* ldbm_instance_config_add_index_entry(inst, 2, argv); */
  186. e = ldbm_instance_init_config_entry(LDBM_PSEUDO_ATTR_DEFAULT,"none", 0, 0, 0);
  187. attr_index_config( be, "ldbm index init", 0, e, 1, 0 );
  188. slapi_entry_free(e);
  189. if (!entryrdn_get_noancestorid()) {
  190. /*
  191. * ancestorid is special, there is actually no such attr type
  192. * but we still want to use the attr index file APIs.
  193. */
  194. e = ldbm_instance_init_config_entry(LDBM_ANCESTORID_STR,"eq", 0, 0, 0);
  195. attr_index_config( be, "ldbm index init", 0, e, 1, 0 );
  196. slapi_entry_free(e);
  197. }
  198. return 0;
  199. }
  200. /* Starts a backend instance */
  201. int
  202. ldbm_instance_start(backend *be)
  203. {
  204. int rc;
  205. PR_Lock (be->be_state_lock);
  206. if (be->be_state != BE_STATE_STOPPED &&
  207. be->be_state != BE_STATE_DELETED) {
  208. slapi_log_err(SLAPI_LOG_TRACE, "ldbm_instance_start",
  209. "Warning - backend is in a wrong state - %d\n",
  210. be->be_state);
  211. PR_Unlock (be->be_state_lock);
  212. return 0;
  213. }
  214. rc = dblayer_instance_start(be, DBLAYER_NORMAL_MODE);
  215. be->be_state = BE_STATE_STARTED;
  216. PR_Unlock (be->be_state_lock);
  217. return rc;
  218. }
  219. /* Stops a backend instance */
  220. void
  221. ldbm_instance_stop_cache(backend *be)
  222. {
  223. ldbm_instance *inst = (ldbm_instance *)be->be_instance_info;
  224. cache_destroy_please(&inst->inst_cache, CACHE_TYPE_ENTRY);
  225. if (entryrdn_get_switch()) { /* subtree-rename: on */
  226. cache_destroy_please(&inst->inst_dncache, CACHE_TYPE_DN);
  227. }
  228. }
  229. static void
  230. ldbm_instance_set_flags(ldbm_instance *inst)
  231. {
  232. if (dblayer_is_restored()) {
  233. slapi_be_set_flag(inst->inst_be, SLAPI_BE_FLAG_POST_RESTORE);
  234. }
  235. if (dblayer_import_file_check(inst)) {
  236. slapi_be_set_flag(inst->inst_be, SLAPI_BE_FLAG_POST_IMPORT);
  237. }
  238. }
  239. /* Walks down the set of instances, starting each one. */
  240. int
  241. ldbm_instance_startall(struct ldbminfo *li)
  242. {
  243. Object *inst_obj;
  244. ldbm_instance *inst;
  245. int rc = 0;
  246. inst_obj = objset_first_obj(li->li_instance_set);
  247. while (inst_obj != NULL) {
  248. int rc1;
  249. inst = (ldbm_instance *) object_get_data(inst_obj);
  250. ldbm_instance_set_flags(inst);
  251. rc1 = ldbm_instance_start(inst->inst_be);
  252. if (rc1 != 0) {
  253. rc = rc1;
  254. } else {
  255. if(ldbm_instance_config_load_dse_info(inst) != 0){
  256. slapi_log_err(SLAPI_LOG_ERR, "ldbm_instance_startall",
  257. "Loading database instance configuration failed for (%s)\n",
  258. inst->inst_name);
  259. rc = -1;
  260. } else {
  261. vlv_init(inst);
  262. slapi_mtn_be_started(inst->inst_be);
  263. }
  264. }
  265. inst_obj = objset_next_obj(li->li_instance_set, inst_obj);
  266. }
  267. return rc;
  268. }
  269. /* Walks down the set of instances, stopping each one. */
  270. int ldbm_instance_stopall_caches(struct ldbminfo *li)
  271. {
  272. Object *inst_obj;
  273. ldbm_instance *inst;
  274. inst_obj = objset_first_obj(li->li_instance_set);
  275. while (inst_obj != NULL) {
  276. inst = (ldbm_instance *) object_get_data(inst_obj);
  277. ldbm_instance_stop_cache(inst->inst_be);
  278. inst_obj = objset_next_obj(li->li_instance_set, inst_obj);
  279. }
  280. return 0;
  281. }
  282. /* Walks down the set of instance, looking for one
  283. * with the given name. Returns a pointer to the
  284. * instance if found, and NULL if not found. The
  285. * string compare on the instance name is NOT case
  286. * sensitive.
  287. */
  288. /* Currently this function doesn't bump
  289. * the ref count of the instance returned.
  290. */
  291. ldbm_instance *
  292. ldbm_instance_find_by_name(struct ldbminfo *li, char *name)
  293. {
  294. Object *inst_obj;
  295. ldbm_instance *inst;
  296. inst_obj = objset_first_obj(li->li_instance_set);
  297. while (inst_obj != NULL) {
  298. inst = (ldbm_instance *) object_get_data(inst_obj);
  299. if (!strcasecmp(inst->inst_name, name)) {
  300. /* Currently we release the object here. There is no
  301. * function for callers of this function to call to
  302. * release the object.
  303. */
  304. object_release(inst_obj);
  305. return inst;
  306. }
  307. inst_obj = objset_next_obj(li->li_instance_set, inst_obj);
  308. }
  309. return NULL;
  310. }
  311. /* Called when all references to the instance are gone. */
  312. /* (ie, only when an instance is being deleted) */
  313. static void
  314. ldbm_instance_destructor(void **arg)
  315. {
  316. ldbm_instance *inst = (ldbm_instance *) *arg;
  317. slapi_log_err(SLAPI_LOG_TRACE, "ldbm_instance_destructor",
  318. "Destructor for instance %s called\n",
  319. inst->inst_name);
  320. slapi_counter_destroy(&(inst->inst_ref_count));
  321. slapi_ch_free_string(&inst->inst_name);
  322. PR_DestroyLock(inst->inst_config_mutex);
  323. slapi_ch_free_string(&inst->inst_dir_name);
  324. slapi_ch_free_string(&inst->inst_parent_dir_name);
  325. /* These are removed in dblayer_terminate */
  326. /* PR_DestroyMonitor(inst->inst_db_mutex); */
  327. /* PR_DestroyLock(inst->inst_handle_list_mutex); */
  328. PR_DestroyLock(inst->inst_nextid_mutex);
  329. PR_DestroyCondVar(inst->inst_indexer_cv);
  330. attrinfo_deletetree(inst);
  331. slapi_ch_free((void **)&inst->inst_dataversion);
  332. /* cache has already been destroyed */
  333. slapi_ch_free((void **)&inst);
  334. }
  335. static int
  336. ldbm_instance_comparator(Object *object, const void *name)
  337. {
  338. void *data = object_get_data(object);
  339. return (data == name) ? 0 : 1;
  340. }
  341. /* find the instance in the objset and remove it */
  342. int
  343. ldbm_instance_destroy(ldbm_instance *inst)
  344. {
  345. Object *object = NULL;
  346. struct ldbminfo *li = inst->inst_li;
  347. object = objset_find(li->li_instance_set, ldbm_instance_comparator, inst);
  348. if (object == NULL) {
  349. return -1;
  350. }
  351. /* decref from objset_find */
  352. object_release(object);
  353. /* now remove from the instance set */
  354. objset_remove_obj(li->li_instance_set, object);
  355. return 0;
  356. }