dbverify.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * Copyright (C) 2007 Red Hat, Inc.
  34. * All rights reserved.
  35. * END COPYRIGHT BLOCK **/
  36. #ifdef HAVE_CONFIG_H
  37. # include <config.h>
  38. #endif
  39. /* dbverify.c - verify database files */
  40. #include "back-ldbm.h"
  41. #include "dblayer.h"
  42. static int
  43. dbverify_ext( ldbm_instance *inst, int verbose )
  44. {
  45. char dbdir[MAXPATHLEN];
  46. char *filep = NULL;
  47. PRDir *dirhandle = NULL;
  48. PRDirEntry *direntry = NULL;
  49. DB *dbp = NULL;
  50. int tmplen = 0;
  51. int filelen = 0;
  52. int rval = 1;
  53. int rval_main = 0;
  54. struct ldbminfo *li = inst->inst_li;
  55. dblayer_private *priv = (dblayer_private*)li->li_dblayer_private;
  56. struct dblayer_private_env *pEnv = priv->dblayer_env;
  57. dbdir[sizeof(dbdir)-1] = '\0';
  58. PR_snprintf(dbdir, sizeof(dbdir), "%s/%s", inst->inst_parent_dir_name,
  59. inst->inst_dir_name);
  60. if ('\0' != dbdir[sizeof(dbdir)-1]) /* overflown */
  61. {
  62. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  63. "db path too long: %s/%s\n",
  64. inst->inst_parent_dir_name, inst->inst_dir_name);
  65. return 1;
  66. }
  67. tmplen = strlen(dbdir);
  68. filep = dbdir + tmplen;
  69. filelen = sizeof(dbdir) - tmplen;
  70. /* run dbverify on each each db file */
  71. dirhandle = PR_OpenDir(dbdir);
  72. if (! dirhandle)
  73. {
  74. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  75. "PR_OpenDir (%s) failed (%d): %s\n",
  76. dbdir, PR_GetError(),slapd_pr_strerror(PR_GetError()));
  77. return 1;
  78. }
  79. while (NULL !=
  80. (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
  81. {
  82. /* struct attrinfo *ai = NULL; */
  83. dbp = NULL;
  84. if (!direntry->name)
  85. {
  86. break;
  87. }
  88. if (!strstr(direntry->name, LDBM_FILENAME_SUFFIX)) /* non db file */
  89. {
  90. continue;
  91. }
  92. if (sizeof(direntry->name) + 2 > filelen)
  93. {
  94. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  95. "db path too long: %s/%s\n",
  96. dbdir, direntry->name);
  97. continue;
  98. }
  99. PR_snprintf(filep, filelen, "/%s", direntry->name);
  100. rval = db_create(&dbp, pEnv->dblayer_DB_ENV, 0);
  101. if (0 != rval)
  102. {
  103. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  104. "Unable to create id2entry db file %d\n", rval);
  105. return rval;
  106. }
  107. #define VLVPREFIX "vlv#"
  108. if (0 != strncmp(direntry->name, ID2ENTRY, strlen(ID2ENTRY)))
  109. {
  110. struct attrinfo *ai = NULL;
  111. char *p = NULL;
  112. p = strstr(filep, LDBM_FILENAME_SUFFIX); /* since already checked,
  113. it must have it */
  114. *p = '\0';
  115. ainfo_get( inst->inst_be, filep+1, &ai );
  116. *p = '.';
  117. if (ai->ai_key_cmp_fn) {
  118. dbp->app_private = (void *)ai->ai_key_cmp_fn;
  119. dbp->set_bt_compare(dbp, dblayer_bt_compare);
  120. }
  121. if (idl_get_idl_new())
  122. {
  123. rval = dbp->set_pagesize(dbp,
  124. (priv->dblayer_index_page_size == 0) ?
  125. DBLAYER_INDEX_PAGESIZE : priv->dblayer_index_page_size);
  126. }
  127. else
  128. {
  129. rval = dbp->set_pagesize(dbp,
  130. (priv->dblayer_page_size == 0) ?
  131. DBLAYER_PAGESIZE : priv->dblayer_page_size);
  132. }
  133. if (0 != rval)
  134. {
  135. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  136. "Unable to set pagesize flags to db (%d)\n", rval);
  137. return rval;
  138. }
  139. if (0 == strncmp(direntry->name, VLVPREFIX, strlen(VLVPREFIX)))
  140. {
  141. rval = dbp->set_flags(dbp, DB_RECNUM);
  142. if (0 != rval)
  143. {
  144. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  145. "Unable to set RECNUM flag to vlv index (%d)\n", rval);
  146. return rval;
  147. }
  148. }
  149. else if (idl_get_idl_new())
  150. {
  151. rval = dbp->set_flags(dbp, DB_DUP | DB_DUPSORT);
  152. if (0 != rval)
  153. {
  154. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  155. "Unable to set DUP flags to db (%d)\n", rval);
  156. return rval;
  157. }
  158. rval = dbp->set_dup_compare(dbp, idl_new_compare_dups);
  159. if (0 != rval)
  160. {
  161. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  162. "Unable to set dup_compare to db (%d)\n", rval);
  163. return rval;
  164. }
  165. }
  166. }
  167. #undef VLVPREFIX
  168. rval = dbp->verify(dbp, dbdir, NULL, NULL, 0);
  169. if (0 == rval)
  170. {
  171. if (verbose)
  172. {
  173. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  174. "%s: ok\n", dbdir);
  175. }
  176. }
  177. else
  178. {
  179. slapi_log_error(SLAPI_LOG_FATAL, "DB verify",
  180. "verify failed(%d): %s\n", rval, dbdir);
  181. }
  182. rval_main |= rval;
  183. *filep = '\0';
  184. }
  185. PR_CloseDir(dirhandle);
  186. return rval_main;
  187. }
  188. int
  189. ldbm_back_dbverify( Slapi_PBlock *pb )
  190. {
  191. struct ldbminfo *li = NULL;
  192. Object *inst_obj = NULL;
  193. ldbm_instance *inst = NULL;
  194. int verbose = 0;
  195. int rval = 1;
  196. int rval_main = 0;
  197. char **instance_names = NULL;
  198. slapi_log_error(SLAPI_LOG_TRACE, "verify DB", "Verifying db files...\n");
  199. slapi_pblock_get(pb, SLAPI_BACKEND_INSTANCE_NAME, &instance_names);
  200. slapi_pblock_get(pb, SLAPI_SEQ_TYPE, &verbose);
  201. slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &li);
  202. ldbm_config_load_dse_info(li);
  203. ldbm_config_internal_set(li, CONFIG_DB_TRANSACTION_LOGGING, "off");
  204. /* no write needed; choose EXPORT MODE */
  205. if (0 != dblayer_start(li, DBLAYER_EXPORT_MODE))
  206. {
  207. slapi_log_error(SLAPI_LOG_FATAL, "verify DB",
  208. "dbverify: Failed to init database\n");
  209. return rval;
  210. }
  211. /* server is up */
  212. slapi_log_error(SLAPI_LOG_TRACE, "verify DB", "server is up\n");
  213. if (instance_names) /* instance is specified */
  214. {
  215. char **inp = NULL;
  216. for (inp = instance_names; inp && *inp; inp++)
  217. {
  218. inst = ldbm_instance_find_by_name(li, *inp);
  219. if (inst)
  220. {
  221. rval_main |= dbverify_ext(inst, verbose);
  222. }
  223. else
  224. {
  225. rval_main |= 1; /* no such instance */
  226. }
  227. }
  228. }
  229. else /* all instances */
  230. {
  231. for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
  232. inst_obj = objset_next_obj(li->li_instance_set, inst_obj))
  233. {
  234. inst = (ldbm_instance *)object_get_data(inst_obj);
  235. /* check if an import/restore is already ongoing... */
  236. if (instance_set_busy(inst) != 0)
  237. {
  238. /* standalone, only. never happens */
  239. slapi_log_error(SLAPI_LOG_FATAL, "upgrade DB",
  240. "ldbm: '%s' is already in the middle of "
  241. "another task and cannot be disturbed.\n",
  242. inst->inst_name);
  243. continue; /* skip this instance and go to the next*/
  244. }
  245. rval_main |= dbverify_ext(inst, verbose);
  246. }
  247. }
  248. /* close the database down again */
  249. rval = dblayer_post_close(li, DBLAYER_EXPORT_MODE);
  250. if (0 != rval)
  251. {
  252. slapi_log_error(SLAPI_LOG_FATAL,
  253. "verify DB", "Failed to close database\n");
  254. }
  255. return rval_main;
  256. }