dbhelp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * File for helper functions related to BerkeleyDB.
  42. * This exists because dblayer.c is 5k+ lines long,
  43. * so it seems time to move code to a new file.
  44. */
  45. #include "back-ldbm.h"
  46. #include "dblayer.h"
  47. static int
  48. dblayer_copy_file_keybykey(DB_ENV *env,
  49. char *source_file_name,
  50. char *destination_file_name,
  51. int overwrite,
  52. dblayer_private *priv,
  53. ldbm_instance *inst)
  54. {
  55. int retval = 0;
  56. int retval_cleanup = 0;
  57. DB *source_file = NULL;
  58. DB *destination_file = NULL;
  59. DBC *source_cursor = NULL;
  60. DBTYPE dbtype = 0;
  61. PRUint32 dbflags = 0;
  62. PRUint32 dbpagesize = 0;
  63. int cursor_flag = 0;
  64. int finished = 0;
  65. int mode = 0;
  66. char *p = NULL;
  67. LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_copy_file_keybykey\n", 0, 0, 0 );
  68. if (!env) {
  69. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Out of memory\n", 0, 0, 0);
  70. goto error;
  71. }
  72. if (priv->dblayer_file_mode)
  73. mode = priv->dblayer_file_mode;
  74. dblayer_set_env_debugging(env, priv);
  75. /* Open the source file */
  76. retval = db_create(&source_file, env, 0);
  77. if (retval) {
  78. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Create error %d: %s\n", retval, db_strerror(retval), 0);
  79. goto error;
  80. }
  81. retval = (source_file->open)(source_file, NULL, source_file_name, NULL, DB_UNKNOWN, DB_RDONLY, 0);
  82. if (retval) {
  83. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Open error %d: %s\n", retval, db_strerror(retval), 0);
  84. goto error;
  85. }
  86. /* Get the info we need from the source file */
  87. retval = source_file->get_flags(source_file, &dbflags);
  88. if (retval) {
  89. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, get_flags error %d: %s\n", retval, db_strerror(retval), 0);
  90. goto error;
  91. }
  92. retval = source_file->get_type(source_file, &dbtype);
  93. if (retval) {
  94. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, get_type error %d: %s\n", retval, db_strerror(retval), 0);
  95. goto error;
  96. }
  97. retval = source_file->get_pagesize(source_file, &dbpagesize);
  98. if (retval) {
  99. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, get_pagesize error %d: %s\n", retval, db_strerror(retval), 0);
  100. goto error;
  101. }
  102. /* Open the destination file
  103. * and make sure that it has the correct page size, the correct access method, and the correct flags (dup etc)
  104. */
  105. retval = db_create(&destination_file, env, 0);
  106. if (retval) {
  107. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Create error %d: %s\n", retval, db_strerror(retval), 0);
  108. goto error;
  109. }
  110. retval = destination_file->set_flags(destination_file,dbflags);
  111. if (retval) {
  112. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, set_flags error %d: %s\n", retval, db_strerror(retval), 0);
  113. goto error;
  114. }
  115. retval = destination_file->set_pagesize(destination_file,dbpagesize);
  116. if (retval) {
  117. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, set_pagesize error %d: %s\n", retval, db_strerror(retval), 0);
  118. goto error;
  119. }
  120. /* TEL 20130412: Make sure to set the dup comparison function if needed.
  121. * We key our decision off of the presence of new IDL and dup flags on
  122. * the source database. This is similar dblayer_open_file, except that
  123. * we don't have the attribute info index mask for VLV. That should be OK
  124. * since the DB_DUP and DB_DUPSORT flags wouldn't have been toggled on
  125. * unless they passed the check on the source.
  126. */
  127. /* Entryrdn index has its own dup compare function */
  128. if ((p = PL_strcasestr(source_file_name, LDBM_ENTRYRDN_STR)) &&
  129. (*(p + sizeof(LDBM_ENTRYRDN_STR) - 1) == '.')) {
  130. /* entryrdn.db */
  131. struct attrinfo *ai = NULL;
  132. if (NULL == inst) {
  133. LDAPDebug0Args(LDAP_DEBUG_ANY,
  134. "dblayer_copy_file_keybykey(entryrdn), "
  135. "dup_cmp_fn cannot be retrieved since inst is NULL.\n");
  136. goto error;
  137. }
  138. ainfo_get(inst->inst_be, LDBM_ENTRYRDN_STR, &ai);
  139. if (ai->ai_dup_cmp_fn) {
  140. /* If set, use the special dup compare callback */
  141. retval = destination_file->set_dup_compare(destination_file, ai->ai_dup_cmp_fn);
  142. if (retval) {
  143. LDAPDebug2Args(LDAP_DEBUG_ANY,
  144. "dblayer_copy_file_keybykey(entryrdn), set_dup_compare error %d: %s\n",
  145. retval, db_strerror(retval));
  146. goto error;
  147. }
  148. }
  149. } else if (idl_get_idl_new() && (dbflags & DB_DUP) && (dbflags & DB_DUPSORT)) {
  150. retval = destination_file->set_dup_compare(destination_file, idl_new_compare_dups);
  151. if (retval) {
  152. LDAPDebug2Args(LDAP_DEBUG_ANY,
  153. "dblayer_copy_file_keybykey, set_dup_compare error %d: %s\n",
  154. retval, db_strerror(retval));
  155. goto error;
  156. }
  157. }
  158. retval = (destination_file->open)(destination_file, NULL, destination_file_name, NULL, dbtype, DB_CREATE | DB_EXCL, mode);
  159. if (retval) {
  160. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Open error %d: %s\n", retval, db_strerror(retval), 0);
  161. goto error;
  162. }
  163. /* Open a cursor on the source file */
  164. retval = source_file->cursor(source_file,NULL,&source_cursor,0);
  165. if (retval) {
  166. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Create cursor error %d: %s\n", retval, db_strerror(retval), 0);
  167. goto error;
  168. }
  169. /* Seek to the first key */
  170. cursor_flag = DB_FIRST;
  171. /* Loop seeking to the next key until they're all done */
  172. while (!finished) {
  173. DBT key = {0};
  174. DBT data = {0};
  175. retval = source_cursor->c_get(source_cursor, &key, &data, cursor_flag);
  176. if (retval) {
  177. /* DB_NOTFOUND is expected when we find the end, log a message for any other error.
  178. * In either case, set finished=1 so we can hop down and close the cursor. */
  179. if ( DB_NOTFOUND != retval )
  180. {
  181. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, c_get error %d: %s\n", retval, db_strerror(retval), 0);
  182. goto error;
  183. }
  184. retval = 0; /* DB_NOTFOUND was OK... */
  185. finished = 1;
  186. } else {
  187. /* For each key, insert into the destination file */
  188. retval = destination_file->put(destination_file, NULL, &key, &data, 0);
  189. if (retval) {
  190. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, put error %d: %s\n", retval, db_strerror(retval), 0);
  191. goto error;
  192. }
  193. cursor_flag = DB_NEXT;
  194. }
  195. }
  196. error:
  197. /* Close the cursor */
  198. if (source_cursor) {
  199. retval_cleanup = source_cursor->c_close(source_cursor);
  200. if (retval_cleanup) {
  201. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Close cursor error %d: %s\n", retval_cleanup, db_strerror(retval_cleanup), 0);
  202. retval += retval_cleanup;
  203. }
  204. }
  205. /* Close the source file */
  206. if (source_file) {
  207. retval_cleanup = source_file->close(source_file,0);
  208. source_file = NULL;
  209. if (retval_cleanup) {
  210. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Close error %d: %s\n", retval_cleanup, db_strerror(retval_cleanup), 0);
  211. retval += retval_cleanup;
  212. }
  213. }
  214. /* Close the destination file */
  215. if (destination_file) {
  216. retval_cleanup = destination_file->close(destination_file,0);
  217. destination_file = NULL;
  218. if (retval_cleanup) {
  219. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Close error %d: %s\n", retval_cleanup, db_strerror(retval_cleanup), 0);
  220. retval += retval_cleanup;
  221. }
  222. }
  223. LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_copy_file_keybykey\n", 0, 0, 0 );
  224. return retval;
  225. }
  226. int
  227. dblayer_copy_file_resetlsns(char *home_dir,
  228. char *source_file_name,
  229. char *destination_file_name,
  230. int overwrite,
  231. dblayer_private *priv,
  232. ldbm_instance *inst)
  233. {
  234. int retval = 0;
  235. DB_ENV *env = NULL;
  236. LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_copy_file_resetlsns\n", 0, 0, 0 );
  237. /* Make the environment */
  238. retval = dblayer_make_private_simple_env(home_dir,&env);
  239. if (retval || !env) {
  240. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns: Call to dblayer_make_private_simple_env failed!\n"
  241. "Unable to open an environment.", 0, 0, 0);
  242. goto out;
  243. }
  244. /* Do the copy */
  245. retval = dblayer_copy_file_keybykey(env, source_file_name, destination_file_name, overwrite, priv, inst);
  246. if (retval) {
  247. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns: Copy not completed successfully.", 0, 0, 0);
  248. }
  249. out:
  250. /* Close the environment */
  251. if (env) {
  252. int retval2 = 0;
  253. retval2 = env->close(env,0);
  254. if (retval2) {
  255. if (0 == retval) {
  256. retval = retval2;
  257. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns, error %d: %s\n", retval, db_strerror(retval), 0);
  258. }
  259. }
  260. }
  261. LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_copy_file_resetlsns\n", 0, 0, 0 );
  262. return retval;
  263. }
  264. void dblayer_set_env_debugging(DB_ENV *pEnv, dblayer_private *priv)
  265. {
  266. pEnv->set_errpfx(pEnv, "ns-slapd");
  267. if (priv->dblayer_verbose) {
  268. #if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4300
  269. /* DB_VERB_CHKPOINT removed in 43 */
  270. #else
  271. pEnv->set_verbose(pEnv, DB_VERB_CHKPOINT, 1); /* 1 means on */
  272. #endif
  273. pEnv->set_verbose(pEnv, DB_VERB_DEADLOCK, 1); /* 1 means on */
  274. pEnv->set_verbose(pEnv, DB_VERB_RECOVERY, 1); /* 1 means on */
  275. pEnv->set_verbose(pEnv, DB_VERB_WAITSFOR, 1); /* 1 means on */
  276. }
  277. if (priv->dblayer_debug) {
  278. pEnv->set_errcall(pEnv, dblayer_log_print);
  279. }
  280. }
  281. /* Make an environment to be used for isolated recovery (e.g. during a partial restore operation) */
  282. int dblayer_make_private_recovery_env(char *db_home_dir, dblayer_private *priv, DB_ENV **env)
  283. {
  284. int retval = 0;
  285. DB_ENV *ret_env = NULL;
  286. LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_make_private_recovery_env\n", 0, 0, 0 );
  287. if (NULL == env) {
  288. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_recovery_env: Null environment. Cannot continue.", 0, 0, 0);
  289. return -1;
  290. }
  291. *env = NULL;
  292. retval = db_env_create(&ret_env,0);
  293. if (retval) {
  294. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_recovery_env, Create error %d: %s\n", retval, db_strerror(retval), 0);
  295. goto error;
  296. }
  297. dblayer_set_env_debugging(ret_env, priv);
  298. retval = (ret_env->open)(ret_env,db_home_dir, DB_INIT_TXN | DB_RECOVER_FATAL | DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE,0);
  299. if (0 == retval) {
  300. *env = ret_env;
  301. } else {
  302. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_recovery_env, Open error %d: %s\n", retval, db_strerror(retval), 0);
  303. goto error;
  304. }
  305. error:
  306. LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_make_private_recovery_env\n", 0, 0, 0 );
  307. return retval;
  308. }
  309. /* Make an environment to be used for simple non-transacted database operations, e.g. fixup during upgrade */
  310. int dblayer_make_private_simple_env(char *db_home_dir, DB_ENV **env)
  311. {
  312. int retval = 0;
  313. DB_ENV *ret_env = NULL;
  314. LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_make_private_simple_env\n", 0, 0, 0 );
  315. if (NULL == env) {
  316. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_simple_env: Null environment. Cannot continue.", 0, 0, 0);
  317. return -1;
  318. }
  319. *env = NULL;
  320. retval = db_env_create(&ret_env,0);
  321. if (retval) {
  322. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_simple_env, error %d: %s\n", retval, db_strerror(retval), 0);
  323. goto error;
  324. }
  325. retval = (ret_env->open)(ret_env,db_home_dir,DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE,0);
  326. if (0 == retval) {
  327. *env = ret_env;
  328. } else {
  329. LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_simple_env, error %d: %s\n", retval, db_strerror(retval), 0);
  330. goto error;
  331. }
  332. error:
  333. LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_make_private_simple_env\n", 0, 0, 0 );
  334. return retval;
  335. }