idl.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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. /* idl.c - ldap id list handling routines */
  42. #include "back-ldbm.h"
  43. /*
  44. * Disable idl locking since it causes unbreakable deadlock.
  45. */
  46. #undef IDL_LOCKING_ENABLE
  47. static void make_cont_key( DBT *contkey, DBT *key, ID id );
  48. static int idl_insert_maxids( IDList **idl, ID id, int maxids );
  49. /* for the cache of open index files */
  50. struct idl_private {
  51. int idl_maxids; /* Number of IDS in a block */
  52. int idl_maxindirect; /* Number of blocks allowed */
  53. size_t idl_allidslimit; /* Max number of IDs before it turns to allids */
  54. #ifdef IDL_LOCKING_ENABLE
  55. PRRWLock *idl_rwlock;
  56. #endif
  57. };
  58. static int idl_tune = DEFAULT_IDL_TUNE; /* tuning parameters for IDL code */
  59. #define IDL_TUNE_BSEARCH 1 /* do a binary search when inserting into an IDL */
  60. #define IDL_TUNE_NOPAD 2 /* Don't pad IDLs with space at the end */
  61. void idl_old_set_tune(int val)
  62. {
  63. idl_tune = val;
  64. }
  65. int idl_old_get_tune() {
  66. return idl_tune;
  67. }
  68. size_t idl_old_get_allidslimit(struct attrinfo *a)
  69. {
  70. idl_private *priv = NULL;
  71. PR_ASSERT(NULL != a);
  72. PR_ASSERT(NULL != a->ai_idl);
  73. priv = a->ai_idl;
  74. return priv->idl_allidslimit;
  75. }
  76. static void idl_init_maxids(struct ldbminfo *li,idl_private *priv)
  77. {
  78. const size_t blksize = dblayer_get_optimal_block_size(li);
  79. if (0 == li->li_allidsthreshold) {
  80. li->li_allidsthreshold = DEFAULT_ALLIDSTHRESHOLD;
  81. }
  82. priv->idl_maxids = (blksize / sizeof(ID)) - 2;
  83. priv->idl_maxindirect = (li->li_allidsthreshold / priv->idl_maxids) + 1;
  84. priv->idl_allidslimit = (priv->idl_maxids * priv->idl_maxindirect);
  85. LDAPDebug (LDAP_DEBUG_ARGS,
  86. "idl_init_private: blksize %lu, maxids %i, maxindirect %i\n",
  87. (unsigned long)blksize, priv->idl_maxids, priv->idl_maxindirect);
  88. }
  89. /* routine to initialize the private data used by the IDL code per-attribute */
  90. int idl_old_init_private(backend *be,struct attrinfo *a)
  91. {
  92. idl_private *priv = NULL;
  93. PR_ASSERT(NULL != a);
  94. PR_ASSERT(NULL == a->ai_idl);
  95. priv = (idl_private*) slapi_ch_malloc(sizeof(idl_private));
  96. if (NULL == priv) {
  97. return -1; /* Memory allocation failure */
  98. }
  99. {
  100. priv->idl_maxids = 0;
  101. priv->idl_maxindirect = 0;
  102. }
  103. #ifdef IDL_LOCKING_ENABLE
  104. priv->idl_rwlock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "idl lock");
  105. if (NULL == priv->idl_rwlock) {
  106. slapi_ch_free((void**)&priv);
  107. return -1;
  108. }
  109. #endif
  110. a->ai_idl = (void*)priv;
  111. return 0;
  112. }
  113. /* routine to release resources used by IDL private data structure */
  114. int idl_old_release_private(struct attrinfo *a)
  115. {
  116. PR_ASSERT(NULL != a);
  117. if (NULL != a->ai_idl)
  118. {
  119. #ifdef IDL_LOCKING_ENABLE
  120. idl_private *priv = a->ai_idl;
  121. PR_ASSERT(NULL != priv->idl_rwlock);
  122. PR_DestroyRWLock(priv->idl_rwlock);
  123. #endif
  124. slapi_ch_free( (void **)&(a->ai_idl) );
  125. }
  126. return 0;
  127. }
  128. /* Locks one IDL so we can modify it knowing that
  129. * nobody else is trying to do so at the same time
  130. * also called by readers, since they need to be blocked
  131. * when they read to avoid them seeing inconsistent data
  132. * This is not really necessary for update operations
  133. * today because they are already serialized by a lock
  134. * at the backend level but is still necessary to
  135. * stop concurrent access by one update thread and
  136. * some other search threads
  137. */
  138. #ifdef IDL_LOCKING_ENABLE
  139. static void idl_Wlock_list(idl_private *priv, DBT *key)
  140. {
  141. PRRWLock *lock = NULL;
  142. PR_ASSERT(NULL != priv);
  143. lock = priv->idl_rwlock;
  144. PR_ASSERT(NULL != lock);
  145. PR_RWLock_Wlock(lock);
  146. }
  147. static void idl_Rlock_list(idl_private *priv, DBT *key)
  148. {
  149. PRRWLock *lock = NULL;
  150. PR_ASSERT(NULL != priv);
  151. lock = priv->idl_rwlock;
  152. PR_ASSERT(NULL != lock);
  153. PR_RWLock_Rlock(lock);
  154. }
  155. static void idl_unlock_list(idl_private *priv, DBT *key)
  156. {
  157. PRRWLock *lock = NULL;
  158. PR_ASSERT(NULL != priv);
  159. lock = priv->idl_rwlock;
  160. PR_ASSERT(NULL != lock);
  161. PR_RWLock_Unlock(lock);
  162. }
  163. #endif
  164. #ifndef IDL_LOCKING_ENABLE
  165. #define idl_Wlock_list(idl,dbt)
  166. #define idl_Rlock_list(idl,dbt)
  167. #define idl_unlock_list(idl,dbt)
  168. #endif
  169. /*
  170. * idl_fetch_one - fetch a single IDList from the database and return a
  171. * pointer to it.
  172. *
  173. * this routine always propagates errors other than DB_LOCK_DEADLOCK.
  174. * for DB_LOCK_DEADLOCK, it propagates the error if called inside a
  175. * transaction. if called not inside a transaction, it loops on
  176. * DB_LOCK_DEADLOCK, retrying the fetch.
  177. *
  178. */
  179. static IDList *
  180. idl_fetch_one(
  181. struct ldbminfo *li,
  182. DB *db,
  183. DBT *key,
  184. DB_TXN *txn,
  185. int *err
  186. )
  187. {
  188. DBT data = {0};
  189. IDList *idl = NULL;
  190. /* LDAPDebug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
  191. data.flags = DB_DBT_MALLOC;
  192. do {
  193. *err = db->get( db, txn, key, &data, 0 );
  194. if ( 0 != *err && DB_NOTFOUND != *err && DB_LOCK_DEADLOCK != *err )
  195. {
  196. char *msg;
  197. if ( EPERM == *err && *err != errno ) {
  198. LDAPDebug( LDAP_DEBUG_ANY,
  199. "idl_fetch_one(%s): Database failed to run, "
  200. "There is either insufficient disk space or "
  201. "insufficient memory available for database.\n",
  202. ((char*)key->dptr)[ key->dsize - 1 ] ?
  203. "" : (char*)key->dptr, 0, 0 );
  204. } else {
  205. LDAPDebug( LDAP_DEBUG_ANY,
  206. "idl_fetch_one error %d %s\n",
  207. *err, (msg = dblayer_strerror( *err )) ? msg : "", 0 );
  208. }
  209. }
  210. }
  211. while ( DB_LOCK_DEADLOCK == *err && NULL == txn );
  212. if (0 == *err) {
  213. idl = (IDList *) data.data;
  214. }
  215. return( idl );
  216. }
  217. IDList *
  218. idl_old_fetch(
  219. backend *be,
  220. DB *db,
  221. DBT *key,
  222. DB_TXN *txn,
  223. struct attrinfo *a,
  224. int *err
  225. )
  226. {
  227. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  228. DBT k2 = {0};
  229. IDList *idl;
  230. IDList **tmp;
  231. back_txn s_txn;
  232. char *kstr;
  233. int i;
  234. unsigned long nids;
  235. /* LDAPDebug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
  236. if ( (idl = idl_fetch_one( li, db, key, txn, err )) == NULL ) {
  237. return( NULL );
  238. }
  239. /* regular block */
  240. if ( ! INDIRECT_BLOCK( idl ) ) {
  241. /* make sure we have the current value of highest id */
  242. if ( ALLIDS(idl) ) {
  243. idl_free( idl );
  244. idl = idl_allids( be );
  245. }
  246. return( idl );
  247. }
  248. idl_free( idl );
  249. /* Taking a transaction is expensive; so we try and optimize for the common case by not
  250. taking one above. If we have a indirect block; we need to take a transaction and re-read
  251. the idl since they could have been changed by another thread after we read the first block
  252. above */
  253. dblayer_txn_init(li,&s_txn);
  254. if (NULL != txn)
  255. {
  256. dblayer_read_txn_begin(li,txn,&s_txn);
  257. }
  258. if ( (idl = idl_fetch_one( li, db, key, s_txn.back_txn_txn, err )) == NULL ) {
  259. dblayer_read_txn_commit(li,&s_txn);
  260. return( NULL );
  261. }
  262. /* regular block */
  263. if ( ! INDIRECT_BLOCK( idl ) ) {
  264. dblayer_read_txn_commit(li,&s_txn);
  265. /* make sure we have the current value of highest id */
  266. if ( ALLIDS(idl) ) {
  267. idl_free( idl );
  268. idl = idl_allids( be );
  269. }
  270. return( idl );
  271. }
  272. /*
  273. * this is an indirect block which points to other blocks.
  274. * we need to read in all the blocks it points to and construct
  275. * a big id list containing all the ids, which we will return.
  276. */
  277. /* count the number of blocks & allocate space for pointers to them */
  278. for ( i = 0; idl->b_ids[i] != NOID; i++ )
  279. ; /* NULL */
  280. tmp = (IDList **) slapi_ch_malloc( (i + 1) * sizeof(IDList *) );
  281. /* read in all the blocks */
  282. kstr = (char *) slapi_ch_malloc( key->dsize + 20 );
  283. nids = 0;
  284. for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
  285. ID thisID = idl->b_ids[i];
  286. ID nextID = idl->b_ids[i+1];
  287. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr, (u_long)thisID );
  288. k2.dptr = kstr;
  289. k2.dsize = strlen( kstr ) + 1;
  290. if ( (tmp[i] = idl_fetch_one( li, db, &k2, s_txn.back_txn_txn, err )) == NULL ) {
  291. if(*err == DB_LOCK_DEADLOCK) {
  292. dblayer_read_txn_abort(li,&s_txn);
  293. } else {
  294. dblayer_read_txn_commit(li,&s_txn);
  295. }
  296. slapi_ch_free((void**)&kstr );
  297. slapi_ch_free((void**)&tmp );
  298. return( NULL );
  299. }
  300. nids += tmp[i]->b_nids;
  301. /* Check for inconsistencies: */
  302. if ( tmp[i]->b_ids[0] != thisID ) {
  303. LDAPDebug (LDAP_DEBUG_ANY, "idl_fetch_one(%s)->b_ids[0] == %lu\n",
  304. k2.dptr, (u_long)tmp[i]->b_ids[0], 0);
  305. }
  306. if ( nextID != NOID ) {
  307. if ( nextID <= thisID ) {
  308. LDAPDebug (LDAP_DEBUG_ANY, "indirect block (%s) contains %lu, %lu\n",
  309. key->dptr, (u_long)thisID, (u_long)nextID);
  310. }
  311. if ( nextID <= tmp[i]->b_ids[(tmp[i]->b_nids)-1] ) {
  312. LDAPDebug (LDAP_DEBUG_ANY, "idl_fetch_one(%s)->b_ids[last] == %lu"
  313. " >= %lu (next indirect ID)\n",
  314. k2.dptr, (u_long)tmp[i]->b_ids[(tmp[i]->b_nids)-1], (u_long)nextID);
  315. }
  316. }
  317. }
  318. dblayer_read_txn_commit(li,&s_txn);
  319. tmp[i] = NULL;
  320. slapi_ch_free((void**)&kstr );
  321. idl_free( idl );
  322. /* allocate space for the big block */
  323. idl = idl_alloc( nids );
  324. idl->b_nids = nids;
  325. nids = 0;
  326. /* copy in all the ids from the component blocks */
  327. for ( i = 0; tmp[i] != NULL; i++ ) {
  328. if ( tmp[i] == NULL ) {
  329. continue;
  330. }
  331. SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
  332. tmp[i]->b_nids * sizeof(ID) );
  333. nids += tmp[i]->b_nids;
  334. idl_free( tmp[i] );
  335. }
  336. slapi_ch_free((void**)&tmp );
  337. LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_fetch %lu ids (%lu max)\n", (u_long)idl->b_nids,
  338. (u_long)idl->b_nmax, 0 );
  339. return( idl );
  340. }
  341. static int
  342. idl_store(
  343. backend *be,
  344. DB *db,
  345. DBT *key,
  346. IDList *idl,
  347. DB_TXN *txn
  348. )
  349. {
  350. int rc;
  351. DBT data = {0};
  352. /* LDAPDebug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
  353. data.dptr = (char *) idl;
  354. data.dsize = (2 + idl->b_nmax) * sizeof(ID);
  355. rc = db->put( db, txn, key, &data, 0 );
  356. if ( 0 != rc ) {
  357. char *msg;
  358. if ( EPERM == rc && rc != errno ) {
  359. LDAPDebug( LDAP_DEBUG_ANY,
  360. "idl_store(%s): Database failed to run, "
  361. "There is insufficient memory available for database.\n",
  362. ((char*)key->dptr)[ key->dsize - 1 ] ? "" : (char*)key->dptr, 0, 0 );
  363. } else {
  364. if (LDBM_OS_ERR_IS_DISKFULL(rc)) {
  365. operation_out_of_disk_space();
  366. }
  367. LDAPDebug( ((DB_LOCK_DEADLOCK == rc) ? LDAP_DEBUG_TRACE : LDAP_DEBUG_ANY),
  368. "idl_store(%s) returns %d %s\n",
  369. ((char*)key->dptr)[ key->dsize - 1 ] ? "" : (char*)key->dptr,
  370. rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  371. if (rc == DB_RUNRECOVERY) {
  372. LDAPDebug(LDAP_DEBUG_ANY, "%s\n", "Note: idl_store failures can be an indication of insufficient disk space.", 0, 0);
  373. ldbm_nasty("idl_store",71,rc);
  374. }
  375. }
  376. }
  377. /* LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
  378. return( rc );
  379. }
  380. static void
  381. idl_split_block(
  382. IDList *b,
  383. ID id,
  384. IDList **n1,
  385. IDList **n2
  386. )
  387. {
  388. ID i;
  389. /* find where to split the block */
  390. for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
  391. ; /* NULL */
  392. *n1 = idl_alloc( i == 0 ? 1 : i );
  393. *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
  394. /*
  395. * everything before the id being inserted in the first block
  396. * unless there is nothing, in which case the id being inserted
  397. * goes there.
  398. */
  399. SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
  400. i * sizeof(ID) );
  401. (*n1)->b_nids = (i == 0 ? 1 : i);
  402. if ( i == 0 ) {
  403. (*n1)->b_ids[0] = id;
  404. } else {
  405. (*n2)->b_ids[0] = id;
  406. }
  407. /* the id being inserted & everything after in the second block */
  408. SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
  409. (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
  410. (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
  411. }
  412. /*
  413. * idl_change_first - called when an indirect block's first key has
  414. * changed, meaning it needs to be stored under a new key, and the
  415. * header block pointing to it needs updating.
  416. */
  417. static int
  418. idl_change_first(
  419. backend *be,
  420. DB *db,
  421. DBT *hkey, /* header block key */
  422. IDList *h, /* header block */
  423. int pos, /* pos in h to update */
  424. DBT *bkey, /* data block key */
  425. IDList *b, /* data block */
  426. DB_TXN *txn
  427. )
  428. {
  429. int rc;
  430. char *msg;
  431. /* LDAPDebug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
  432. /* delete old key block */
  433. rc = db->del( db, txn, bkey, 0 );
  434. if ( (rc != 0) && (DB_LOCK_DEADLOCK != rc) )
  435. {
  436. LDAPDebug( LDAP_DEBUG_ANY, "idl_change_first del (%s) err %d %s\n",
  437. bkey->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  438. if (rc == DB_RUNRECOVERY) {
  439. ldbm_nasty("idl_store",72,rc);
  440. }
  441. return( rc );
  442. }
  443. /* write block with new key */
  444. sprintf( bkey->dptr, "%c%s%lu", CONT_PREFIX, (char *)hkey->dptr, (u_long)b->b_ids[0] );
  445. bkey->dsize = strlen( bkey->dptr ) + 1;
  446. if ( (rc = idl_store( be, db, bkey, b, txn )) != 0 ) {
  447. return( rc );
  448. }
  449. /* update + write indirect header block */
  450. h->b_ids[pos] = b->b_ids[0];
  451. if ( (rc = idl_store( be, db, hkey, h, txn )) != 0 ) {
  452. return( rc );
  453. }
  454. return( 0 );
  455. }
  456. #define IDL_CHECK_FAILED(FORMAT, ARG1, ARG2) \
  457. do { \
  458. char* fmt = slapi_ch_malloc (strlen(func) + strlen(note) + strlen(FORMAT) + 30); \
  459. if (fmt != NULL) { \
  460. sprintf (fmt, "%s(%%s,%lu) %s: %s\n", func, (u_long)id, note, FORMAT); \
  461. LDAPDebug (LDAP_DEBUG_ANY, fmt, key->dptr, ARG1, ARG2); \
  462. slapi_ch_free((void**)&fmt); \
  463. } \
  464. } while(0)
  465. static void
  466. idl_check_indirect (IDList* idl, int i, IDList* tmp, IDList* tmp2,
  467. char* func, char* note, DBT* key, ID id)
  468. /* Check for inconsistencies; report any via LDAPDebug(LDAP_DEBUG_ANY).
  469. The caller alleges that *idl is a header block, in which the
  470. i'th item points to the indirect block *tmp, and either tmp2 == NULL
  471. or *tmp2 is the indirect block to which the i+1'th item in *idl points.
  472. The other parameters are merely output in each error message, like:
  473. printf ("%s(%s,%lu) %s: ...", func, key->dptr, (u_long)id, note, ...)
  474. */
  475. {
  476. /* The implementation is optimized for no inconsistencies. */
  477. const ID thisID = idl->b_ids[i];
  478. const ID nextID = idl->b_ids[i+1];
  479. const ID tmp0 = tmp->b_ids[0];
  480. const ID tmpLast = tmp->b_ids[tmp->b_nids-1];
  481. if (tmp0 != thisID) {
  482. IDL_CHECK_FAILED ("tmp->b_ids[0] == %lu, not %lu\n",
  483. (u_long)tmp0, (u_long)thisID);
  484. }
  485. if (tmp0 > tmpLast) {
  486. IDL_CHECK_FAILED ("tmp->b_ids[0] == %lu > %lu [last]\n",
  487. (u_long)tmp0, (u_long)tmpLast);
  488. }
  489. if (nextID == NOID) {
  490. if (tmp2 != NULL) {
  491. IDL_CHECK_FAILED ("idl->b_ids[%i+1] == NOID, but tmp2 != NULL\n", i, 0);
  492. }
  493. } else {
  494. if (nextID <= thisID) {
  495. IDL_CHECK_FAILED ("idl->b_ids contains %lu, %lu\n", (u_long)thisID, (u_long)nextID);
  496. }
  497. if (nextID <= tmpLast) {
  498. IDL_CHECK_FAILED ("idl->b_ids[i+1] == %lu <= %lu (last of idl->b_ids[i])\n",
  499. (u_long)nextID, (u_long)tmpLast);
  500. }
  501. if (tmp2 != NULL && tmp2->b_ids[0] != nextID) {
  502. IDL_CHECK_FAILED ("tmp2->b_ids[0] == %lu, not %lu\n",
  503. (u_long)tmp2->b_ids[0], (u_long)nextID);
  504. }
  505. }
  506. }
  507. int
  508. idl_old_insert_key(
  509. backend *be,
  510. DB *db,
  511. DBT *key,
  512. ID id,
  513. DB_TXN *txn,
  514. struct attrinfo *a,
  515. int *disposition
  516. )
  517. {
  518. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  519. int i, j, rc = 0;
  520. char *msg;
  521. IDList *idl, *tmp, *tmp2, *tmp3;
  522. char *kstr;
  523. DBT k2 = {0};
  524. DBT k3 = {0};
  525. if (NULL != disposition) {
  526. *disposition = IDL_INSERT_NORMAL;
  527. }
  528. if (0 == a->ai_idl->idl_maxids) {
  529. idl_init_maxids(li,a->ai_idl);
  530. }
  531. idl_Wlock_list(a->ai_idl,key);
  532. if ( (idl = idl_fetch_one( li, db, key, txn, &rc )) == NULL ) {
  533. if ( rc != 0 && rc != DB_NOTFOUND ) {
  534. if ( rc != DB_LOCK_DEADLOCK )
  535. {
  536. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 0 BAD %d %s\n",
  537. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  538. }
  539. return( rc );
  540. }
  541. idl = idl_alloc( 1 );
  542. idl->b_ids[idl->b_nids++] = id;
  543. rc = idl_store( be, db, key, idl, txn );
  544. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  545. {
  546. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 1 BAD %d %s\n",
  547. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  548. }
  549. idl_free( idl );
  550. idl_unlock_list(a->ai_idl,key);
  551. return( rc );
  552. }
  553. /* regular block */
  554. if ( ! INDIRECT_BLOCK( idl ) ) {
  555. switch ( idl_insert_maxids( &idl, id, a->ai_idl->idl_maxids ) ) {
  556. case 0: /* id inserted - store the updated block */
  557. case 1:
  558. rc = idl_store( be, db, key, idl, txn );
  559. break;
  560. case 2: /* id already there - nothing to do */
  561. rc = 0;
  562. /* Could be an ALLID block, let's check */
  563. if (ALLIDS(idl)) {
  564. if (NULL != disposition) {
  565. *disposition = IDL_INSERT_ALLIDS;
  566. }
  567. }
  568. break;
  569. case 3: /* id not inserted - block must be split */
  570. /* check threshold for marking this an all-id block */
  571. if ( a->ai_idl->idl_maxindirect < 2 ) {
  572. idl_free( idl );
  573. idl = idl_allids( be );
  574. rc = idl_store( be, db, key, idl, txn );
  575. idl_free( idl );
  576. idl_unlock_list(a->ai_idl,key);
  577. if ( rc != 0 && rc != DB_LOCK_DEADLOCK)
  578. {
  579. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 2 BAD %d %s\n",
  580. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  581. }
  582. if (NULL != disposition) {
  583. *disposition = IDL_INSERT_NOW_ALLIDS;
  584. }
  585. return( rc );
  586. }
  587. idl_split_block( idl, id, &tmp, &tmp2 );
  588. idl_free( idl );
  589. /* create the header indirect block */
  590. idl = idl_alloc( 3 );
  591. idl->b_nmax = 3;
  592. idl->b_nids = INDBLOCK;
  593. idl->b_ids[0] = tmp->b_ids[0];
  594. idl->b_ids[1] = tmp2->b_ids[0];
  595. idl->b_ids[2] = NOID;
  596. /* store it */
  597. rc = idl_store( be, db, key, idl, txn );
  598. if ( rc != 0 ) {
  599. idl_free( idl );
  600. idl_free( tmp );
  601. idl_free( tmp2 );
  602. if ( rc != DB_LOCK_DEADLOCK )
  603. {
  604. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 3 BAD %d %s\n",
  605. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  606. }
  607. return( rc );
  608. }
  609. /* store the first id block */
  610. kstr = (char *) slapi_ch_malloc( key->dsize + 20 );
  611. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr,
  612. (u_long)tmp->b_ids[0] );
  613. k2.dptr = kstr;
  614. k2.dsize = strlen( kstr ) + 1;
  615. rc = idl_store( be, db, &k2, tmp, txn );
  616. /* store the second id block */
  617. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr,
  618. (u_long)tmp2->b_ids[0] );
  619. k2.dptr = kstr;
  620. k2.dsize = strlen( kstr ) + 1;
  621. rc = idl_store( be, db, &k2, tmp2, txn );
  622. if ( rc != 0 ) {
  623. idl_free( idl );
  624. idl_free( tmp );
  625. idl_free( tmp2 );
  626. if ( rc != DB_LOCK_DEADLOCK )
  627. {
  628. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 4 BAD %d %s\n",
  629. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  630. }
  631. return( rc );
  632. }
  633. idl_check_indirect (idl, 0, tmp, tmp2,
  634. "idl_insert_key", "split", key, id);
  635. slapi_ch_free((void**)&kstr );
  636. idl_free( tmp );
  637. idl_free( tmp2 );
  638. break;
  639. }
  640. idl_free( idl );
  641. idl_unlock_list(a->ai_idl,key);
  642. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  643. {
  644. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 5 BAD %d %s\n",
  645. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  646. }
  647. return( rc );
  648. }
  649. /*
  650. * this is an indirect block which points to other blocks.
  651. * we need to read in the block into which the id should be
  652. * inserted, then insert the id and store the block. we might
  653. * have to split the block if it is full, which means we also
  654. * need to write a new "header" block.
  655. */
  656. /* select the block to try inserting into */
  657. for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
  658. ; /* NULL */
  659. if ( id == idl->b_ids[i] ) { /* already in a block */
  660. #ifdef _DEBUG_LARGE_BLOCKS
  661. LDAPDebug( LDAP_DEBUG_ANY,
  662. "id %lu for key (%s) is already in block %d\n",
  663. (u_long)id, key.dptr, i);
  664. #endif
  665. idl_unlock_list(a->ai_idl,key);
  666. idl_free( idl );
  667. return( 0 );
  668. }
  669. if ( i != 0 ) {
  670. i--;
  671. }
  672. /* get the block */
  673. kstr = (char *) slapi_ch_malloc( key->dsize + 20 );
  674. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr, (u_long)idl->b_ids[i] );
  675. k2.dptr = kstr;
  676. k2.dsize = strlen( kstr ) + 1;
  677. if ( (tmp = idl_fetch_one( li, db, &k2, txn, &rc )) == NULL ) {
  678. if ( rc != 0 ) {
  679. if ( rc != DB_LOCK_DEADLOCK )
  680. {
  681. LDAPDebug( LDAP_DEBUG_ANY, "idl_insert_key 5.5 BAD %d %s\n",
  682. rc, (msg = dblayer_strerror( rc )) ? msg : "", 0 );
  683. }
  684. return( rc );
  685. }
  686. LDAPDebug( LDAP_DEBUG_ANY,
  687. "nonexistent continuation block (%s)\n", k2.dptr, 0, 0 );
  688. idl_unlock_list(a->ai_idl,key);
  689. idl_free( idl );
  690. slapi_ch_free((void**)&kstr );
  691. return( -1 );
  692. }
  693. /* insert the id */
  694. switch ( idl_insert_maxids( &tmp, id, a->ai_idl->idl_maxids ) ) {
  695. case 0: /* id inserted ok */
  696. rc = idl_store( be, db, &k2, tmp, txn );
  697. if (0 != rc) {
  698. idl_check_indirect (idl, i, tmp, NULL,
  699. "idl_insert_key", "indirect", key, id);
  700. }
  701. break;
  702. case 1: /* id inserted - first id in block has changed */
  703. /*
  704. * key for this block has changed, so we have to
  705. * write the block under the new key, delete the
  706. * old key block + update and write the indirect
  707. * header block.
  708. */
  709. rc = idl_change_first( be, db, key, idl, i, &k2, tmp, txn );
  710. if ( rc != 0 ) {
  711. break; /* return error in rc */
  712. }
  713. idl_check_indirect (idl, i, tmp, NULL,
  714. "idl_insert_key", "indirect 1", key, id);
  715. break;
  716. case 2: /* id not inserted - already there */
  717. idl_check_indirect (idl, i, tmp, NULL,
  718. "idl_insert_key", "indirect no change", key, id);
  719. break;
  720. case 3: /* id not inserted - block is full */
  721. /*
  722. * first, see if we can shift ids down one, moving
  723. * the last id in the current block to the next
  724. * block, and then adding the id we are inserting to
  725. * the current block. we'll need to split the block
  726. * otherwise.
  727. */
  728. /* is there a next block? */
  729. if ( idl->b_ids[i + 1] != NOID ) {
  730. char *kstr3 = (char *) slapi_ch_malloc( key->dsize + 20 );
  731. /* yes - read it in */
  732. sprintf( kstr3, "%c%s%lu", CONT_PREFIX, (char *)key->dptr,
  733. (u_long)idl->b_ids[i + 1] );
  734. k3.dptr = kstr3;
  735. k3.dsize = strlen( kstr3 ) + 1;
  736. if ( (tmp2 = idl_fetch_one( li, db, &k3, txn, &rc ))
  737. == NULL ) {
  738. if ( rc != DB_LOCK_DEADLOCK )
  739. {
  740. LDAPDebug( LDAP_DEBUG_ANY,
  741. "idl_fetch_one (%s) returns NULL\n",
  742. k3.dptr, 0, 0 );
  743. }
  744. if (0 != rc) {
  745. idl_check_indirect (idl, i, tmp, NULL,
  746. "idl_insert_key", "indirect missing", key, id);
  747. }
  748. break;
  749. }
  750. /*
  751. * insert the last key in the previous block in
  752. * the next block. it should go at the beginning
  753. * always, if it fits at all.
  754. */
  755. rc = idl_insert_maxids (&tmp2,
  756. id > tmp->b_ids[tmp->b_nids-1] ?
  757. id : tmp->b_ids[tmp->b_nids-1],
  758. a->ai_idl->idl_maxids);
  759. switch ( rc ) {
  760. case 1: /* id inserted first in block */
  761. rc = idl_change_first( be, db, key, idl,
  762. i + 1, &k3, tmp2, txn );
  763. if ( rc != 0 ) {
  764. break; /* return error in rc */
  765. }
  766. if (id < tmp->b_ids[tmp->b_nids-1]) {
  767. /*
  768. * we inserted the last id in the previous
  769. * block in this block. we need to "remove"
  770. * it from the previous block and insert the
  771. * new id. decrementing the b_nids count
  772. * in the previous block has the effect
  773. * of removing the last id.
  774. */
  775. /* remove last id in previous block */
  776. tmp->b_nids--;
  777. /* insert new id in previous block */
  778. switch ( (rc = idl_insert_maxids( &tmp, id,
  779. a->ai_idl->idl_maxids )) ) {
  780. case 0: /* id inserted */
  781. rc = idl_store( be, db, &k2, tmp, txn );
  782. break;
  783. case 1: /* first in block */
  784. rc = idl_change_first( be, db, key, idl,
  785. i, &k2, tmp, txn );
  786. break;
  787. case 2: /* already there - how? */
  788. case 3: /* split block - how? */
  789. LDAPDebug( LDAP_DEBUG_ANY,
  790. "not expecting (%d) from idl_insert_maxids of %lu in (%s)\n",
  791. rc, (u_long)id, k2.dptr );
  792. LDAPDebug( LDAP_DEBUG_ANY,
  793. "likely database corruption\n",
  794. 0, 0, 0 );
  795. rc = 0;
  796. break;
  797. }
  798. }
  799. if ( rc != 0 ) {
  800. break; /* return error in rc */
  801. }
  802. idl_check_indirect (idl, i, tmp, tmp2,
  803. "idl_insert_key", "overflow", key, id);
  804. slapi_ch_free( (void **)&(k2.dptr) );
  805. slapi_ch_free( (void **)&(k3.dptr) );
  806. idl_free( tmp );
  807. idl_free( tmp2 );
  808. idl_free( idl );
  809. idl_unlock_list(a->ai_idl,key);
  810. return( rc );
  811. case 0: /* id inserted not at start - how? */
  812. case 2: /* id already there - how? */
  813. /*
  814. * if either of these cases happen, this
  815. * index entry must have been corrupt when
  816. * we started this insert. what can we do
  817. * aside from log a warning?
  818. */
  819. LDAPDebug( LDAP_DEBUG_ANY,
  820. "not expecting return %d from idl_insert_maxids of id %lu in block with key (%s)\n",
  821. rc, (u_long)tmp->b_ids[tmp->b_nids-1], k3.dptr );
  822. LDAPDebug( LDAP_DEBUG_ANY,
  823. "likely database corruption\n", 0, 0, 0 );
  824. /* FALL */
  825. case 3: /* block is full */
  826. /*
  827. * if this case happens, we fall back to
  828. * splitting the original block.
  829. * This is not an error condition. So set
  830. * rc = 0 to continue. Otherwise, it will break
  831. * from the case statement and return rc=3,
  832. * which is not correct.
  833. */
  834. rc = 0;
  835. idl_free( tmp2 );
  836. break;
  837. }
  838. if ( rc != 0 ) {
  839. break; /* return error in rc */
  840. }
  841. }
  842. /*
  843. * must split the block, write both new blocks + update
  844. * and write the indirect header block.
  845. */
  846. /* count how many indirect blocks */
  847. for ( j = 0; idl->b_ids[j] != NOID; j++ )
  848. ; /* NULL */
  849. /* check it against all-id thresholed */
  850. if ( j + 1 > a->ai_idl->idl_maxindirect ) {
  851. /*
  852. * we've passed the all-id threshold, meaning
  853. * that this set of blocks should be replaced
  854. * by a single "all-id" block. our job: delete
  855. * all the indirect blocks, and replace the header
  856. * block by an all-id block.
  857. */
  858. /* delete all indirect blocks */
  859. for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
  860. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr,
  861. (u_long)idl->b_ids[j] );
  862. k2.dptr = kstr;
  863. k2.dsize = strlen( kstr ) + 1;
  864. rc = db->del( db, txn, &k2, 0 );
  865. if ( rc != 0 ) {
  866. if (rc == DB_RUNRECOVERY) {
  867. ldbm_nasty("",73,rc);
  868. }
  869. break;
  870. }
  871. }
  872. /* store allid block in place of header block */
  873. if ( 0 == rc ) {
  874. idl_free( idl );
  875. idl = idl_allids( be );
  876. rc = idl_store( be, db, key, idl, txn );
  877. if (NULL != disposition) {
  878. *disposition = IDL_INSERT_NOW_ALLIDS;
  879. }
  880. }
  881. slapi_ch_free( (void **)&(k2.dptr) );
  882. slapi_ch_free( (void **)&(k3.dptr) );
  883. idl_free( idl );
  884. idl_free( tmp );
  885. idl_unlock_list(a->ai_idl,key);
  886. return( rc );
  887. }
  888. idl_split_block( tmp, id, &tmp2, &tmp3 );
  889. idl_free( tmp );
  890. /* create a new updated indirect header block */
  891. tmp = idl_alloc( idl->b_nmax + 1 );
  892. tmp->b_nids = INDBLOCK;
  893. /* everything up to the split block */
  894. SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
  895. i * sizeof(ID) );
  896. /* the two new blocks */
  897. tmp->b_ids[i] = tmp2->b_ids[0];
  898. tmp->b_ids[i + 1] = tmp3->b_ids[0];
  899. /* everything after the split block */
  900. SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
  901. &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
  902. /* store the header block */
  903. rc = idl_store( be, db, key, tmp, txn );
  904. if ( rc != 0 ) {
  905. idl_free( tmp2 );
  906. idl_free( tmp3 );
  907. break;
  908. }
  909. /* store the first id block */
  910. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr,
  911. (u_long)tmp2->b_ids[0] );
  912. k2.dptr = kstr;
  913. k2.dsize = strlen( kstr ) + 1;
  914. rc = idl_store( be, db, &k2, tmp2, txn );
  915. if ( rc != 0 ) {
  916. idl_free( tmp2 );
  917. idl_free( tmp3 );
  918. break;
  919. }
  920. /* store the second id block */
  921. sprintf( kstr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr,
  922. (u_long)tmp3->b_ids[0] );
  923. k2.dptr = kstr;
  924. k2.dsize = strlen( kstr ) + 1;
  925. rc = idl_store( be, db, &k2, tmp3, txn );
  926. if ( rc != 0 ) {
  927. idl_free( tmp2 );
  928. idl_free( tmp3 );
  929. break;
  930. }
  931. idl_check_indirect (tmp, i, tmp2, tmp3,
  932. "idl_insert_key", "indirect split", key, id);
  933. idl_free( tmp2 );
  934. idl_free( tmp3 );
  935. break;
  936. }
  937. slapi_ch_free( (void **)&(k2.dptr) );
  938. slapi_ch_free( (void **)&(k3.dptr) );
  939. idl_free( tmp );
  940. idl_free( idl );
  941. idl_unlock_list(a->ai_idl,key);
  942. return( rc );
  943. }
  944. /* Store a complete IDL all in one go, there must not be an existing key with the same value */
  945. /* Routine used by merging import code */
  946. int idl_old_store_block(
  947. backend *be,
  948. DB *db,
  949. DBT *key,
  950. IDList *idl,
  951. DB_TXN *txn,
  952. struct attrinfo *a
  953. )
  954. {
  955. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  956. int ret = 0;
  957. idl_private *priv = a->ai_idl;
  958. if (0 == a->ai_idl->idl_maxids) {
  959. idl_init_maxids(li,a->ai_idl);
  960. }
  961. /* First, is it an ALLIDS block ? */
  962. if (ALLIDS(idl)) {
  963. /* If so, we can store it as-is */
  964. ret = idl_store(be,db,key,idl,txn);
  965. } else {
  966. /* Next, is it a block with so many IDs in it that it _should_ be an ALLIDS block ? */
  967. if (idl->b_nids > (ID)li->li_allidsthreshold) {
  968. /* If so, store an ALLIDS block */
  969. IDList *all = idl_allids(be);
  970. ret = idl_store(be,db,key,all,txn);
  971. idl_free(all);
  972. } else {
  973. /* Then , is it a block which is smaller than the size at which it needs splitting ? */
  974. if (idl->b_nids <= (ID)priv->idl_maxids) {
  975. /* If so, store as-is */
  976. ret = idl_store(be,db,key,idl,txn);
  977. } else {
  978. size_t number_of_ids = 0;
  979. size_t max_ids_in_block = 0;
  980. size_t number_of_cont_blks = 0;
  981. size_t i = 0;
  982. size_t number_of_ids_left = 0;
  983. IDList *master_block = NULL;
  984. size_t index = 0;
  985. DBT cont_key = {0};
  986. number_of_ids = idl->b_nids;
  987. max_ids_in_block = priv->idl_maxids;
  988. number_of_cont_blks = number_of_ids / max_ids_in_block;
  989. if (0 != number_of_ids % max_ids_in_block) {
  990. number_of_cont_blks++;
  991. }
  992. number_of_ids_left = number_of_ids;
  993. /* Block needs splitting into continuation blocks */
  994. /* We need to make up a master block and n continuation blocks */
  995. /* Alloc master block */
  996. master_block = idl_alloc(number_of_cont_blks + 1);
  997. if (NULL == master_block) {
  998. return -1;
  999. }
  1000. master_block->b_nids = INDBLOCK;
  1001. master_block->b_ids[number_of_cont_blks] = NOID;
  1002. /* Iterate over ids making the continuation blocks */
  1003. for (i = 0 ; i < number_of_cont_blks; i++) {
  1004. IDList *this_cont_block = NULL;
  1005. size_t size_of_this_block = 0;
  1006. ID lead_id = NOID;
  1007. size_t j = 0;
  1008. lead_id = idl->b_ids[index];
  1009. if (number_of_ids_left >= max_ids_in_block) {
  1010. size_of_this_block = max_ids_in_block;
  1011. } else {
  1012. size_of_this_block = number_of_ids_left;
  1013. }
  1014. this_cont_block = idl_alloc(size_of_this_block);
  1015. if (NULL == this_cont_block) {
  1016. return -1;
  1017. }
  1018. this_cont_block->b_nids = size_of_this_block;
  1019. /* Copy over the ids to the cont block we're making */
  1020. for (j = 0; j < size_of_this_block; j++) {
  1021. this_cont_block->b_ids[j] = idl->b_ids[index + j];
  1022. }
  1023. /* Make the continuation key */
  1024. make_cont_key(&cont_key,key,lead_id);
  1025. /* Now store the continuation block */
  1026. ret = idl_store(be,db,&cont_key,this_cont_block,txn);
  1027. idl_free(this_cont_block);
  1028. slapi_ch_free(&(cont_key.data));
  1029. if ( ret != 0 && ret != DB_LOCK_DEADLOCK )
  1030. {
  1031. LDAPDebug( LDAP_DEBUG_ANY, "idl_store_block(%s) 1 BAD %d %s\n",key->data, ret, dblayer_strerror( ret ));
  1032. return ret;
  1033. }
  1034. /* Put the lead ID number in the header block */
  1035. master_block->b_ids[i] = lead_id;
  1036. /* Make our loop invariants correct */
  1037. number_of_ids_left -= size_of_this_block;
  1038. index += size_of_this_block;
  1039. }
  1040. PR_ASSERT(0 == number_of_ids_left);
  1041. /* Now store the master block */
  1042. ret = idl_store(be,db,key,master_block,txn);
  1043. /* And free it */
  1044. idl_free(master_block);
  1045. }
  1046. }
  1047. }
  1048. return ret;
  1049. }
  1050. /*
  1051. * idl_insert - insert an id into an id list.
  1052. */
  1053. void idl_insert(IDList **idl, ID id)
  1054. {
  1055. ID i, j;
  1056. NIDS nids;
  1057. if ((*idl) == NULL) {
  1058. (*idl) = idl_alloc(1);
  1059. idl_append((*idl), id);
  1060. return;
  1061. }
  1062. if (ALLIDS(*idl)) {
  1063. return;
  1064. }
  1065. i = nids = (*idl)->b_nids;
  1066. if (nids > 0) {
  1067. /* optimize for a simple append */
  1068. if (id == (*idl)->b_ids[nids-1]) {
  1069. return;
  1070. } else if (id > (*idl)->b_ids[nids-1]) {
  1071. if (nids < (*idl)->b_nmax) {
  1072. (*idl)->b_ids[nids] = id;
  1073. (*idl)->b_nids++;
  1074. return;
  1075. }
  1076. i = nids;
  1077. } else if (id < (*idl)->b_ids[0]) {
  1078. /* prepend */
  1079. i = 0;
  1080. } else {
  1081. int lo = 0;
  1082. int hi = (*idl)->b_nids - 1;
  1083. int mid = 0;
  1084. ID *ids = (*idl)->b_ids;
  1085. if (0 != (*idl)->b_nids) {
  1086. while (lo <= hi) {
  1087. mid = (hi + lo) >> 1;
  1088. if (ids[mid] > id) {
  1089. hi = mid - 1;
  1090. } else {
  1091. if (ids[mid] < id) {
  1092. lo = mid + 1;
  1093. } else {
  1094. /* Found it ! */
  1095. return;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. i = lo;
  1101. }
  1102. }
  1103. /* do we need to make room for it? */
  1104. if ( (*idl)->b_nids == (*idl)->b_nmax ) {
  1105. (*idl)->b_nmax *= 2;
  1106. (*idl) = (IDList *) slapi_ch_realloc( (char *) (*idl),
  1107. ((*idl)->b_nmax + 2) * sizeof(ID) );
  1108. }
  1109. /* make a slot for the new id */
  1110. for ( j = (*idl)->b_nids; j != i; j-- ) {
  1111. (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
  1112. }
  1113. (*idl)->b_ids[i] = id;
  1114. (*idl)->b_nids++;
  1115. memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
  1116. ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
  1117. return;
  1118. }
  1119. /*
  1120. * idl_insert_maxids - insert an id into an id list.
  1121. * returns 0 id inserted
  1122. * 1 id inserted, first id in block has changed
  1123. * 2 id not inserted, already there
  1124. * 3 id not inserted, block must be split
  1125. */
  1126. static int
  1127. idl_insert_maxids( IDList **idl, ID id, int maxids )
  1128. {
  1129. ID i, j;
  1130. NIDS nids;
  1131. if ( ALLIDS( *idl ) ) {
  1132. return( 2 ); /* already there */
  1133. }
  1134. nids = (*idl)->b_nids;
  1135. if (nids > 0) {
  1136. /* optimize for a simple append */
  1137. if (id == (*idl)->b_ids[nids-1]) {
  1138. return (2);
  1139. } else if (id > (*idl)->b_ids[nids-1]) {
  1140. if (nids < (*idl)->b_nmax) {
  1141. (*idl)->b_ids[nids] = id;
  1142. (*idl)->b_nids++;
  1143. return 0;
  1144. }
  1145. i = nids;
  1146. } else if (idl_tune & IDL_TUNE_BSEARCH) {
  1147. int lo = 0;
  1148. int hi = (*idl)->b_nids - 1;
  1149. int mid = 0;
  1150. ID *ids = (*idl)->b_ids;
  1151. if (0 != (*idl)->b_nids) {
  1152. while (lo <= hi) {
  1153. mid = (hi + lo) >> 1;
  1154. if (ids[mid] > id) {
  1155. hi = mid - 1;
  1156. } else {
  1157. if (ids[mid] < id) {
  1158. lo = mid + 1;
  1159. } else {
  1160. /* Found it ! */
  1161. return(2);
  1162. }
  1163. }
  1164. }
  1165. }
  1166. i = lo;
  1167. } else {
  1168. /* is it already there? linear search */
  1169. for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
  1170. ; /* NULL */
  1171. }
  1172. if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
  1173. return( 2 ); /* already there */
  1174. }
  1175. }
  1176. }
  1177. /* do we need to make room for it? */
  1178. if ( (*idl)->b_nids == (*idl)->b_nmax ) {
  1179. /* make room or indicate block needs splitting */
  1180. if ( (*idl)->b_nmax == (ID) maxids ) {
  1181. return( 3 ); /* block needs splitting */
  1182. }
  1183. if (idl_tune & IDL_TUNE_NOPAD) {
  1184. (*idl)->b_nmax++;
  1185. } else {
  1186. (*idl)->b_nmax *= 2;
  1187. }
  1188. if ( (*idl)->b_nmax > (ID)maxids ) {
  1189. (*idl)->b_nmax = maxids;
  1190. }
  1191. *idl = (IDList *) slapi_ch_realloc( (char *) *idl,
  1192. ((*idl)->b_nmax + 2) * sizeof(ID) );
  1193. }
  1194. /* make a slot for the new id */
  1195. for ( j = (*idl)->b_nids; j != i; j-- ) {
  1196. (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
  1197. }
  1198. (*idl)->b_ids[i] = id;
  1199. (*idl)->b_nids++;
  1200. (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
  1201. ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
  1202. return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
  1203. }
  1204. /*
  1205. * idl_delete_key - delete an id from the index entry identified by key
  1206. * returns 0 id was deleted
  1207. * -666 no such index entry or id in index entry
  1208. * other an error code from db
  1209. */
  1210. int
  1211. idl_old_delete_key(
  1212. backend *be,
  1213. DB *db,
  1214. DBT *key,
  1215. ID id,
  1216. DB_TXN *txn,
  1217. struct attrinfo *a
  1218. )
  1219. {
  1220. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  1221. int i, j, rc;
  1222. char *msg;
  1223. IDList *idl, *didl;
  1224. DBT contkey = {0};
  1225. LDAPDebug( LDAP_DEBUG_TRACE, "=> idl_delete_key(%s,%lu)\n",
  1226. key->dptr, (u_long)id, 0 );
  1227. idl_Wlock_list(a->ai_idl,key);
  1228. if ( (idl = idl_fetch_one( li, db, key, txn, &rc )) == NULL ) {
  1229. idl_unlock_list(a->ai_idl,key);
  1230. if ( rc != 0 && rc != DB_NOTFOUND && rc != DB_LOCK_DEADLOCK )
  1231. {
  1232. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 0 BAD %d %s\n",
  1233. key->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1234. }
  1235. if ( 0 == rc || DB_NOTFOUND == rc ) rc = -666;
  1236. LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_delete_key(%s,%lu) %d !idl_fetch_one\n",
  1237. key->dptr, (u_long)id, rc );
  1238. return rc;
  1239. }
  1240. /* regular block */
  1241. if ( ! INDIRECT_BLOCK( idl ) ) {
  1242. switch ( idl_delete( &idl, id ) ) {
  1243. case 0: /* id deleted, store the updated block */
  1244. case 1: /* first id changed - ok in direct block */
  1245. rc = idl_store( be, db, key, idl, txn );
  1246. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1247. {
  1248. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 1 BAD %d %s\n",
  1249. key->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1250. }
  1251. break;
  1252. case 2: /* id deleted, block empty - delete it */
  1253. rc = db->del( db, txn, key, 0 );
  1254. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1255. {
  1256. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 2 BAD %d %s\n",
  1257. key->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1258. if (rc == DB_RUNRECOVERY) {
  1259. ldbm_nasty("",74,rc);
  1260. }
  1261. }
  1262. break;
  1263. case 3: /* not there - previously deleted */
  1264. case 4: /* all ids block */
  1265. rc = 0;
  1266. break;
  1267. default:
  1268. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 3 BAD idl_delete\n",
  1269. key->dptr, 0, 0 );
  1270. break;
  1271. }
  1272. idl_free( idl );
  1273. idl_unlock_list(a->ai_idl,key);
  1274. LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_delete_key(%s,%lu) %d (not indirect)\n",
  1275. key->dptr, (u_long)id, rc );
  1276. return( rc );
  1277. }
  1278. /*
  1279. * this is an indirect block that points to other blocks. we
  1280. * need to read the block containing the id to delete, delete
  1281. * the id, and store the changed block. if the first id in the
  1282. * block changes, or the block becomes empty, we need to rewrite
  1283. * the header block too.
  1284. */
  1285. /* select the block the id is in */
  1286. for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ ) {
  1287. ; /* NULL */
  1288. }
  1289. /* id smaller than smallest id - not there */
  1290. if ( i == 0 && id < idl->b_ids[i] ) {
  1291. idl_free( idl );
  1292. idl_unlock_list(a->ai_idl,key);
  1293. LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_delete_key(%s,%lu) -666 (id not found)\n",
  1294. key->dptr, (u_long)id, 0 );
  1295. return( -666 );
  1296. }
  1297. if ( id != idl->b_ids[i] ) {
  1298. i--;
  1299. }
  1300. /* get the block to delete from */
  1301. make_cont_key( &contkey, key, idl->b_ids[i] );
  1302. if ( (didl = idl_fetch_one( li, db, &contkey, txn, &rc )) == NULL ) {
  1303. idl_free( idl );
  1304. idl_unlock_list(a->ai_idl,key);
  1305. if ( rc != DB_LOCK_DEADLOCK )
  1306. {
  1307. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 5 BAD %d %s\n",
  1308. contkey.dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1309. }
  1310. LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_delete_key(%s,%lu) %d idl_fetch_one(contkey)\n",
  1311. contkey.dptr, (u_long)id, rc );
  1312. slapi_ch_free( (void **)&(contkey.dptr) );
  1313. return( rc );
  1314. }
  1315. rc = 0;
  1316. switch ( idl_delete( &didl, id ) ) {
  1317. case 0: /* id deleted - rewrite block */
  1318. if ( (rc = idl_store( be, db, &contkey, didl, txn )) != 0 ) {
  1319. if ( rc != DB_LOCK_DEADLOCK )
  1320. {
  1321. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) BAD %d %s\n",
  1322. contkey.dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1323. }
  1324. }
  1325. if (0 != rc) {
  1326. idl_check_indirect( idl, i, didl, NULL, "idl_delete_key", "0", key, id );
  1327. }
  1328. break;
  1329. case 1: /* id deleted, first id changed, - write hdr, block */
  1330. rc = idl_change_first( be, db, key, idl, i, &contkey, didl, txn );
  1331. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1332. {
  1333. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 7 BAD %d %s\n",
  1334. contkey.dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1335. }
  1336. if (0 != rc) {
  1337. idl_check_indirect( idl, i, didl, NULL, "idl_delete_key", "1", key, id );
  1338. }
  1339. break;
  1340. case 2: /* id deleted, block empty - write hdr, del block */
  1341. for ( j = i; idl->b_ids[j] != NOID; j++ ) {
  1342. idl->b_ids[j] = idl->b_ids[j+1];
  1343. }
  1344. if ( idl->b_ids[0] != NOID ) { /* Write the header, first: */
  1345. rc = idl_store( be, db, key, idl, txn );
  1346. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1347. {
  1348. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key: idl_store(%s) BAD %d %s\n",
  1349. key->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1350. }
  1351. } else { /* This index is entirely empty. Delete the header: */
  1352. rc = db->del( db, txn, key, 0 );
  1353. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1354. {
  1355. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key: db->del(%s) BAD %d %s\n",
  1356. key->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1357. if (rc == DB_RUNRECOVERY) {
  1358. ldbm_nasty("",75,rc);
  1359. }
  1360. }
  1361. }
  1362. if ( rc == 0 ) { /* Delete the indirect block: */
  1363. rc = db->del( db, txn, &contkey, 0 );
  1364. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1365. {
  1366. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key: db->del(%s) BAD %d %s\n",
  1367. contkey.dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1368. if (rc == DB_RUNRECOVERY) {
  1369. ldbm_nasty("",76,rc);
  1370. }
  1371. }
  1372. }
  1373. break;
  1374. case 3: /* id not found - previously deleted */
  1375. rc = 0;
  1376. idl_check_indirect( idl, i, didl, NULL, "idl_delete_key", "3", key, id );
  1377. break;
  1378. case 4: /* all ids block - should not happen */
  1379. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key: cont block (%s) is allids\n",
  1380. contkey.dptr, 0, 0 );
  1381. rc = 0;
  1382. break;
  1383. }
  1384. idl_free( idl );
  1385. idl_free( didl );
  1386. slapi_ch_free( (void **)&(contkey.dptr) );
  1387. idl_unlock_list(a->ai_idl,key);
  1388. if ( rc != 0 && rc != DB_LOCK_DEADLOCK )
  1389. {
  1390. LDAPDebug( LDAP_DEBUG_ANY, "idl_delete_key(%s) 9 BAD %d %s\n",
  1391. key->dptr, rc, (msg = dblayer_strerror( rc )) ? msg : "" );
  1392. }
  1393. LDAPDebug( LDAP_DEBUG_TRACE, "<= idl_delete_key(%s,%lu) %d (indirect)\n",
  1394. key->dptr, (u_long)id, rc );
  1395. return( rc );
  1396. }
  1397. /*
  1398. * idl_delete - delete an id from an id list.
  1399. * returns 0 id deleted
  1400. * 1 id deleted, first id in block has changed
  1401. * 2 id deleted, block is empty
  1402. * 3 id not there
  1403. * 4 cannot delete from allids block
  1404. */
  1405. int
  1406. idl_delete( IDList **idl, ID id )
  1407. {
  1408. ID i, delpos;
  1409. if ( ALLIDS( *idl ) ) {
  1410. return( 4 ); /* cannot delete from allids block */
  1411. }
  1412. /* find the id to delete */
  1413. for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
  1414. ; /* NULL */
  1415. }
  1416. if ( i == (*idl)->b_nids || (*idl)->b_ids[i] != id ) {
  1417. return( 3 ); /* id not there */
  1418. }
  1419. if ( --((*idl)->b_nids) == 0 ) {
  1420. return( 2 ); /* id deleted, block empty */
  1421. }
  1422. /* delete it */
  1423. delpos = i;
  1424. for ( ; i < (*idl)->b_nids; i++ ) {
  1425. (*idl)->b_ids[i] = (*idl)->b_ids[i+1];
  1426. }
  1427. return( delpos == 0 ? 1 : 0 ); /* first id changed : id deleted */
  1428. }
  1429. static void
  1430. make_cont_key( DBT *contkey, DBT *key, ID id )
  1431. {
  1432. contkey->dptr = (char *) slapi_ch_malloc( key->dsize + 20 );
  1433. sprintf( contkey->dptr, "%c%s%lu", CONT_PREFIX, (char *)key->dptr, (u_long)id );
  1434. contkey->dsize = strlen( contkey->dptr ) + 1;
  1435. }