instance.c 14 KB

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