usn_cleanup.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. #include "usn.h"
  12. struct usn_cleanup_data {
  13. char *suffix;
  14. char *maxusn_to_delete;
  15. char *bind_dn;
  16. };
  17. static int usn_cleanup_add(Slapi_PBlock *pb, Slapi_Entry *e,
  18. Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  19. static void usn_cleanup_task_destructor(Slapi_Task *task);
  20. int
  21. usn_cleanup_start(Slapi_PBlock *pb)
  22. {
  23. int rc = slapi_plugin_task_register_handler("USN tombstone cleanup task",
  24. usn_cleanup_add, pb);
  25. return rc;
  26. }
  27. int
  28. usn_cleanup_close()
  29. {
  30. int rc = slapi_plugin_task_unregister_handler("USN tombstone cleanup task",
  31. usn_cleanup_add);
  32. return rc;
  33. }
  34. /*
  35. * Task thread
  36. */
  37. static void
  38. usn_cleanup_thread(void *arg)
  39. {
  40. Slapi_Task *task = (Slapi_Task *)arg;
  41. int rv = 0;
  42. int total_work = 2;
  43. /* fetch our argument from the task */
  44. struct usn_cleanup_data *cleanup_data =
  45. (struct usn_cleanup_data*)slapi_task_get_data(task);
  46. Slapi_PBlock *search_pb = NULL;
  47. Slapi_Entry **entries = NULL, **ep = NULL;
  48. Slapi_PBlock *delete_pb = NULL;
  49. char *filter = "objectclass=nsTombstone";
  50. if (!task) {
  51. return; /* no task */
  52. }
  53. slapi_log_error(SLAPI_LOG_TRACE, USN_PLUGIN_SUBSYSTEM,
  54. "--> usn_cleanup_thread\n");
  55. slapi_task_inc_refcount(task);
  56. slapi_log_error(SLAPI_LOG_PLUGIN, USN_PLUGIN_SUBSYSTEM,
  57. "usn_cleanup_thread --> refcount incremented.\n" );
  58. if (NULL == usn_get_identity()) { /* plugin is not initialized */
  59. slapi_task_log_notice(task, "USN plugin is not initialized\n");
  60. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  61. "USN tombstone cleanup: USN plugin is not initialized\n");
  62. rv = -1;
  63. filter = NULL; /* so we don't try to free it */
  64. goto bail;
  65. }
  66. /* Initialize and set the thread data */
  67. slapi_td_set_dn(slapi_ch_strdup(cleanup_data->bind_dn));
  68. /* update task state to show it's running */
  69. slapi_task_begin(task, total_work);
  70. if (cleanup_data->maxusn_to_delete) {
  71. /* (&(objectclass=nsTombstone)(entryusn<=maxusn_to_delete)) */
  72. int filter_len =
  73. strlen(filter) + strlen(cleanup_data->maxusn_to_delete) + 32;
  74. filter = (char *)slapi_ch_malloc(filter_len);
  75. PR_snprintf(filter, filter_len,
  76. "(&(objectclass=nsTombstone)(entryusn<=%s))",
  77. cleanup_data->maxusn_to_delete);
  78. }
  79. search_pb = slapi_pblock_new();
  80. slapi_search_internal_set_pb(search_pb, cleanup_data->suffix,
  81. LDAP_SCOPE_SUBTREE, filter,
  82. NULL, 0, NULL, NULL, usn_get_identity(), 0);
  83. slapi_search_internal_pb(search_pb);
  84. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rv);
  85. if (LDAP_NO_SUCH_OBJECT == rv) {
  86. slapi_task_log_notice(task,
  87. "USN tombstone cleanup: no such suffix %s.\n",
  88. cleanup_data->suffix);
  89. slapi_task_log_status(task,
  90. "USN tombstone cleanup: no such suffix %s.\n",
  91. cleanup_data->suffix);
  92. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  93. "USN tombstone cleanup: no such suffix %s.\n",
  94. cleanup_data->suffix);
  95. goto bail;
  96. } else if (LDAP_SUCCESS != rv) {
  97. slapi_task_log_notice(task,
  98. "USN tombstone cleanup: searching tombstone entries "
  99. "in %s failed; (%d).\n", cleanup_data->suffix, rv);
  100. slapi_task_log_status(task,
  101. "USN tombstone cleanup: searching tombstone entries in "
  102. "%s failed; (%d).\n", cleanup_data->suffix, rv);
  103. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  104. "USN tombstone cleanup: searching tombstone entries in "
  105. "%s failed; (%d).\n", cleanup_data->suffix, rv);
  106. goto bail;
  107. }
  108. slapi_task_log_notice(task,
  109. "USN tombstone cleanup task starts (suffix: %s) ...\n",
  110. cleanup_data->suffix);
  111. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  112. "USN tombstone cleanup task starts (suffix: %s) ...\n",
  113. cleanup_data->suffix);
  114. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  115. delete_pb = slapi_pblock_new();
  116. for (ep = entries; ep && *ep; ep++) {
  117. int delrv = 0;
  118. const Slapi_DN *sdn = slapi_entry_get_sdn_const(*ep);
  119. int opflags = OP_FLAG_TOMBSTONE_ENTRY;
  120. /* check for shutdown */
  121. if(slapi_is_shutting_down()){
  122. slapi_task_log_notice(task, "USN tombstone cleanup task aborted due to shutdown.");
  123. slapi_task_log_status(task, "USN tombstone cleanup task aborted due to shutdown.");
  124. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  125. "USN tombstone cleanup task aborted due to shutdown.\n");
  126. goto bail;
  127. }
  128. slapi_delete_internal_set_pb(delete_pb, slapi_sdn_get_dn(sdn),
  129. NULL, NULL, usn_get_identity(), opflags);
  130. slapi_delete_internal_pb(delete_pb);
  131. slapi_pblock_get(delete_pb, SLAPI_PLUGIN_INTOP_RESULT, &delrv);
  132. if (LDAP_SUCCESS != delrv) {
  133. slapi_task_log_notice(task,
  134. "USN tombstone cleanup: deleting %s failed; (%d).\n",
  135. slapi_sdn_get_dn(sdn), delrv);
  136. slapi_task_log_status(task,
  137. "USN tombstone cleanup: deleting %s failed; (%d).\n",
  138. slapi_sdn_get_dn(sdn), delrv);
  139. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  140. "USN tombstone cleanup: deleting %s failed; (%d).\n",
  141. slapi_sdn_get_dn(sdn), delrv);
  142. rv = delrv;
  143. }
  144. slapi_pblock_init(delete_pb);
  145. slapi_task_inc_progress(task);
  146. }
  147. slapi_task_log_notice(task, "USN tombstone cleanup task finished.");
  148. slapi_task_log_status(task, "USN tombstone cleanup task finished.");
  149. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  150. "USN tombstone cleanup task finished.\n");
  151. bail:
  152. slapi_free_search_results_internal(search_pb);
  153. slapi_pblock_destroy(search_pb);
  154. slapi_pblock_destroy(delete_pb);
  155. if (cleanup_data->maxusn_to_delete) {
  156. slapi_ch_free_string(&filter);
  157. }
  158. /* this will queue the destruction of the task */
  159. slapi_task_finish(task, rv);
  160. slapi_task_dec_refcount(task);
  161. slapi_log_error(SLAPI_LOG_PLUGIN, USN_PLUGIN_SUBSYSTEM,
  162. "usn_cleanup_thread <-- refcount decremented.\n");
  163. slapi_log_error(SLAPI_LOG_TRACE, USN_PLUGIN_SUBSYSTEM,
  164. "<-- usn_cleanup_thread\n");
  165. }
  166. #define MAPPING_TREE_BASE_DN "cn=mapping tree,cn=config"
  167. static int
  168. _usn_cleanup_is_mmr_enabled(const char *suffix)
  169. {
  170. Slapi_PBlock *search_pb = NULL;
  171. Slapi_Entry **entries = NULL;
  172. char *base_dn = NULL;
  173. int rc = 0; /* disabled, by default */
  174. /* This function converts the old style DN to the new one */
  175. base_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",%s",
  176. suffix, MAPPING_TREE_BASE_DN);
  177. if (NULL == base_dn) {
  178. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  179. "_usn_cleanup_is_mmr_enabled: failed to normalize "
  180. "mappingtree dn for %s\n", suffix);
  181. return 1;
  182. }
  183. search_pb = slapi_pblock_new();
  184. slapi_search_internal_set_pb(search_pb, base_dn, LDAP_SCOPE_ONELEVEL,
  185. "objectclass=nsDS5ReplicationAgreement",
  186. NULL, 0, NULL, NULL, usn_get_identity(), 0);
  187. slapi_search_internal_pb(search_pb);
  188. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  189. if (LDAP_SUCCESS != rc) { /* agreement is not available */
  190. goto bail;
  191. }
  192. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  193. if (entries && *entries) {
  194. rc = 1; /* At least one agreement on the suffix is found */
  195. }
  196. bail:
  197. slapi_free_search_results_internal(search_pb);
  198. slapi_pblock_destroy(search_pb);
  199. slapi_ch_free_string(&base_dn);
  200. return rc;
  201. }
  202. static int
  203. usn_cleanup_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  204. int *returncode, char *returntext, void *arg)
  205. {
  206. PRThread *thread = NULL;
  207. char *cn = NULL;
  208. char *suffix = NULL;
  209. char *backend = NULL;
  210. char *maxusn = NULL;
  211. char *bind_dn;
  212. struct usn_cleanup_data *cleanup_data = NULL;
  213. int rv = SLAPI_DSE_CALLBACK_OK;
  214. Slapi_Task *task = NULL;
  215. Slapi_Backend *be = NULL;
  216. const Slapi_DN *be_suffix = NULL;
  217. slapi_log_error(SLAPI_LOG_TRACE, USN_PLUGIN_SUBSYSTEM,
  218. "--> usn_cleanup_add\n");
  219. *returncode = LDAP_SUCCESS;
  220. /* get the requestor dn */
  221. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  222. cn = slapi_entry_attr_get_charptr(e, "cn");
  223. if (NULL == cn) {
  224. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  225. rv = SLAPI_DSE_CALLBACK_ERROR;
  226. goto bail;
  227. }
  228. /* get args */
  229. suffix = slapi_entry_attr_get_charptr(e, "suffix");
  230. backend = slapi_entry_attr_get_charptr(e, "backend");
  231. maxusn = slapi_entry_attr_get_charptr(e, "maxusn_to_delete");
  232. if (!suffix && !backend) {
  233. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  234. "USN tombstone cleanup: Both suffix and backend are missing.\n");
  235. *returncode = LDAP_PARAM_ERROR;
  236. rv = SLAPI_DSE_CALLBACK_ERROR;
  237. goto bail;
  238. }
  239. /* suffix is not given, but backend is; get the suffix */
  240. if (!suffix && backend) {
  241. be = slapi_be_select_by_instance_name(backend);
  242. be_suffix = slapi_be_getsuffix(be, 0);
  243. if (be_suffix) {
  244. suffix = slapi_ch_strdup(slapi_sdn_get_ndn(be_suffix));
  245. } else {
  246. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  247. "USN tombstone cleanup: Backend %s is invalid.\n", backend);
  248. *returncode = LDAP_PARAM_ERROR;
  249. rv = SLAPI_DSE_CALLBACK_ERROR;
  250. goto bail;
  251. }
  252. }
  253. /* The suffix is the target of replication,
  254. * we don't want to clean up tombstones used by MMR */
  255. if (_usn_cleanup_is_mmr_enabled(suffix)) {
  256. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  257. "USN tombstone cleanup: Suffix %s is replicated. Unwilling to "
  258. "perform cleaning up tombstones.\n", suffix);
  259. *returncode = LDAP_UNWILLING_TO_PERFORM;
  260. rv = SLAPI_DSE_CALLBACK_ERROR;
  261. goto bail;
  262. }
  263. /* allocate new task now */
  264. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  265. if (task == NULL) {
  266. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  267. "USN tombstone cleanup: unable to allocate new task.\n");
  268. *returncode = LDAP_OPERATIONS_ERROR;
  269. rv = SLAPI_DSE_CALLBACK_ERROR;
  270. goto bail;
  271. }
  272. /* register our destructor for cleaning up our private data */
  273. slapi_task_set_destructor_fn(task, usn_cleanup_task_destructor);
  274. /* Stash our argument in the task for use by the task thread */
  275. cleanup_data =
  276. (struct usn_cleanup_data *)slapi_ch_malloc(sizeof(struct usn_cleanup_data));
  277. cleanup_data->suffix = suffix;
  278. suffix = NULL; /* don't free in this function */
  279. cleanup_data->maxusn_to_delete = maxusn;
  280. maxusn = NULL; /* don't free in this function */
  281. cleanup_data->bind_dn = bind_dn;
  282. bind_dn = NULL; /* don't free in this function */
  283. slapi_task_set_data(task, cleanup_data);
  284. /* start the USN tombstone cleanup task as a separate thread */
  285. thread = PR_CreateThread(PR_USER_THREAD, usn_cleanup_thread,
  286. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  287. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  288. if (thread == NULL) {
  289. slapi_log_error(SLAPI_LOG_FATAL, USN_PLUGIN_SUBSYSTEM,
  290. "USN tombstone cleanup: unable to create task thread.\n");
  291. *returncode = LDAP_OPERATIONS_ERROR;
  292. rv = SLAPI_DSE_CALLBACK_ERROR;
  293. slapi_task_finish(task, *returncode);
  294. } else {
  295. /* thread successful */
  296. rv = SLAPI_DSE_CALLBACK_OK;
  297. }
  298. bail:
  299. slapi_ch_free_string(&cn);
  300. slapi_ch_free_string(&suffix);
  301. slapi_ch_free_string(&backend);
  302. slapi_ch_free_string(&maxusn);
  303. slapi_log_error(SLAPI_LOG_TRACE, USN_PLUGIN_SUBSYSTEM,
  304. "<-- usn_cleanup_add\n");
  305. return rv;
  306. }
  307. static void
  308. usn_cleanup_task_destructor(Slapi_Task *task)
  309. {
  310. slapi_log_error(SLAPI_LOG_PLUGIN, USN_PLUGIN_SUBSYSTEM, "usn_cleanup_task_destructor -->\n");
  311. if (task) {
  312. struct usn_cleanup_data *mydata = (struct usn_cleanup_data *)slapi_task_get_data(task);
  313. while (slapi_task_get_refcount(task) > 0) {
  314. /* Yield to wait for the fixup task finishes. */
  315. DS_Sleep (PR_MillisecondsToInterval(100));
  316. }
  317. if (mydata) {
  318. slapi_ch_free_string(&mydata->suffix);
  319. slapi_ch_free_string(&mydata->maxusn_to_delete);
  320. slapi_ch_free_string(&mydata->bind_dn);
  321. /* Need to cast to avoid a compiler warning */
  322. slapi_ch_free((void **)&mydata);
  323. }
  324. }
  325. slapi_log_error(SLAPI_LOG_PLUGIN, USN_PLUGIN_SUBSYSTEM, "usn_cleanup_task_destructor <--\n");
  326. }