retrocl_cn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 "retrocl.h"
  42. static changeNumber retrocl_internal_cn = 0;
  43. static changeNumber retrocl_first_cn = 0;
  44. static int check_last_changenumber = 0;
  45. /*
  46. * Function: a2changeNumber
  47. *
  48. * Returns: changeNumber (long)
  49. *
  50. * Arguments: string
  51. *
  52. * Description: parses the string to a changenumber. changenumbers are
  53. * positive integers.
  54. *
  55. */
  56. static changeNumber a2changeNumber (const char *p)
  57. {
  58. changeNumber c;
  59. c = strntoul((char *)p,strlen(p),10);
  60. return c;
  61. }
  62. /*
  63. * Function: handle_cnum_entry
  64. * Arguments: op - pointer to Operation struct for this operation
  65. * e - pointer to returned entry.
  66. * Returns: nothing
  67. * Description: Search result handler for retrocl_getchangenum(). Sets the
  68. * op->o_handler_data to point to a structure which contains
  69. * the changenumber retrieved and an error code.
  70. */
  71. static int
  72. handle_cnum_entry( Slapi_Entry *e, void *callback_data )
  73. {
  74. cnumRet *cr = (cnumRet *)callback_data;
  75. Slapi_Value *sval=NULL;
  76. const struct berval *value;
  77. cr->cr_cnum = 0UL;
  78. cr->cr_time = NULL;
  79. if ( NULL != e ) {
  80. Slapi_Attr *chattr = NULL;
  81. sval = NULL;
  82. value = NULL;
  83. if ( slapi_entry_attr_find( e, attr_changenumber, &chattr ) == 0 ) {
  84. slapi_attr_first_value( chattr,&sval );
  85. if ( NULL != sval ) {
  86. value = slapi_value_get_berval ( sval );
  87. if( NULL != value && NULL != value->bv_val &&
  88. '\0' != value->bv_val[0]) {
  89. cr->cr_cnum = a2changeNumber( value->bv_val );
  90. }
  91. }
  92. }
  93. chattr = NULL;
  94. sval = NULL;
  95. value = NULL;
  96. chattr = NULL;
  97. sval = NULL;
  98. value = NULL;
  99. if ( slapi_entry_attr_find( e, attr_changetime, &chattr ) == 0 ) {
  100. slapi_attr_first_value( chattr,&sval );
  101. if ( NULL != sval) {
  102. value = slapi_value_get_berval ( sval );
  103. if (NULL != value && NULL != value->bv_val &&
  104. '\0' != value->bv_val[0]) {
  105. cr->cr_time = slapi_ch_strdup( value->bv_val );
  106. }
  107. }
  108. }
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Function: handle_cnum_result
  114. * Arguments: err - error code returned from search
  115. * callback_data - private data for callback
  116. * Returns: nothing
  117. * Description: result handler for retrocl_getchangenum(). Sets the cr_lderr
  118. * field of the cnumRet struct to the error returned
  119. * from the backend.
  120. */
  121. static void
  122. handle_cnum_result( int err, void *callback_data )
  123. {
  124. cnumRet *cr = (cnumRet *)callback_data;
  125. cr->cr_lderr = err;
  126. }
  127. /*
  128. * Function: retrocl_get_changenumbers
  129. *
  130. * Returns: 0/-1
  131. *
  132. * Arguments: none
  133. *
  134. * Description: reads the first and last entry in the changelog to obtain
  135. * the starting and ending change numbers.
  136. *
  137. */
  138. int retrocl_get_changenumbers(void)
  139. {
  140. cnumRet cr;
  141. if (retrocl_be_changelog == NULL) return -1;
  142. cr.cr_cnum = 0;
  143. cr.cr_time = 0;
  144. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_FIRST,
  145. (char *)attr_changenumber, /* cast away const */
  146. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  147. handle_cnum_entry, NULL);
  148. slapi_rwlock_wrlock(retrocl_cn_lock);
  149. retrocl_first_cn = cr.cr_cnum;
  150. slapi_ch_free(( void **) &cr.cr_time );
  151. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  152. (char *)attr_changenumber, /* cast away const */
  153. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  154. handle_cnum_entry, NULL);
  155. retrocl_internal_cn = cr.cr_cnum;
  156. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Got changenumbers %lu and %lu\n",
  157. retrocl_first_cn,
  158. retrocl_internal_cn);
  159. slapi_rwlock_unlock(retrocl_cn_lock);
  160. slapi_ch_free(( void **) &cr.cr_time );
  161. return 0;
  162. }
  163. /*
  164. * Function: retrocl_getchangetime
  165. * Arguments: type - one of SLAPI_SEQ_FIRST, SLAPI_SEQ_LAST
  166. * Returns: The time of the requested change record. If the return value is
  167. * NO_TIME, the changelog could not be read.
  168. * If err is non-NULL, the memory it points to is set the the
  169. * error code returned from the backend. If "type" is not valid,
  170. * *err is set to -1.
  171. * Description: Get the first or last changenumber stored in the changelog,
  172. * depending on the value of argument "type".
  173. */
  174. time_t retrocl_getchangetime( int type, int *err )
  175. {
  176. cnumRet cr;
  177. time_t ret;
  178. if ( type != SLAPI_SEQ_FIRST && type != SLAPI_SEQ_LAST ) {
  179. if ( err != NULL ) {
  180. *err = -1;
  181. }
  182. return NO_TIME;
  183. }
  184. memset( &cr, '\0', sizeof( cnumRet ));
  185. slapi_seq_callback( RETROCL_CHANGELOG_DN, type,
  186. (char *)attr_changenumber, /* cast away const */
  187. NULL,
  188. NULL, 0, &cr, NULL,
  189. handle_cnum_result, handle_cnum_entry, NULL );
  190. if ( err != NULL ) {
  191. *err = cr.cr_lderr;
  192. }
  193. if ( NULL == cr.cr_time ) {
  194. ret = NO_TIME;
  195. } else {
  196. ret = parse_localTime( cr.cr_time );
  197. }
  198. slapi_ch_free(( void **) &cr.cr_time );
  199. return ret;
  200. }
  201. /*
  202. * Function: retrocl_forget_changenumbers
  203. *
  204. * Returns: none
  205. *
  206. * Arguments: none
  207. *
  208. * Description: used only when the server is shutting down
  209. *
  210. */
  211. void retrocl_forget_changenumbers(void)
  212. {
  213. slapi_rwlock_wrlock(retrocl_cn_lock);
  214. retrocl_first_cn = 0;
  215. retrocl_internal_cn = 0;
  216. slapi_rwlock_unlock(retrocl_cn_lock);
  217. }
  218. /*
  219. * Function: retrocl_get_first_changenumber
  220. *
  221. * Returns: changeNumber
  222. *
  223. * Arguments: none
  224. *
  225. * Description: used in root DSE
  226. *
  227. */
  228. changeNumber retrocl_get_first_changenumber(void)
  229. {
  230. changeNumber cn;
  231. slapi_rwlock_rdlock(retrocl_cn_lock);
  232. cn = retrocl_first_cn;
  233. slapi_rwlock_unlock(retrocl_cn_lock);
  234. return cn;
  235. }
  236. /*
  237. * Function: retrocl_set_first_changenumber
  238. *
  239. * Returns: none
  240. *
  241. * Arguments: changenumber
  242. *
  243. * Description: used in changelog trimming
  244. *
  245. */
  246. void retrocl_set_first_changenumber(changeNumber cn)
  247. {
  248. slapi_rwlock_wrlock(retrocl_cn_lock);
  249. retrocl_first_cn = cn;
  250. slapi_rwlock_unlock(retrocl_cn_lock);
  251. }
  252. /*
  253. * Function: retrocl_get_last_changenumber
  254. *
  255. * Returns:
  256. *
  257. * Arguments:
  258. *
  259. * Description: used in root DSE
  260. *
  261. */
  262. changeNumber retrocl_get_last_changenumber(void)
  263. {
  264. changeNumber cn;
  265. slapi_rwlock_rdlock(retrocl_cn_lock);
  266. cn = retrocl_internal_cn;
  267. slapi_rwlock_unlock(retrocl_cn_lock);
  268. return cn;
  269. }
  270. /*
  271. * Function: retrocl_commit_changenumber
  272. *
  273. * Returns: none
  274. *
  275. * Arguments: none, lock must be held
  276. *
  277. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  278. *
  279. */
  280. void retrocl_commit_changenumber(void)
  281. {
  282. slapi_rwlock_wrlock(retrocl_cn_lock);
  283. if ( retrocl_first_cn == 0) {
  284. retrocl_first_cn = retrocl_internal_cn;
  285. }
  286. slapi_rwlock_unlock(retrocl_cn_lock);
  287. }
  288. /*
  289. * Function: retrocl_release_changenumber
  290. *
  291. * Returns: none
  292. *
  293. * Arguments: none, lock must be held
  294. *
  295. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  296. *
  297. */
  298. void retrocl_release_changenumber(void)
  299. {
  300. slapi_rwlock_wrlock(retrocl_cn_lock);
  301. retrocl_internal_cn--;
  302. slapi_rwlock_unlock(retrocl_cn_lock);
  303. }
  304. /*
  305. * Function: retrocl_update_lastchangenumber
  306. *
  307. * Returns: 0/-1
  308. *
  309. * Arguments: none. The caller should have taken write lock for the change numbers
  310. *
  311. * Description: reads the last entry in the changelog to obtain
  312. * the last change number.
  313. *
  314. */
  315. int retrocl_update_lastchangenumber(void)
  316. {
  317. cnumRet cr;
  318. if (retrocl_be_changelog == NULL) return -1;
  319. slapi_rwlock_unlock(retrocl_cn_lock);
  320. cr.cr_cnum = 0;
  321. cr.cr_time = 0;
  322. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  323. (char *)attr_changenumber, /* cast away const */
  324. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  325. handle_cnum_entry, NULL);
  326. slapi_rwlock_wrlock(retrocl_cn_lock);
  327. retrocl_internal_cn = cr.cr_cnum;
  328. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Refetched last changenumber = %lu \n",
  329. retrocl_internal_cn);
  330. slapi_ch_free(( void **) &cr.cr_time );
  331. return 0;
  332. }
  333. /*
  334. * Function: retrocl_assign_changenumber
  335. *
  336. * Returns: change number, 0 on error
  337. *
  338. * Arguments: none. Lock must be held.
  339. *
  340. * Description: NOTE! MUST BE FOLLOWED BY retrocl_commit_changenumber or
  341. * retrocl_release_changenumber
  342. *
  343. */
  344. changeNumber retrocl_assign_changenumber(void)
  345. {
  346. changeNumber cn;
  347. /* Before we assign the changenumber; we should check for the
  348. * validity of the internal assignment of retrocl_internal_cn
  349. * we had from the startup */
  350. slapi_rwlock_wrlock(retrocl_cn_lock);
  351. if((check_last_changenumber) ||
  352. ((retrocl_internal_cn <= retrocl_first_cn) &&
  353. (retrocl_internal_cn > 1 ))){
  354. /* the numbers have become out of sync - retrocl_get_changenumbers
  355. * gets called only once during startup and it may have had a problem
  356. * getting the last changenumber.
  357. * If there was any problem then update the lastchangenumber from the changelog db.
  358. * This function is being called by only the thread that is actually writing
  359. * to the changelog.
  360. *
  361. * after the first change was applied both _cn numbers are 1, that's ok
  362. */
  363. retrocl_update_lastchangenumber();
  364. check_last_changenumber = 0;
  365. }
  366. retrocl_internal_cn++;
  367. cn = retrocl_internal_cn;
  368. slapi_rwlock_unlock(retrocl_cn_lock);
  369. return cn;
  370. }
  371. void retrocl_set_check_changenumber(void)
  372. {
  373. slapi_rwlock_wrlock(retrocl_cn_lock);
  374. check_last_changenumber = 1;
  375. slapi_rwlock_unlock(retrocl_cn_lock);
  376. }