start.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /*
  42. * start.c
  43. */
  44. #include "back-ldbm.h"
  45. static int initialized = 0;
  46. int
  47. ldbm_back_isinitialized()
  48. {
  49. return initialized;
  50. }
  51. /*
  52. * Start the LDBM plugin, and all its instances.
  53. */
  54. int
  55. ldbm_back_start( Slapi_PBlock *pb )
  56. {
  57. struct ldbminfo *li;
  58. char *home_dir;
  59. int action;
  60. int retval;
  61. LDAPDebug( LDAP_DEBUG_TRACE, "ldbm backend starting\n", 0, 0, 0 );
  62. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  63. /* parse the config file here */
  64. if (0 != ldbm_config_load_dse_info(li)) {
  65. LDAPDebug( LDAP_DEBUG_ANY, "start: Loading database configuration failed\n",
  66. 0, 0, 0 );
  67. return SLAPI_FAIL_GENERAL;
  68. }
  69. /* register with the binder-based resource limit subsystem so that */
  70. /* lookthroughlimit can be supported on a per-connection basis. */
  71. if ( slapi_reslimit_register( SLAPI_RESLIMIT_TYPE_INT,
  72. LDBM_LOOKTHROUGHLIMIT_AT, &li->li_reslimit_lookthrough_handle )
  73. != SLAPI_RESLIMIT_STATUS_SUCCESS ) {
  74. LDAPDebug( LDAP_DEBUG_ANY, "start: Resource limit registration failed\n",
  75. 0, 0, 0 );
  76. return SLAPI_FAIL_GENERAL;
  77. }
  78. /* If the db directory hasn't been set yet, we need to set it to
  79. * the default. */
  80. if (NULL == li->li_directory || '\0' == li->li_directory[0]) {
  81. /* "get default" is a special string that tells the config
  82. * routines to figure out the default db directory by
  83. * reading cn=config. */
  84. ldbm_config_internal_set(li, CONFIG_DIRECTORY, "get default");
  85. }
  86. /* sanity check the autosizing values,
  87. no value or sum of values larger than 100.
  88. */
  89. if ((li->li_cache_autosize > 100) ||
  90. (li->li_cache_autosize_split > 100) ||
  91. (li->li_import_cache_autosize > 100) ||
  92. ((li->li_cache_autosize > 0) && (li->li_import_cache_autosize > 0) &&
  93. (li->li_cache_autosize + li->li_import_cache_autosize > 100))) {
  94. LDAPDebug( LDAP_DEBUG_ANY, "cache autosizing: bad settings, "
  95. "value or sum of values can not larger than 100.\n", 0, 0, 0 );
  96. } else
  97. /* if cache autosize was selected, select the cache sizes now */
  98. if ((li->li_cache_autosize > 0) || (li->li_import_cache_autosize > 0)) {
  99. size_t pagesize, pages, procpages, availpages;
  100. dblayer_sys_pages(&pagesize, &pages, &procpages, &availpages);
  101. if (pagesize) {
  102. char s[32]; /* big enough to hold %ld */
  103. unsigned long cache_size_to_configure = 0;
  104. int zone_pages, db_pages, entry_pages, import_pages;
  105. Object *inst_obj;
  106. ldbm_instance *inst;
  107. /* autosizing dbCache and entryCache */
  108. if (li->li_cache_autosize > 0) {
  109. zone_pages = (li->li_cache_autosize * pages) / 100;
  110. /* now split it according to user prefs */
  111. db_pages = (li->li_cache_autosize_split * zone_pages) / 100;
  112. /* fudge an extra instance into our calculations... */
  113. entry_pages = (zone_pages - db_pages) /
  114. (objset_size(li->li_instance_set) + 1);
  115. LDAPDebug(LDAP_DEBUG_ANY, "cache autosizing. found %dk physical memory\n",
  116. pages*(pagesize/1024), 0, 0);
  117. LDAPDebug(LDAP_DEBUG_ANY, "cache autosizing: db cache: %dk, "
  118. "each entry cache (%d total): %dk\n",
  119. db_pages*(pagesize/1024), objset_size(li->li_instance_set),
  120. entry_pages*(pagesize/1024));
  121. /* libdb allocates 1.25x the amount we tell it to,
  122. * but only for values < 500Meg
  123. * For the larger memory, the overhead is relatively small. */
  124. cache_size_to_configure = (unsigned long)(db_pages * pagesize);
  125. if (cache_size_to_configure < (500 * MEGABYTE)) {
  126. cache_size_to_configure = (unsigned long)((db_pages * pagesize) / 1.25);
  127. }
  128. sprintf(s, "%lu", cache_size_to_configure);
  129. ldbm_config_internal_set(li, CONFIG_DBCACHESIZE, s);
  130. li->li_cache_autosize_ec = (unsigned long)entry_pages * pagesize;
  131. for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
  132. inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
  133. inst = (ldbm_instance *)object_get_data(inst_obj);
  134. cache_set_max_entries(&(inst->inst_cache), -1);
  135. cache_set_max_size(&(inst->inst_cache),
  136. li->li_cache_autosize_ec, CACHE_TYPE_ENTRY);
  137. }
  138. }
  139. /* autosizing importCache */
  140. if (li->li_import_cache_autosize > 0) {
  141. /* For some reason, -1 means 50 ... */
  142. if (li->li_import_cache_autosize == -1) {
  143. li->li_import_cache_autosize = 50;
  144. }
  145. import_pages = (li->li_import_cache_autosize * pages) / 100;
  146. LDAPDebug(LDAP_DEBUG_ANY, "cache autosizing: import cache: %dk \n",
  147. import_pages*(pagesize/1024), NULL, NULL);
  148. sprintf(s, "%lu", (unsigned long)(import_pages * pagesize));
  149. ldbm_config_internal_set(li, CONFIG_IMPORT_CACHESIZE, s);
  150. }
  151. }
  152. }
  153. retval = check_db_version(li, &action);
  154. if (0 != retval)
  155. {
  156. LDAPDebug( LDAP_DEBUG_ANY, "start: db version is not supported\n",
  157. 0, 0, 0);
  158. return SLAPI_FAIL_GENERAL;
  159. }
  160. if (action & (DBVERSION_UPGRADE_3_4|DBVERSION_UPGRADE_4_4))
  161. {
  162. retval = dblayer_start(li,DBLAYER_CLEAN_RECOVER_MODE);
  163. }
  164. else
  165. {
  166. retval = dblayer_start(li,DBLAYER_NORMAL_MODE);
  167. }
  168. if (0 != retval) {
  169. char *msg;
  170. LDAPDebug( LDAP_DEBUG_ANY, "start: Failed to init database, err=%d %s\n",
  171. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  172. if (LDBM_OS_ERR_IS_DISKFULL(retval)) return return_on_disk_full(li);
  173. else return SLAPI_FAIL_GENERAL;
  174. }
  175. /* Walk down the instance list, starting all the instances. */
  176. retval = ldbm_instance_startall(li);
  177. if (0 != retval) {
  178. char *msg;
  179. LDAPDebug( LDAP_DEBUG_ANY, "start: Failed to start databases, err=%d %s\n",
  180. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  181. if (LDBM_OS_ERR_IS_DISKFULL(retval)) return return_on_disk_full(li);
  182. else {
  183. if ((li->li_cache_autosize > 0) && (li->li_cache_autosize <= 100)) {
  184. LDAPDebug( LDAP_DEBUG_ANY, "Failed to allocate %d byte dbcache. "
  185. "Please reduce the value of %s and restart the server.\n",
  186. li->li_dbcachesize, CONFIG_CACHE_AUTOSIZE, 0);
  187. }
  188. return SLAPI_FAIL_GENERAL;
  189. }
  190. }
  191. /* write DBVERSION file if one does not exist */
  192. home_dir = dblayer_get_home_dir(li, NULL);
  193. if (!dbversion_exists(li, home_dir))
  194. {
  195. dbversion_write (li, home_dir, NULL, DBVERSION_ALL);
  196. }
  197. /* this function is called every time new db is initialized */
  198. /* currently it is called the 2nd time when changelog db is */
  199. /* dynamically created. Code below should only be called once */
  200. if (!initialized)
  201. {
  202. ldbm_compute_init();
  203. initialized = 1;
  204. }
  205. /* initialize the USN counter */
  206. ldbm_usn_init(li);
  207. LDAPDebug( LDAP_DEBUG_TRACE, "ldbm backend done starting\n", 0, 0, 0 );
  208. return( 0 );
  209. }