import-threads.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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. /*
  42. * the threads that make up an import:
  43. * producer (1)
  44. * foreman (1)
  45. * worker (N: 1 for each index)
  46. *
  47. * a wire import (aka "fast replica" import) won't have a producer thread.
  48. */
  49. #include "back-ldbm.h"
  50. #include "vlv_srch.h"
  51. #include "import.h"
  52. #ifdef XP_WIN32
  53. #define STDIN_FILENO 0
  54. #endif
  55. static void import_wait_for_space_in_fifo(ImportJob *job, size_t new_esize);
  56. static struct backentry *import_make_backentry(Slapi_Entry *e, ID id)
  57. {
  58. struct backentry *ep = backentry_alloc();
  59. if (NULL != ep) {
  60. ep->ep_entry = e;
  61. ep->ep_id = id;
  62. }
  63. return ep;
  64. }
  65. static void import_decref_entry(struct backentry *ep)
  66. {
  67. PR_AtomicDecrement(&(ep->ep_refcnt));
  68. PR_ASSERT(ep->ep_refcnt >= 0);
  69. }
  70. /* generate uniqueid if requested */
  71. static void import_generate_uniqueid(ImportJob *job, Slapi_Entry *e)
  72. {
  73. const char *uniqueid = slapi_entry_get_uniqueid(e);
  74. int rc;
  75. if (!uniqueid && (job->uuid_gen_type != SLAPI_UNIQUEID_GENERATE_NONE)) {
  76. char *newuniqueid;
  77. /* generate id based on dn */
  78. if (job->uuid_gen_type == SLAPI_UNIQUEID_GENERATE_NAME_BASED) {
  79. char *dn = slapi_entry_get_dn(e);
  80. rc = slapi_uniqueIDGenerateFromNameString(&newuniqueid,
  81. job->uuid_namespace, dn, strlen(dn));
  82. } else {
  83. /* time based */
  84. rc = slapi_uniqueIDGenerateString(&newuniqueid);
  85. }
  86. if (rc == UID_SUCCESS) {
  87. slapi_entry_set_uniqueid (e, newuniqueid);
  88. } else {
  89. char ebuf[BUFSIZ];
  90. LDAPDebug( LDAP_DEBUG_ANY,
  91. "import_generate_uniqueid: failed to generate "
  92. "uniqueid for %s; error=%d.\n",
  93. escape_string(slapi_entry_get_dn_const(e), ebuf), rc, 0 );
  94. }
  95. }
  96. }
  97. /********** BETTER LDIF PARSER **********/
  98. /* like the function in libldif, except this one doesn't need to use
  99. * FILE (which breaks on various platforms for >4G files or large numbers
  100. * of open files)
  101. */
  102. #define LDIF_BUFFER_SIZE 8192
  103. typedef struct {
  104. char *b; /* buffer */
  105. size_t size; /* how full the buffer is */
  106. size_t offset; /* where the current entry starts */
  107. } ldif_context;
  108. static void import_init_ldif(ldif_context *c)
  109. {
  110. c->size = c->offset = 0;
  111. c->b = NULL;
  112. }
  113. static void import_free_ldif(ldif_context *c)
  114. {
  115. if (c->b)
  116. FREE(c->b);
  117. import_init_ldif(c);
  118. }
  119. static char *import_get_entry(ldif_context *c, int fd, int *lineno)
  120. {
  121. int ret;
  122. int done = 0, got_lf = 0;
  123. size_t bufSize = 0, bufOffset = 0, i;
  124. char *buf = NULL;
  125. while (!done) {
  126. /* If there's no data in the buffer, get some */
  127. if ((c->size == 0) || (c->offset == c->size)) {
  128. /* Do we even have a buffer ? */
  129. if (! c->b) {
  130. c->b = slapi_ch_malloc(LDIF_BUFFER_SIZE);
  131. if (! c->b)
  132. return NULL;
  133. }
  134. ret = read(fd, c->b, LDIF_BUFFER_SIZE);
  135. if (ret < 0) {
  136. /* Must be error */
  137. goto error;
  138. } else if (ret == 0) {
  139. /* eof */
  140. if (buf) {
  141. /* last entry */
  142. buf[bufOffset] = 0;
  143. return buf;
  144. }
  145. return NULL;
  146. } else {
  147. /* read completed OK */
  148. c->size = ret;
  149. c->offset = 0;
  150. }
  151. }
  152. /* skip blank lines at start of entry */
  153. if (bufOffset == 0) {
  154. size_t n;
  155. char *p;
  156. for (n = c->offset, p = c->b + n; n < c->size; n++, p++) {
  157. if (!(*p == '\r' || *p == '\n' || *p == ' '|| *p == '\t'))
  158. break;
  159. }
  160. c->offset = n;
  161. if (c->offset == c->size) continue;
  162. }
  163. i = c->offset;
  164. while (!done && (i < c->size)) {
  165. /* scan forward in the buffer, looking for the end of the entry */
  166. while ((i < c->size) && (c->b[i] != '\n'))
  167. i++;
  168. if ((i < c->size) && (c->b[i] == '\n')) {
  169. if (got_lf && ((i == 0) || ((i == 1) && (c->b[0] == '\r')))) {
  170. /* saw an lf at the end of the last buffer */
  171. i++, (*lineno)++;
  172. done = 1;
  173. got_lf = 0;
  174. break;
  175. }
  176. got_lf = 0;
  177. (*lineno)++;
  178. /* is this the end? (need another linefeed) */
  179. if (++i < c->size) {
  180. if (c->b[i] == '\n') {
  181. /* gotcha! */
  182. i++, (*lineno)++;
  183. done = 1;
  184. } else if (c->b[i] == '\r') {
  185. if (++i < c->size) {
  186. if (c->b[i] == '\n') {
  187. /* gotcha! (nt) */
  188. i++, (*lineno)++;
  189. done = 1;
  190. }
  191. } else {
  192. got_lf = 1;
  193. }
  194. }
  195. } else {
  196. /* lf at the very end of the buffer */
  197. got_lf = 1;
  198. }
  199. }
  200. }
  201. /* copy what we did so far into the output buffer */
  202. /* (first, make sure the output buffer is large enough) */
  203. if (bufSize - bufOffset < i - c->offset + 1) {
  204. char *newbuf = NULL;
  205. size_t newsize = (buf ? bufSize*2 : LDIF_BUFFER_SIZE);
  206. newbuf = slapi_ch_malloc(newsize);
  207. if (! newbuf)
  208. goto error;
  209. /* copy over the old data (if there was any) */
  210. if (buf) {
  211. memmove(newbuf, buf, bufOffset);
  212. slapi_ch_free((void **)&buf);
  213. }
  214. buf = newbuf;
  215. bufSize = newsize;
  216. }
  217. memmove(buf + bufOffset, c->b + c->offset, i - c->offset);
  218. bufOffset += (i - c->offset);
  219. c->offset = i;
  220. }
  221. /* add terminating NUL char */
  222. buf[bufOffset] = 0;
  223. return buf;
  224. error:
  225. if (buf)
  226. slapi_ch_free((void **)&buf);
  227. return NULL;
  228. }
  229. /********** THREADS **********/
  230. /*
  231. * Description:
  232. * 1) return the ldif version #
  233. * 2) replace "version: 1" with "#ersion: 1"
  234. * to pretend like a comment for the str2entry
  235. */
  236. static int
  237. import_get_version(char *str)
  238. {
  239. char *s;
  240. char *type;
  241. char *valuecharptr;
  242. char *mystr, *ms;
  243. int offset;
  244. #if defined(USE_OPENLDAP)
  245. ber_len_t valuelen;
  246. #else
  247. int valuelen;
  248. #endif
  249. int my_version = 0;
  250. int retmalloc = 0;
  251. if ((s = strstr(str, "version:")) == NULL)
  252. return 0;
  253. offset = s - str;
  254. mystr = ms = slapi_ch_strdup(str);
  255. while ( (s = ldif_getline( &ms )) != NULL ) {
  256. if ( (retmalloc = ldif_parse_line( s, &type, &valuecharptr, &valuelen )) >= 0 ) {
  257. if (!strcasecmp(type, "version")) {
  258. my_version = atoi(valuecharptr);
  259. *(str + offset) = '#';
  260. /* the memory below was not allocated by the slapi_ch_ functions */
  261. if (retmalloc) slapi_ch_free((void **) &valuecharptr);
  262. break;
  263. }
  264. }
  265. /* the memory below was not allocated by the slapi_ch_ functions */
  266. if (retmalloc) slapi_ch_free((void **) &valuecharptr);
  267. }
  268. slapi_ch_free((void **)&mystr);
  269. return my_version;
  270. }
  271. /*
  272. * add CreatorsName, ModifiersName, CreateTimestamp, ModifyTimestamp to entry
  273. */
  274. static void
  275. import_add_created_attrs(Slapi_Entry *e)
  276. {
  277. char buf[20];
  278. struct berval bv;
  279. struct berval *bvals[2];
  280. time_t curtime;
  281. struct tm ltm;
  282. bvals[0] = &bv;
  283. bvals[1] = NULL;
  284. bv.bv_val = "";
  285. bv.bv_len = 0;
  286. slapi_entry_attr_replace(e, "creatorsname", bvals);
  287. slapi_entry_attr_replace(e, "modifiersname", bvals);
  288. curtime = current_time();
  289. #ifdef _WIN32
  290. {
  291. struct tm *pt;
  292. pt = gmtime(&curtime);
  293. memcpy(&ltm, pt, sizeof(struct tm));
  294. }
  295. #else
  296. gmtime_r(&curtime, &ltm);
  297. #endif
  298. strftime(buf, sizeof(buf), "%Y%m%d%H%M%SZ", &ltm);
  299. bv.bv_val = buf;
  300. bv.bv_len = strlen(bv.bv_val);
  301. slapi_entry_attr_replace(e, "createtimestamp", bvals);
  302. bv.bv_val = buf;
  303. bv.bv_len = strlen(bv.bv_val);
  304. slapi_entry_attr_replace(e, "modifytimestamp", bvals);
  305. }
  306. /* producer thread:
  307. * read through the given file list, parsing entries (str2entry), assigning
  308. * them IDs and queueing them on the entry FIFO. other threads will do
  309. * the indexing.
  310. */
  311. void import_producer(void *param)
  312. {
  313. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  314. ImportJob *job = info->job;
  315. ID id = job->first_ID, id_filestart = id;
  316. Slapi_Entry *e = NULL;
  317. struct backentry *ep = NULL, *old_ep = NULL;
  318. ldbm_instance *inst = job->inst;
  319. PRIntervalTime sleeptime;
  320. char *estr = NULL;
  321. int str2entry_flags =
  322. SLAPI_STR2ENTRY_TOMBSTONE_CHECK |
  323. SLAPI_STR2ENTRY_REMOVEDUPVALS |
  324. SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES |
  325. SLAPI_STR2ENTRY_ADDRDNVALS |
  326. SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF;
  327. int finished = 0;
  328. int detected_eof = 0;
  329. int fd, curr_file, curr_lineno;
  330. char *curr_filename = NULL;
  331. int idx;
  332. ldif_context c;
  333. int my_version = 0;
  334. size_t newesize = 0;
  335. PR_ASSERT(info != NULL);
  336. PR_ASSERT(inst != NULL);
  337. if ( job->flags & FLAG_ABORT ) {
  338. goto error;
  339. }
  340. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  341. /* pause until we're told to run */
  342. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)) {
  343. info->state = WAITING;
  344. DS_Sleep(sleeptime);
  345. }
  346. info->state = RUNNING;
  347. import_init_ldif(&c);
  348. /* jumpstart by opening the first file */
  349. curr_file = 0;
  350. fd = -1;
  351. detected_eof = finished = 0;
  352. /* we loop around reading the input files and processing each entry
  353. * as we read it.
  354. */
  355. while (! finished) {
  356. Slapi_Attr *attr = NULL;
  357. int flags = 0;
  358. int prev_lineno = 0;
  359. int lines_in_entry = 0;
  360. if (job->flags & FLAG_ABORT) {
  361. goto error;
  362. }
  363. /* move on to next file? */
  364. if (detected_eof) {
  365. /* check if the file can still be read, whine if so... */
  366. if (read(fd, (void *)&idx, 1) > 0) {
  367. import_log_notice(job, "WARNING: Unexpected end of file found "
  368. "at line %d of file \"%s\"", curr_lineno,
  369. curr_filename);
  370. }
  371. if (fd == STDIN_FILENO) {
  372. import_log_notice(job, "Finished scanning file stdin (%lu "
  373. "entries)", (u_long)(id-id_filestart));
  374. } else {
  375. import_log_notice(job, "Finished scanning file \"%s\" (%lu "
  376. "entries)", curr_filename, (u_long)(id-id_filestart));
  377. }
  378. close(fd);
  379. fd = -1;
  380. detected_eof = 0;
  381. id_filestart = id;
  382. curr_file++;
  383. if (job->task) {
  384. job->task->task_progress++;
  385. slapi_task_status_changed(job->task);
  386. }
  387. if (job->input_filenames[curr_file] == NULL) {
  388. /* done! */
  389. finished = 1;
  390. break;
  391. }
  392. }
  393. /* separate from above, because this is also triggered when we
  394. * start (to open the first file)
  395. */
  396. if (fd < 0) {
  397. curr_lineno = 0;
  398. curr_filename = job->input_filenames[curr_file];
  399. if (strcmp(curr_filename, "-") == 0) {
  400. fd = STDIN_FILENO;
  401. } else {
  402. int o_flag = O_RDONLY;
  403. #ifdef XP_WIN32
  404. /* 613041 Somehow the windows low level io lose "\n"
  405. at a very particular situation using O_TEXT mode read.
  406. I think it is a windows bug for O_TEXT mode read.
  407. Use O_BINARY instead, which honestly returns chars
  408. without any translation.
  409. */
  410. o_flag |= O_BINARY;
  411. #endif
  412. fd = dblayer_open_huge_file(curr_filename, o_flag, 0);
  413. }
  414. if (fd < 0) {
  415. import_log_notice(job, "Could not open LDIF file \"%s\", errno %d (%s)",
  416. curr_filename, errno, slapd_system_strerror(errno));
  417. goto error;
  418. }
  419. if (fd == STDIN_FILENO) {
  420. import_log_notice(job, "Processing file stdin");
  421. } else {
  422. import_log_notice(job, "Processing file \"%s\"", curr_filename);
  423. }
  424. }
  425. if (job->flags & FLAG_ABORT) {
  426. goto error;
  427. }
  428. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)){
  429. info->state = WAITING;
  430. DS_Sleep(sleeptime);
  431. }
  432. info->state = RUNNING;
  433. prev_lineno = curr_lineno;
  434. estr = import_get_entry(&c, fd, &curr_lineno);
  435. lines_in_entry = curr_lineno - prev_lineno;
  436. if (!estr) {
  437. /* error reading entry, or end of file */
  438. detected_eof = 1;
  439. continue;
  440. }
  441. if (0 == my_version && strstr(estr, "version:")) {
  442. my_version = import_get_version(estr);
  443. str2entry_flags |= SLAPI_STR2ENTRY_INCLUDE_VERSION_STR;
  444. }
  445. /* If there are more than so many lines in the entry, we tell
  446. * str2entry to optimize for a large entry.
  447. */
  448. if (lines_in_entry > STR2ENTRY_ATTRIBUTE_PRESENCE_CHECK_THRESHOLD) {
  449. flags = str2entry_flags | SLAPI_STR2ENTRY_BIGENTRY;
  450. } else {
  451. flags = str2entry_flags;
  452. }
  453. e = slapi_str2entry(estr, flags);
  454. FREE(estr);
  455. if (! e) {
  456. if (!(str2entry_flags & SLAPI_STR2ENTRY_INCLUDE_VERSION_STR))
  457. import_log_notice(job, "WARNING: skipping bad LDIF entry "
  458. "ending line %d of file \"%s\"", curr_lineno,
  459. curr_filename);
  460. continue;
  461. }
  462. if (0 == my_version) {
  463. /* after the first entry version string won't be given */
  464. my_version = -1;
  465. }
  466. if (! import_entry_belongs_here(e, inst->inst_be)) {
  467. /* silently skip */
  468. if (e) {
  469. job->not_here_skipped++;
  470. slapi_entry_free(e);
  471. }
  472. continue;
  473. }
  474. if (slapi_entry_schema_check(NULL, e) != 0) {
  475. char ebuf[BUFSIZ];
  476. import_log_notice(job, "WARNING: skipping entry \"%s\" which "
  477. "violates schema, ending line %d of file "
  478. "\"%s\"", escape_string(slapi_entry_get_dn(e), ebuf),
  479. curr_lineno, curr_filename);
  480. if (e) {
  481. slapi_entry_free(e);
  482. }
  483. job->skipped++;
  484. continue;
  485. }
  486. /* Check attribute syntax */
  487. if (slapi_entry_syntax_check(NULL, e, 0) != 0)
  488. {
  489. char ebuf[BUFSIZ];
  490. import_log_notice(job, "WARNING: skipping entry \"%s\" which "
  491. "violates attribute syntax, ending line %d of "
  492. "file \"%s\"", escape_string(slapi_entry_get_dn(e), ebuf),
  493. curr_lineno, curr_filename);
  494. if (e) {
  495. slapi_entry_free(e);
  496. }
  497. job->skipped++;
  498. continue;
  499. }
  500. /* generate uniqueid if necessary */
  501. import_generate_uniqueid(job, e);
  502. if (g_get_global_lastmod()) {
  503. import_add_created_attrs(e);
  504. }
  505. ep = import_make_backentry(e, id);
  506. if (!ep)
  507. goto error;
  508. /* check for include/exclude subtree lists */
  509. if (! ldbm_back_ok_to_dump(backentry_get_ndn(ep),
  510. job->include_subtrees,
  511. job->exclude_subtrees)) {
  512. backentry_free(&ep);
  513. continue;
  514. }
  515. /* not sure what this does, but it looked like it could be
  516. * simplified. if it's broken, it's my fault. -robey
  517. */
  518. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  519. Slapi_Value **va = attr_get_present_values(attr);
  520. pw_encodevals( (Slapi_Value **)va ); /* jcm - cast away const */
  521. }
  522. if (job->flags & FLAG_ABORT) {
  523. goto error;
  524. }
  525. /* Now we have this new entry, all decoded
  526. * Next thing we need to do is:
  527. * (1) see if the appropriate fifo location contains an
  528. * entry which had been processed by the indexers.
  529. * If so, proceed.
  530. * If not, spin waiting for it to become free.
  531. * (2) free the old entry and store the new one there.
  532. * (3) Update the job progress indicators so the indexers
  533. * can use the new entry.
  534. */
  535. idx = id % job->fifo.size;
  536. old_ep = job->fifo.item[idx].entry;
  537. if (old_ep) {
  538. /* for the slot to be recycled, it needs to be already absorbed
  539. * by the foreman (id >= ready_ID), and all the workers need to
  540. * be finished with it (refcount = 0).
  541. */
  542. while (((old_ep->ep_refcnt > 0) ||
  543. (old_ep->ep_id >= job->ready_ID))
  544. && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  545. info->state = WAITING;
  546. DS_Sleep(sleeptime);
  547. }
  548. if (job->flags & FLAG_ABORT){
  549. goto error;
  550. }
  551. info->state = RUNNING;
  552. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  553. job->fifo.item[idx].entry = NULL;
  554. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  555. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  556. else
  557. job->fifo.c_bsize = 0;
  558. backentry_free(&old_ep);
  559. }
  560. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  561. if (newesize > job->fifo.bsize) { /* entry too big */
  562. char ebuf[BUFSIZ];
  563. import_log_notice(job, "WARNING: skipping entry \"%s\" "
  564. "ending line %d of file \"%s\"",
  565. escape_string(slapi_entry_get_dn(e), ebuf),
  566. curr_lineno, curr_filename);
  567. import_log_notice(job, "REASON: entry too large (%ld bytes) for "
  568. "the buffer size (%lu bytes)", newesize, job->fifo.bsize);
  569. backentry_free(&ep);
  570. job->skipped++;
  571. continue;
  572. }
  573. /* Now check if fifo has enough space for the new entry */
  574. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  575. import_wait_for_space_in_fifo( job, newesize );
  576. }
  577. /* We have enough space */
  578. job->fifo.item[idx].filename = curr_filename;
  579. job->fifo.item[idx].line = curr_lineno;
  580. job->fifo.item[idx].entry = ep;
  581. job->fifo.item[idx].bad = 0;
  582. job->fifo.item[idx].esize = newesize;
  583. /* Add the entry size to total fifo size */
  584. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  585. /* Update the job to show our progress */
  586. job->lead_ID = id;
  587. if ((id - info->first_ID) <= job->fifo.size) {
  588. job->trailing_ID = info->first_ID;
  589. } else {
  590. job->trailing_ID = id - job->fifo.size;
  591. }
  592. /* Update our progress meter too */
  593. info->last_ID_processed = id;
  594. id++;
  595. if (job->flags & FLAG_ABORT){
  596. goto error;
  597. }
  598. if (info->command == STOP) {
  599. if (fd >= 0)
  600. close(fd);
  601. finished = 1;
  602. }
  603. }
  604. import_free_ldif(&c);
  605. info->state = FINISHED;
  606. return;
  607. error:
  608. info->state = ABORTED;
  609. }
  610. /* producer thread for re-indexing:
  611. * read id2entry, parsing entries (str2entry) (needed???), assigning
  612. * them IDs (again, needed???) and queueing them on the entry FIFO.
  613. * other threads will do the indexing -- same as in import.
  614. */
  615. void index_producer(void *param)
  616. {
  617. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  618. ImportJob *job = info->job;
  619. ID id = job->first_ID;
  620. Slapi_Entry *e = NULL;
  621. struct backentry *ep = NULL, *old_ep = NULL;
  622. ldbm_instance *inst = job->inst;
  623. PRIntervalTime sleeptime;
  624. int finished = 0;
  625. int idx;
  626. /* vars for Berkeley DB */
  627. DB_ENV *env = NULL;
  628. DB *db = NULL;
  629. DBC *dbc = NULL;
  630. DBT key = {0};
  631. DBT data = {0};
  632. int db_rval = -1;
  633. backend *be = inst->inst_be;
  634. int isfirst = 1;
  635. int curr_entry = 0;
  636. size_t newesize = 0;
  637. PR_ASSERT(info != NULL);
  638. PR_ASSERT(inst != NULL);
  639. PR_ASSERT(be != NULL);
  640. if ( job->flags & FLAG_ABORT )
  641. goto error;
  642. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  643. /* pause until we're told to run */
  644. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)) {
  645. info->state = WAITING;
  646. DS_Sleep(sleeptime);
  647. }
  648. info->state = RUNNING;
  649. /* open id2entry with dedicated db env and db handler */
  650. if ( dblayer_get_aux_id2entry( be, &db, &env ) != 0 || db == NULL ||
  651. env == NULL) {
  652. LDAPDebug( LDAP_DEBUG_ANY, "Could not open id2entry\n", 0, 0, 0 );
  653. goto error;
  654. }
  655. /* get a cursor to we can walk over the table */
  656. db_rval = db->cursor(db, NULL, &dbc, 0);
  657. if ( 0 != db_rval ) {
  658. LDAPDebug( LDAP_DEBUG_ANY,
  659. "Failed to get cursor for reindexing\n", 0, 0, 0 );
  660. dblayer_release_id2entry(be, db);
  661. goto error;
  662. }
  663. /* we loop around reading the input files and processing each entry
  664. * as we read it.
  665. */
  666. finished = 0;
  667. while (!finished) {
  668. Slapi_Attr *attr = NULL;
  669. ID temp_id;
  670. if (job->flags & FLAG_ABORT) {
  671. goto error;
  672. }
  673. while ((info->command == PAUSE) && !(job->flags & FLAG_ABORT)){
  674. info->state = WAITING;
  675. DS_Sleep(sleeptime);
  676. }
  677. info->state = RUNNING;
  678. key.flags = DB_DBT_MALLOC;
  679. data.flags = DB_DBT_MALLOC;
  680. if (isfirst)
  681. {
  682. db_rval = dbc->c_get(dbc, &key, &data, DB_FIRST);
  683. isfirst = 0;
  684. }
  685. else
  686. {
  687. db_rval = dbc->c_get(dbc, &key, &data, DB_NEXT);
  688. }
  689. if (0 != db_rval) {
  690. if (DB_NOTFOUND != db_rval) {
  691. LDAPDebug(LDAP_DEBUG_ANY, "%s: Failed to read database, "
  692. "errno=%d (%s)\n", inst->inst_name, db_rval,
  693. dblayer_strerror(db_rval));
  694. if (job->task) {
  695. slapi_task_log_notice(job->task,
  696. "%s: Failed to read database, err %d (%s)",
  697. inst->inst_name, db_rval,
  698. dblayer_strerror(db_rval));
  699. }
  700. }
  701. break;
  702. }
  703. curr_entry++;
  704. temp_id = id_stored_to_internal((char *)key.data);
  705. slapi_ch_free(&(key.data));
  706. /* call post-entry plugin */
  707. plugin_call_entryfetch_plugins((char **) &data.dptr, &data.dsize);
  708. e = slapi_str2entry(data.data, 0);
  709. if ( NULL == e ) {
  710. if (job->task) {
  711. slapi_task_log_notice(job->task,
  712. "%s: WARNING: skipping badly formatted entry (id %lu)",
  713. inst->inst_name, (u_long)temp_id);
  714. }
  715. LDAPDebug(LDAP_DEBUG_ANY,
  716. "%s: WARNING: skipping badly formatted entry (id %lu)\n",
  717. inst->inst_name, (u_long)temp_id, 0);
  718. continue;
  719. }
  720. slapi_ch_free(&(data.data));
  721. /* generate uniqueid if necessary */
  722. import_generate_uniqueid(job, e);
  723. ep = import_make_backentry(e, temp_id);
  724. if (!ep)
  725. goto error;
  726. /* not sure what this does, but it looked like it could be
  727. * simplified. if it's broken, it's my fault. -robey
  728. */
  729. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  730. Slapi_Value **va = attr_get_present_values(attr);
  731. pw_encodevals( (Slapi_Value **)va ); /* jcm - cast away const */
  732. }
  733. if (job->flags & FLAG_ABORT)
  734. goto error;
  735. /* Now we have this new entry, all decoded
  736. * Next thing we need to do is:
  737. * (1) see if the appropriate fifo location contains an
  738. * entry which had been processed by the indexers.
  739. * If so, proceed.
  740. * If not, spin waiting for it to become free.
  741. * (2) free the old entry and store the new one there.
  742. * (3) Update the job progress indicators so the indexers
  743. * can use the new entry.
  744. */
  745. idx = id % job->fifo.size;
  746. old_ep = job->fifo.item[idx].entry;
  747. if (old_ep) {
  748. /* for the slot to be recycled, it needs to be already absorbed
  749. * by the foreman (id >= ready_ID), and all the workers need to
  750. * be finished with it (refcount = 0).
  751. */
  752. while (((old_ep->ep_refcnt > 0) ||
  753. (old_ep->ep_id >= job->ready_ID))
  754. && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  755. info->state = WAITING;
  756. DS_Sleep(sleeptime);
  757. }
  758. if (job->flags & FLAG_ABORT)
  759. goto error;
  760. info->state = RUNNING;
  761. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  762. job->fifo.item[idx].entry = NULL;
  763. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  764. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  765. else
  766. job->fifo.c_bsize = 0;
  767. backentry_free(&old_ep);
  768. }
  769. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  770. if (newesize > job->fifo.bsize) { /* entry too big */
  771. char ebuf[BUFSIZ];
  772. import_log_notice(job, "WARNING: skipping entry \"%s\"",
  773. escape_string(slapi_entry_get_dn(e), ebuf));
  774. import_log_notice(job, "REASON: entry too large (%lu bytes) for "
  775. "the buffer size (%lu bytes)", newesize, job->fifo.bsize);
  776. backentry_free(&ep);
  777. job->skipped++;
  778. continue;
  779. }
  780. /* Now check if fifo has enough space for the new entry */
  781. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  782. import_wait_for_space_in_fifo( job, newesize );
  783. }
  784. /* We have enough space */
  785. job->fifo.item[idx].filename = ID2ENTRY LDBM_FILENAME_SUFFIX;
  786. job->fifo.item[idx].line = curr_entry;
  787. job->fifo.item[idx].entry = ep;
  788. job->fifo.item[idx].bad = 0;
  789. job->fifo.item[idx].esize = newesize;
  790. /* Add the entry size to total fifo size */
  791. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  792. /* Update the job to show our progress */
  793. job->lead_ID = id;
  794. if ((id - info->first_ID) <= job->fifo.size) {
  795. job->trailing_ID = info->first_ID;
  796. } else {
  797. job->trailing_ID = id - job->fifo.size;
  798. }
  799. /* Update our progress meter too */
  800. info->last_ID_processed = id;
  801. id++;
  802. if (job->flags & FLAG_ABORT)
  803. goto error;
  804. if (info->command == STOP)
  805. {
  806. finished = 1;
  807. }
  808. }
  809. dbc->c_close(dbc);
  810. dblayer_release_aux_id2entry( be, db, env );
  811. info->state = FINISHED;
  812. return;
  813. error:
  814. dbc->c_close(dbc);
  815. dblayer_release_aux_id2entry( be, db, env );
  816. info->state = ABORTED;
  817. }
  818. static void
  819. import_wait_for_space_in_fifo(ImportJob *job, size_t new_esize)
  820. {
  821. struct backentry *temp_ep = NULL;
  822. size_t i;
  823. int slot_found;
  824. PRIntervalTime sleeptime;
  825. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  826. /* Now check if fifo has enough space for the new entry */
  827. while ((job->fifo.c_bsize + new_esize) > job->fifo.bsize) {
  828. for ( i = 0, slot_found = 0 ; i < job->fifo.size ; i++ ) {
  829. temp_ep = job->fifo.item[i].entry;
  830. if (temp_ep) {
  831. if (temp_ep->ep_refcnt == 0 && temp_ep->ep_id <= job->ready_ID) {
  832. job->fifo.item[i].entry = NULL;
  833. if (job->fifo.c_bsize > job->fifo.item[i].esize)
  834. job->fifo.c_bsize -= job->fifo.item[i].esize;
  835. else
  836. job->fifo.c_bsize = 0;
  837. backentry_free(&temp_ep);
  838. slot_found = 1;
  839. }
  840. }
  841. }
  842. if ( slot_found == 0 )
  843. DS_Sleep(sleeptime);
  844. }
  845. }
  846. /* helper function for the foreman: */
  847. static int foreman_do_parentid(ImportJob *job, struct backentry *entry,
  848. struct attrinfo *parentid_ai)
  849. {
  850. backend *be = job->inst->inst_be;
  851. Slapi_Value **svals = NULL;
  852. Slapi_Attr *attr = NULL;
  853. int idl_disposition = 0;
  854. int ret = 0;
  855. if (slapi_entry_attr_find(entry->ep_entry, "parentid", &attr) == 0) {
  856. svals = attr_get_present_values(attr);
  857. ret = index_addordel_values_ext_sv(be, "parentid", svals, NULL, entry->ep_id,
  858. BE_INDEX_ADD, NULL, &idl_disposition, NULL);
  859. if (idl_disposition != IDL_INSERT_NORMAL) {
  860. char *attr_value = slapi_value_get_berval(svals[0])->bv_val;
  861. ID parent_id = atol(attr_value);
  862. if (idl_disposition == IDL_INSERT_NOW_ALLIDS) {
  863. import_subcount_mother_init(job->mothers, parent_id,
  864. idl_get_allidslimit(parentid_ai)+1);
  865. } else if (idl_disposition == IDL_INSERT_ALLIDS) {
  866. import_subcount_mother_count(job->mothers, parent_id);
  867. }
  868. }
  869. if (ret != 0) {
  870. import_log_notice(job, "ERROR: Can't update parentid index "
  871. "(error %d)", ret);
  872. return ret;
  873. }
  874. }
  875. return 0;
  876. }
  877. /* helper function for the foreman: */
  878. static int foreman_do_entrydn(ImportJob *job, FifoItem *fi)
  879. {
  880. backend *be = job->inst->inst_be;
  881. struct berval bv;
  882. int err = 0, ret = 0;
  883. IDList *IDL;
  884. /* insert into the entrydn index */
  885. bv.bv_val = (void*)backentry_get_ndn(fi->entry); /* jcm - Had to cast away const */
  886. bv.bv_len = strlen(bv.bv_val);
  887. /* We need to check here whether the DN is already present in
  888. * the entrydn index. If it is then the input ldif
  889. * contained a duplicate entry, which it isn't allowed to */
  890. /* Due to popular demand, we only warn on this, given the
  891. * tendency for customers to want to import dirty data */
  892. /* So, we do an index read first */
  893. err = 0;
  894. IDL = index_read(be, "entrydn", indextype_EQUALITY, &bv, NULL, &err);
  895. /* Did this work ? */
  896. if (NULL != IDL) {
  897. /* IMPOSTER ! Get thee hence... */
  898. import_log_notice(job, "WARNING: Skipping duplicate entry "
  899. "\"%s\" found at line %d of file \"%s\"",
  900. slapi_entry_get_dn(fi->entry->ep_entry),
  901. fi->line, fi->filename);
  902. idl_free(IDL);
  903. /* skip this one */
  904. fi->bad = 1;
  905. job->skipped++;
  906. return -1; /* skip to next entry */
  907. }
  908. if ((ret = index_addordel_string(be, "entrydn",
  909. bv.bv_val,
  910. fi->entry->ep_id,
  911. BE_INDEX_ADD|BE_INDEX_NORMALIZED, NULL)) != 0) {
  912. import_log_notice(job, "Error writing entrydn index "
  913. "(error %d: %s)",
  914. ret, dblayer_strerror(ret));
  915. return ret;
  916. }
  917. return 0;
  918. }
  919. /* foreman thread:
  920. * i go through the FIFO just like the other worker threads, but i'm
  921. * responsible for the interrelated indexes: entrydn, id2entry, and the
  922. * operational attributes (plus the parentid index).
  923. */
  924. void import_foreman(void *param)
  925. {
  926. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  927. ImportJob *job = info->job;
  928. ldbm_instance *inst = job->inst;
  929. backend *be = inst->inst_be;
  930. PRIntervalTime sleeptime;
  931. int finished = 0;
  932. ID id = info->first_ID;
  933. int ret = 0;
  934. struct attrinfo *parentid_ai;
  935. Slapi_PBlock *pb = slapi_pblock_new();
  936. int shift = 0;
  937. PR_ASSERT(info != NULL);
  938. PR_ASSERT(inst != NULL);
  939. if (job->flags & FLAG_ABORT) {
  940. goto error;
  941. }
  942. /* the pblock is used only by add_op_attrs */
  943. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  944. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  945. info->state = RUNNING;
  946. ainfo_get(be, "parentid", &parentid_ai);
  947. while (! finished) {
  948. FifoItem *fi = NULL;
  949. int parent_status = 0;
  950. if (job->flags & FLAG_ABORT) {
  951. goto error;
  952. }
  953. while ( ((info->command == PAUSE) || (id > job->lead_ID)) &&
  954. (info->command != STOP) && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  955. /* Check to see if we've been told to stop */
  956. info->state = WAITING;
  957. DS_Sleep(sleeptime);
  958. }
  959. if (info->command == STOP) {
  960. finished = 1;
  961. continue;
  962. }
  963. if (job->flags & FLAG_ABORT) {
  964. goto error;
  965. }
  966. info->state = RUNNING;
  967. /* Read that entry from the cache */
  968. fi = import_fifo_fetch(job, id, 0, shift);
  969. if (! fi) {
  970. import_log_notice(job, "WARNING: entry id %d is missing", id);
  971. shift++;
  972. continue;
  973. }
  974. /* first, fill in any operational attributes */
  975. /* add_op_attrs wants a pblock for some reason. */
  976. if (add_op_attrs(pb, inst->inst_li, fi->entry, &parent_status) != 0) {
  977. import_log_notice(job, "ERROR: Could not add op attrs to "
  978. "entry ending at line %d of file \"%s\"",
  979. fi->line, fi->filename);
  980. goto error;
  981. }
  982. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  983. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  984. /*
  985. * Only check for a parent and add to the entry2dn index if
  986. * the entry is not a tombstone.
  987. */
  988. if (job->flags & FLAG_ABORT) {
  989. goto error;
  990. }
  991. if (parent_status == IMPORT_ADD_OP_ATTRS_NO_PARENT) {
  992. /* If this entry is a suffix entry, this is not a problem */
  993. /* However, if it is not, this is an error---it means that
  994. * someone tried to import an entry before importing its parent
  995. * we reject the entry but carry on since we've not stored
  996. * anything related to this entry.
  997. */
  998. if (! slapi_be_issuffix(inst->inst_be, backentry_get_sdn(fi->entry))) {
  999. import_log_notice(job, "WARNING: Skipping entry \"%s\" "
  1000. "which has no parent, ending at line %d "
  1001. "of file \"%s\"",
  1002. slapi_entry_get_dn(fi->entry->ep_entry),
  1003. fi->line, fi->filename);
  1004. /* skip this one */
  1005. fi->bad = 1;
  1006. job->skipped++;
  1007. goto cont; /* below */
  1008. }
  1009. }
  1010. if (job->flags & FLAG_ABORT) {
  1011. goto error;
  1012. }
  1013. /* insert into the entrydn index */
  1014. ret = foreman_do_entrydn(job, fi);
  1015. if (ret == -1)
  1016. goto cont; /* skip entry */
  1017. if (ret != 0)
  1018. goto error;
  1019. }
  1020. if (job->flags & FLAG_ABORT) {
  1021. goto error;
  1022. }
  1023. if (!(job->flags & FLAG_REINDEXING))/* reindex reads data from id2entry */
  1024. {
  1025. /* insert into the id2entry index
  1026. * (that isn't really an index -- it's the storehouse of the entries
  1027. * themselves.)
  1028. */
  1029. if ((ret = id2entry_add_ext(be, fi->entry, NULL, job->encrypt)) != 0) {
  1030. /* DB_RUNRECOVERY usually occurs if disk fills */
  1031. if (LDBM_OS_ERR_IS_DISKFULL(ret)) {
  1032. import_log_notice(job, "ERROR: OUT OF SPACE ON DISK or FILE TOO LARGE -- "
  1033. "Could not store the entry ending at line "
  1034. "%d of file \"%s\"",
  1035. fi->line, fi->filename);
  1036. } else if (ret == DB_RUNRECOVERY) {
  1037. import_log_notice(job, "FATAL ERROR: (LARGEFILE SUPPORT NOT ENABLED? OUT OF SPACE ON DISK?) -- "
  1038. "Could not store the entry ending at line "
  1039. "%d of file \"%s\"",
  1040. fi->line, fi->filename);
  1041. } else {
  1042. import_log_notice(job, "ERROR: Could not store the entry "
  1043. "ending at line %d of file \"%s\" -- "
  1044. "error %d", fi->line, fi->filename, ret);
  1045. }
  1046. goto error;
  1047. }
  1048. }
  1049. if (job->flags & FLAG_ABORT) {
  1050. goto error;
  1051. }
  1052. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  1053. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  1054. /* parentid index
  1055. * (we have to do this here, because the parentID is dependent on
  1056. * looking up by entrydn.)
  1057. * Only add to the parent index if the entry is not a tombstone.
  1058. */
  1059. ret = foreman_do_parentid(job, fi->entry, parentid_ai);
  1060. if (ret != 0)
  1061. goto error;
  1062. /* Lastly, before we're finished with the entry, pass it to the
  1063. vlv code to see whether it's within the scope a VLV index. */
  1064. vlv_grok_new_import_entry(fi->entry, be);
  1065. }
  1066. if (job->flags & FLAG_ABORT) {
  1067. goto error;
  1068. }
  1069. /* Remove the entry from the cache (caused by id2entry_add) */
  1070. if (!(job->flags & FLAG_REINDEXING))/* reindex reads data from id2entry */
  1071. cache_remove(&inst->inst_cache, fi->entry);
  1072. fi->entry->ep_refcnt = job->number_indexers;
  1073. cont:
  1074. if (job->flags & FLAG_ABORT) {
  1075. goto error;
  1076. }
  1077. job->ready_ID = id;
  1078. info->last_ID_processed = id;
  1079. id++;
  1080. if (job->flags & FLAG_ABORT){
  1081. goto error;
  1082. }
  1083. }
  1084. slapi_pblock_destroy(pb);
  1085. info->state = FINISHED;
  1086. return;
  1087. error:
  1088. slapi_pblock_destroy(pb);
  1089. info->state = ABORTED;
  1090. }
  1091. /* worker thread:
  1092. * given an attribute, this worker plows through the entry FIFO, building
  1093. * up the attribute index.
  1094. */
  1095. void import_worker(void *param)
  1096. {
  1097. ImportWorkerInfo *info = (ImportWorkerInfo *)param;
  1098. ImportJob *job = info->job;
  1099. ldbm_instance *inst = job->inst;
  1100. backend *be = inst->inst_be;
  1101. PRIntervalTime sleeptime;
  1102. int finished = 0;
  1103. ID id = info->first_ID;
  1104. int ret = 0;
  1105. int idl_disposition = 0;
  1106. struct vlvIndex* vlv_index = NULL;
  1107. void *substring_key_buffer = NULL;
  1108. FifoItem *fi = NULL;
  1109. int is_objectclass_attribute;
  1110. int is_nsuniqueid_attribute;
  1111. int is_nscpentrydn_attribute;
  1112. void *attrlist_cursor;
  1113. PR_ASSERT(NULL != info);
  1114. PR_ASSERT(NULL != inst);
  1115. if (job->flags & FLAG_ABORT) {
  1116. goto error;
  1117. }
  1118. if (INDEX_VLV == info->index_info->ai->ai_indexmask) {
  1119. vlv_index = vlv_find_indexname(info->index_info->name, be);
  1120. if (NULL == vlv_index) {
  1121. goto error;
  1122. }
  1123. }
  1124. /*
  1125. * If the entry is a Tombstone, then we only add it to the nsuniqeid index,
  1126. * the nscpEntryDN index, and the idlist for (objectclass=tombstone). These
  1127. * flags are just handy for working out what to do in this case.
  1128. */
  1129. is_objectclass_attribute =
  1130. (strcasecmp(info->index_info->name, "objectclass") == 0);
  1131. is_nsuniqueid_attribute =
  1132. (strcasecmp(info->index_info->name, SLAPI_ATTR_UNIQUEID) == 0);
  1133. is_nscpentrydn_attribute =
  1134. (strcasecmp(info->index_info->name, SLAPI_ATTR_NSCP_ENTRYDN) == 0);
  1135. if (1 != idl_get_idl_new()) {
  1136. /* Is there substring indexing going on here ? */
  1137. if ( (INDEX_SUB & info->index_info->ai->ai_indexmask) &&
  1138. (info->index_buffer_size > 0) ) {
  1139. /* Then make a key buffer thing */
  1140. ret = index_buffer_init(info->index_buffer_size, 0,
  1141. &substring_key_buffer);
  1142. if (0 != ret) {
  1143. import_log_notice(job, "IMPORT FAIL 1 (error %d)", ret);
  1144. }
  1145. }
  1146. }
  1147. sleeptime = PR_MillisecondsToInterval(import_sleep_time);
  1148. info->state = RUNNING;
  1149. info->last_ID_processed = id-1;
  1150. while (! finished) {
  1151. struct backentry *ep = NULL;
  1152. Slapi_Value **svals = NULL;
  1153. Slapi_Attr *attr = NULL;
  1154. if (job->flags & FLAG_ABORT) {
  1155. goto error;
  1156. }
  1157. /* entry can be NULL if it turned out to be bogus */
  1158. while (!finished && !ep) {
  1159. /* This worker thread must wait if the command flag is "PAUSE" or
  1160. * the entry corresponds to the current entry treated by the foreman
  1161. * thread, and the state is neither STOP nor ABORT
  1162. */
  1163. while (((info->command == PAUSE) || (id > job->ready_ID)) &&
  1164. (info->command != STOP) && (info->command != ABORT) && !(job->flags & FLAG_ABORT)) {
  1165. /* Check to see if we've been told to stop */
  1166. info->state = WAITING;
  1167. DS_Sleep(sleeptime);
  1168. }
  1169. if (info->command == STOP) {
  1170. finished = 1;
  1171. continue;
  1172. }
  1173. if (job->flags & FLAG_ABORT) {
  1174. goto error;
  1175. }
  1176. info->state = RUNNING;
  1177. /* Read that entry from the cache */
  1178. fi = import_fifo_fetch(job, id, 1, 0);
  1179. ep = fi ? fi->entry : NULL;
  1180. if (!ep) {
  1181. /* skipping an entry that turned out to be bad */
  1182. info->last_ID_processed = id;
  1183. id++;
  1184. }
  1185. }
  1186. if (finished)
  1187. continue;
  1188. if (! slapi_entry_flag_is_set(fi->entry->ep_entry,
  1189. SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  1190. /* This is not a tombstone entry. */
  1191. /* Is this a VLV index ? */
  1192. if (job->flags & FLAG_ABORT) {
  1193. goto error;
  1194. }
  1195. if (INDEX_VLV == info->index_info->ai->ai_indexmask) {
  1196. /* Yes, call VLV code -- needs pblock to find backend */
  1197. Slapi_PBlock *pb = slapi_pblock_new();
  1198. PR_ASSERT(NULL != vlv_index);
  1199. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  1200. vlv_update_index(vlv_index, NULL, inst->inst_li, pb, NULL, ep);
  1201. slapi_pblock_destroy(pb);
  1202. } else {
  1203. /* No, process regular index */
  1204. /* Look for the attribute we're indexing and its subtypes */
  1205. /* For each attr write to the index */
  1206. attrlist_cursor = NULL;
  1207. while ((attr = attrlist_find_ex(ep->ep_entry->e_attrs,
  1208. info->index_info->name,
  1209. NULL,
  1210. NULL,
  1211. &attrlist_cursor)) != NULL) {
  1212. if (job->flags & FLAG_ABORT) {
  1213. goto error;
  1214. }
  1215. if(valueset_isempty(&(attr->a_present_values))) continue;
  1216. svals = attr_get_present_values(attr);
  1217. ret = index_addordel_values_ext_sv(be, info->index_info->name,
  1218. svals, NULL, ep->ep_id, BE_INDEX_ADD | (job->encrypt ? 0 : BE_INDEX_DONT_ENCRYPT), NULL, &idl_disposition,
  1219. substring_key_buffer);
  1220. if (0 != ret) {
  1221. /* Something went wrong, eg disk filled up */
  1222. goto error;
  1223. }
  1224. }
  1225. }
  1226. } else {
  1227. /* This is a Tombstone entry... we only add it to the nsuniqueid
  1228. * index, the nscpEntryDN index, and the idlist for (objectclass=nstombstone).
  1229. */
  1230. if (job->flags & FLAG_ABORT) {
  1231. goto error;
  1232. }
  1233. if (is_nsuniqueid_attribute) {
  1234. ret = index_addordel_string(be, SLAPI_ATTR_UNIQUEID,
  1235. slapi_entry_get_uniqueid(ep->ep_entry), ep->ep_id,
  1236. BE_INDEX_ADD, NULL);
  1237. if (0 != ret) {
  1238. /* Something went wrong, eg disk filled up */
  1239. goto error;
  1240. }
  1241. }
  1242. if (is_objectclass_attribute) {
  1243. ret = index_addordel_string(be, SLAPI_ATTR_OBJECTCLASS,
  1244. SLAPI_ATTR_VALUE_TOMBSTONE, ep->ep_id, BE_INDEX_ADD, NULL);
  1245. if (0 != ret) {
  1246. /* Something went wrong, eg disk filled up */
  1247. goto error;
  1248. }
  1249. }
  1250. if (is_nscpentrydn_attribute) {
  1251. attrlist_cursor = NULL;
  1252. while ((attr = attrlist_find_ex(ep->ep_entry->e_attrs,
  1253. SLAPI_ATTR_NSCP_ENTRYDN,
  1254. NULL,
  1255. NULL,
  1256. &attrlist_cursor)) != NULL) {
  1257. if (job->flags & FLAG_ABORT) {
  1258. goto error;
  1259. }
  1260. if(valueset_isempty(&(attr->a_present_values))) continue;
  1261. svals = attr_get_present_values(attr);
  1262. ret = index_addordel_values_ext_sv(be, info->index_info->name,
  1263. svals, NULL, ep->ep_id, BE_INDEX_ADD | (job->encrypt ? 0 : BE_INDEX_DONT_ENCRYPT), NULL, &idl_disposition,
  1264. substring_key_buffer);
  1265. if (0 != ret) {
  1266. /* Something went wrong, eg disk filled up */
  1267. goto error;
  1268. }
  1269. }
  1270. }
  1271. }
  1272. import_decref_entry(ep);
  1273. info->last_ID_processed = id;
  1274. id++;
  1275. if (job->flags & FLAG_ABORT) {
  1276. goto error;
  1277. }
  1278. }
  1279. if (job->flags & FLAG_ABORT) {
  1280. goto error;
  1281. }
  1282. /* If we were buffering index keys, now flush them */
  1283. if (substring_key_buffer) {
  1284. ret = index_buffer_flush(substring_key_buffer,
  1285. inst->inst_be, NULL,
  1286. info->index_info->ai);
  1287. if (0 != ret) {
  1288. goto error;
  1289. }
  1290. index_buffer_terminate(substring_key_buffer);
  1291. }
  1292. info->state = FINISHED;
  1293. return;
  1294. error:
  1295. if (ret == DB_RUNRECOVERY) {
  1296. LDAPDebug(LDAP_DEBUG_ANY,"cannot import; database recovery needed\n",
  1297. 0,0,0);
  1298. } else if (ret == DB_LOCK_DEADLOCK) {
  1299. /* can this occur? */
  1300. }
  1301. info->state = ABORTED;
  1302. }
  1303. /*
  1304. * import entries to a backend, over the wire -- entries will arrive
  1305. * asynchronously, so this method has no "producer" thread. instead, the
  1306. * front-end drops new entries in as they arrive.
  1307. *
  1308. * this is sometimes called "fast replica initialization".
  1309. *
  1310. * some of this code is duplicated from ldif2ldbm, but i don't think we
  1311. * can avoid it.
  1312. */
  1313. static int bulk_import_start(Slapi_PBlock *pb)
  1314. {
  1315. struct ldbminfo *li = NULL;
  1316. ImportJob *job = NULL;
  1317. backend *be = NULL;
  1318. PRThread *thread = NULL;
  1319. int ret = 0;
  1320. job = CALLOC(ImportJob);
  1321. if (job == NULL) {
  1322. LDAPDebug(LDAP_DEBUG_ANY, "not enough memory to do import job\n",
  1323. 0, 0, 0);
  1324. return -1;
  1325. }
  1326. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1327. PR_ASSERT(be != NULL);
  1328. li = (struct ldbminfo *)(be->be_database->plg_private);
  1329. job->inst = (ldbm_instance *)be->be_instance_info;
  1330. /* check if an import/restore is already ongoing... */
  1331. PR_Lock(job->inst->inst_config_mutex);
  1332. if (job->inst->inst_flags & INST_FLAG_BUSY) {
  1333. PR_Unlock(job->inst->inst_config_mutex);
  1334. LDAPDebug(LDAP_DEBUG_ANY, "ldbm: '%s' is already in the middle of "
  1335. "another task and cannot be disturbed.\n",
  1336. job->inst->inst_name, 0, 0);
  1337. FREE(job);
  1338. return SLAPI_BI_ERR_BUSY;
  1339. }
  1340. job->inst->inst_flags |= INST_FLAG_BUSY;
  1341. PR_Unlock(job->inst->inst_config_mutex);
  1342. /* take backend offline */
  1343. slapi_mtn_be_disable(be);
  1344. /* get uniqueid info */
  1345. slapi_pblock_get(pb, SLAPI_LDIF2DB_GENERATE_UNIQUEID, &job->uuid_gen_type);
  1346. if (job->uuid_gen_type == SLAPI_UNIQUEID_GENERATE_NAME_BASED) {
  1347. char *namespaceid;
  1348. slapi_pblock_get(pb, SLAPI_LDIF2DB_NAMESPACEID, &namespaceid);
  1349. job->uuid_namespace = slapi_ch_strdup(namespaceid);
  1350. }
  1351. job->flags = 0; /* don't use files */
  1352. job->flags |= FLAG_INDEX_ATTRS;
  1353. job->flags |= FLAG_ONLINE;
  1354. job->starting_ID = 1;
  1355. job->first_ID = 1;
  1356. job->mothers = CALLOC(import_subcount_stuff);
  1357. /* how much space should we allocate to index buffering? */
  1358. job->job_index_buffer_size = import_get_index_buffer_size();
  1359. if (job->job_index_buffer_size == 0) {
  1360. /* 10% of the allocated cache size + one meg */
  1361. job->job_index_buffer_size = (job->inst->inst_li->li_dbcachesize/10) +
  1362. (1024*1024);
  1363. }
  1364. import_subcount_stuff_init(job->mothers);
  1365. job->wire_lock = PR_NewLock();
  1366. job->wire_cv = PR_NewCondVar(job->wire_lock);
  1367. /* COPIED from ldif2ldbm.c : */
  1368. /* shutdown this instance of the db */
  1369. cache_clear(&job->inst->inst_cache);
  1370. dblayer_instance_close(be);
  1371. /* Delete old database files */
  1372. dblayer_delete_instance_dir(be);
  1373. /* it's okay to fail -- it might already be gone */
  1374. /* dblayer_instance_start will init the id2entry index. */
  1375. /* it also (finally) fills in inst_dir_name */
  1376. ret = dblayer_instance_start(be, DBLAYER_IMPORT_MODE);
  1377. if (ret != 0)
  1378. goto fail;
  1379. /* END OF COPIED SECTION */
  1380. PR_Lock(job->wire_lock);
  1381. vlv_init(job->inst);
  1382. /* create thread for import_main, so we can return */
  1383. thread = PR_CreateThread(PR_USER_THREAD, import_main, (void *)job,
  1384. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1385. PR_JOINABLE_THREAD,
  1386. SLAPD_DEFAULT_THREAD_STACKSIZE);
  1387. if (thread == NULL) {
  1388. PRErrorCode prerr = PR_GetError();
  1389. LDAPDebug(LDAP_DEBUG_ANY, "unable to spawn import thread, "
  1390. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  1391. prerr, slapd_pr_strerror(prerr), 0);
  1392. PR_Unlock(job->wire_lock);
  1393. ret = -2;
  1394. goto fail;
  1395. }
  1396. job->main_thread = thread;
  1397. slapi_set_object_extension(li->li_bulk_import_object, pb->pb_conn,
  1398. li->li_bulk_import_handle, job);
  1399. /* wait for the import_main to signal that it's ready for entries */
  1400. /* (don't want to send the success code back to the LDAP client until
  1401. * we're ready for the adds to start rolling in)
  1402. */
  1403. PR_WaitCondVar(job->wire_cv, PR_INTERVAL_NO_TIMEOUT);
  1404. PR_Unlock(job->wire_lock);
  1405. return 0;
  1406. fail:
  1407. PR_Lock(job->inst->inst_config_mutex);
  1408. job->inst->inst_flags &= ~INST_FLAG_BUSY;
  1409. PR_Unlock(job->inst->inst_config_mutex);
  1410. import_free_job(job);
  1411. FREE(job);
  1412. return ret;
  1413. }
  1414. /* returns 0 on success, or < 0 on error
  1415. *
  1416. * on error, the import process is aborted -- so if this returns an error,
  1417. * don't try to queue any more entries or you'll be sorry. The caller
  1418. * is also responsible for free'ing the passed in entry on error. The
  1419. * entry will be consumed on success.
  1420. */
  1421. static int bulk_import_queue(ImportJob *job, Slapi_Entry *entry)
  1422. {
  1423. struct backentry *ep = NULL, *old_ep = NULL;
  1424. int idx;
  1425. ID id = 0;
  1426. Slapi_Attr *attr = NULL;
  1427. size_t newesize = 0;
  1428. PR_Lock(job->wire_lock);
  1429. /* Let's do this inside the lock !*/
  1430. id = job->lead_ID + 1;
  1431. /* generate uniqueid if necessary */
  1432. import_generate_uniqueid(job, entry);
  1433. /* make into backentry */
  1434. ep = import_make_backentry(entry, id);
  1435. if (!ep) {
  1436. import_abort_all(job, 1);
  1437. PR_Unlock(job->wire_lock);
  1438. return -1;
  1439. }
  1440. /* encode the password */
  1441. if (slapi_entry_attr_find(ep->ep_entry, "userpassword", &attr) == 0) {
  1442. Slapi_Value **va = attr_get_present_values(attr);
  1443. pw_encodevals( (Slapi_Value **)va ); /* jcm - had to cast away const */
  1444. }
  1445. /* Now we have this new entry, all decoded
  1446. * Next thing we need to do is:
  1447. * (1) see if the appropriate fifo location contains an
  1448. * entry which had been processed by the indexers.
  1449. * If so, proceed.
  1450. * If not, spin waiting for it to become free.
  1451. * (2) free the old entry and store the new one there.
  1452. * (3) Update the job progress indicators so the indexers
  1453. * can use the new entry.
  1454. */
  1455. idx = id % job->fifo.size;
  1456. old_ep = job->fifo.item[idx].entry;
  1457. if (old_ep) {
  1458. while ((old_ep->ep_refcnt > 0) && !(job->flags & FLAG_ABORT))
  1459. {
  1460. DS_Sleep(PR_MillisecondsToInterval(import_sleep_time));
  1461. }
  1462. /* the producer could be running thru the fifo while
  1463. * everyone else is cycling to a new pass...
  1464. * double-check that this entry is < ready_ID
  1465. */
  1466. while ((old_ep->ep_id >= job->ready_ID) && !(job->flags & FLAG_ABORT))
  1467. {
  1468. DS_Sleep(PR_MillisecondsToInterval(import_sleep_time));
  1469. }
  1470. if (job->flags & FLAG_ABORT) {
  1471. backentry_clear_entry(ep); /* entry is released in the frontend on failure*/
  1472. backentry_free( &ep ); /* release the backend wrapper, here */
  1473. PR_Unlock(job->wire_lock);
  1474. return -2;
  1475. }
  1476. PR_ASSERT(old_ep == job->fifo.item[idx].entry);
  1477. job->fifo.item[idx].entry = NULL;
  1478. if (job->fifo.c_bsize > job->fifo.item[idx].esize)
  1479. job->fifo.c_bsize -= job->fifo.item[idx].esize;
  1480. else
  1481. job->fifo.c_bsize = 0;
  1482. backentry_free(&old_ep);
  1483. }
  1484. newesize = (slapi_entry_size(ep->ep_entry) + sizeof(struct backentry));
  1485. if (newesize > job->fifo.bsize) { /* entry too big */
  1486. char ebuf[BUFSIZ];
  1487. import_log_notice(job, "WARNING: skipping entry \"%s\"",
  1488. escape_string(slapi_entry_get_dn(ep->ep_entry), ebuf));
  1489. import_log_notice(job, "REASON: entry too large (%lu bytes) for "
  1490. "the import buffer size (%lu bytes). Try increasing nsslapd-cachememsize.", newesize, job->fifo.bsize);
  1491. backentry_clear_entry(ep); /* entry is released in the frontend on failure*/
  1492. backentry_free( &ep ); /* release the backend wrapper, here */
  1493. PR_Unlock(job->wire_lock);
  1494. return -1;
  1495. }
  1496. /* Now check if fifo has enough space for the new entry */
  1497. if ((job->fifo.c_bsize + newesize) > job->fifo.bsize) {
  1498. import_wait_for_space_in_fifo( job, newesize );
  1499. }
  1500. /* We have enough space */
  1501. job->fifo.item[idx].filename = "(bulk import)";
  1502. job->fifo.item[idx].line = 0;
  1503. job->fifo.item[idx].entry = ep;
  1504. job->fifo.item[idx].bad = 0;
  1505. job->fifo.item[idx].esize = newesize;
  1506. /* Add the entry size to total fifo size */
  1507. job->fifo.c_bsize += ep->ep_entry? job->fifo.item[idx].esize : 0;
  1508. /* Update the job to show our progress */
  1509. job->lead_ID = id;
  1510. if ((id - job->starting_ID) <= job->fifo.size) {
  1511. job->trailing_ID = job->starting_ID;
  1512. } else {
  1513. job->trailing_ID = id - job->fifo.size;
  1514. }
  1515. PR_Unlock(job->wire_lock);
  1516. return 0;
  1517. }
  1518. void *factory_constructor(void *object, void *parent)
  1519. {
  1520. return NULL;
  1521. }
  1522. void factory_destructor(void *extension, void *object, void *parent)
  1523. {
  1524. ImportJob *job = (ImportJob *)extension;
  1525. PRThread *thread;
  1526. if (extension == NULL)
  1527. return;
  1528. /* connection was destroyed while we were still storing the extension --
  1529. * this is bad news and means we have a bulk import that needs to be
  1530. * aborted!
  1531. */
  1532. thread = job->main_thread;
  1533. LDAPDebug(LDAP_DEBUG_ANY, "ERROR bulk import abandoned\n",
  1534. 0, 0, 0);
  1535. import_abort_all(job, 1);
  1536. /* wait for import_main to finish... */
  1537. PR_JoinThread(thread);
  1538. /* extension object is free'd by import_main */
  1539. return;
  1540. }
  1541. /* plugin entry function for replica init
  1542. *
  1543. * For the SLAPI_BI_STATE_ADD state:
  1544. * On success (rc=0), the entry in pb->pb_import_entry will be
  1545. * consumed. For any other return value, the caller is
  1546. * responsible for freeing the entry in the pb.
  1547. */
  1548. int ldbm_back_wire_import(Slapi_PBlock *pb)
  1549. {
  1550. struct ldbminfo *li;
  1551. backend *be = NULL;
  1552. ImportJob *job = NULL;
  1553. PRThread *thread;
  1554. int state;
  1555. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  1556. PR_ASSERT(be != NULL);
  1557. li = (struct ldbminfo *)(be->be_database->plg_private);
  1558. slapi_pblock_get(pb, SLAPI_BULK_IMPORT_STATE, &state);
  1559. if (state == SLAPI_BI_STATE_START) {
  1560. /* starting a new import */
  1561. return bulk_import_start(pb);
  1562. }
  1563. PR_ASSERT(pb->pb_conn != NULL);
  1564. if (pb->pb_conn != NULL) {
  1565. job = (ImportJob *)slapi_get_object_extension(li->li_bulk_import_object, pb->pb_conn, li->li_bulk_import_handle);
  1566. }
  1567. if ((job == NULL) || (pb->pb_conn == NULL)) {
  1568. /* import might be aborting */
  1569. return -1;
  1570. }
  1571. if (state == SLAPI_BI_STATE_ADD) {
  1572. /* continuing previous import */
  1573. if (! import_entry_belongs_here(pb->pb_import_entry,
  1574. job->inst->inst_be)) {
  1575. /* silently skip */
  1576. /* We need to consume pb->pb_import_entry on success, so we free it here. */
  1577. slapi_entry_free(pb->pb_import_entry);
  1578. return 0;
  1579. }
  1580. return bulk_import_queue(job, pb->pb_import_entry);
  1581. }
  1582. thread = job->main_thread;
  1583. if (state == SLAPI_BI_STATE_DONE) {
  1584. /* finished with an import */
  1585. job->flags |= FLAG_PRODUCER_DONE;
  1586. /* "job" struct may vanish at any moment after we set the DONE
  1587. * flag, so keep a copy of the thread id in 'thread' for safekeeping.
  1588. */
  1589. /* wait for import_main to finish... */
  1590. PR_JoinThread(thread);
  1591. slapi_set_object_extension(li->li_bulk_import_object, pb->pb_conn,
  1592. li->li_bulk_import_handle, NULL);
  1593. return 0;
  1594. }
  1595. /* ??? unknown state */
  1596. LDAPDebug(LDAP_DEBUG_ANY,
  1597. "ERROR: ldbm_back_wire_import: unknown state %d\n",
  1598. state, 0, 0);
  1599. return -1;
  1600. }
  1601. /*
  1602. * backup index configuration
  1603. * this function is called from dblayer_backup (ldbm2archive)
  1604. * [547427] index config must not change between backup and restore
  1605. */
  1606. #define DSE_INDEX "dse_index.ldif"
  1607. #define DSE_INSTANCE "dse_instance.ldif"
  1608. #define DSE_INDEX_FILTER "(objectclass=nsIndex)"
  1609. #define DSE_INSTANCE_FILTER "(objectclass=nsBackendInstance)"
  1610. static int
  1611. dse_conf_backup_core(struct ldbminfo *li, char *dest_dir, char *file_name, char *filter)
  1612. {
  1613. Slapi_PBlock *srch_pb = NULL;
  1614. Slapi_Entry **entries = NULL;
  1615. Slapi_Entry **ep = NULL;
  1616. Slapi_Attr *attr = NULL;
  1617. char *attr_name;
  1618. char *filename = NULL;
  1619. PRFileDesc *prfd = NULL;
  1620. int rval = 0;
  1621. int dlen = 0;
  1622. PRInt32 prrval;
  1623. char tmpbuf[BUFSIZ];
  1624. char *tp = NULL;
  1625. dlen = strlen(dest_dir);
  1626. if (0 == dlen)
  1627. {
  1628. filename = file_name;
  1629. }
  1630. else
  1631. {
  1632. filename = slapi_ch_smprintf("%s/%s", dest_dir, file_name);
  1633. }
  1634. LDAPDebug(LDAP_DEBUG_TRACE, "dse_conf_backup(%s): backup file %s\n",
  1635. filter, filename, 0);
  1636. /* Open the file to write */
  1637. if ((prfd = PR_Open(filename, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE,
  1638. SLAPD_DEFAULT_FILE_MODE)) == NULL)
  1639. {
  1640. LDAPDebug(LDAP_DEBUG_ANY,
  1641. "dse_conf_backup(%s): open %s failed: (%s)\n",
  1642. filter, filename, slapd_pr_strerror(PR_GetError()));
  1643. rval = -1;
  1644. goto out;
  1645. }
  1646. srch_pb = slapi_pblock_new();
  1647. slapi_search_internal_set_pb(srch_pb, li->li_plugin->plg_dn,
  1648. LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, li->li_identity, 0);
  1649. slapi_search_internal_pb(srch_pb);
  1650. slapi_pblock_get(srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1651. for (ep = entries; ep != NULL && *ep != NULL; ep++)
  1652. {
  1653. size_t l = strlen(slapi_entry_get_dn_const(*ep)) + 5 /* "dn: \n" */;
  1654. LDAPDebug(LDAP_DEBUG_TRACE, "\ndn: %s\n",
  1655. slapi_entry_get_dn_const(*ep), 0, 0);
  1656. if (l <= BUFSIZ)
  1657. tp = tmpbuf;
  1658. else
  1659. tp = (char *)slapi_ch_malloc(l); /* should be very rare ... */
  1660. sprintf(tp, "dn: %s\n", slapi_entry_get_dn_const(*ep));
  1661. prrval = PR_Write(prfd, tp, l);
  1662. if ((size_t)prrval != l)
  1663. {
  1664. LDAPDebug(LDAP_DEBUG_ANY,
  1665. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1666. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1667. rval = -1;
  1668. if (l > BUFSIZ)
  1669. slapi_ch_free_string(&tp);
  1670. goto out;
  1671. }
  1672. if (l > BUFSIZ)
  1673. slapi_ch_free_string(&tp);
  1674. for (slapi_entry_first_attr(*ep, &attr); attr;
  1675. slapi_entry_next_attr(*ep, attr, &attr))
  1676. {
  1677. int i;
  1678. Slapi_Value *sval = NULL;
  1679. const struct berval *attr_val;
  1680. int attr_name_len;
  1681. slapi_attr_get_type(attr, &attr_name);
  1682. /* numsubordinates should not be backed up */
  1683. if (!strcasecmp("numsubordinates", attr_name))
  1684. continue;
  1685. attr_name_len = strlen(attr_name);
  1686. for (i = slapi_attr_first_value(attr, &sval); i != -1;
  1687. i = slapi_attr_next_value(attr, i, &sval))
  1688. {
  1689. attr_val = slapi_value_get_berval(sval);
  1690. l = strlen(attr_val->bv_val) + attr_name_len + 3; /* : \n" */
  1691. LDAPDebug(LDAP_DEBUG_TRACE, "%s: %s\n", attr_name,
  1692. attr_val->bv_val, 0);
  1693. if (l <= BUFSIZ)
  1694. tp = tmpbuf;
  1695. else
  1696. tp = (char *)slapi_ch_malloc(l);
  1697. sprintf(tp, "%s: %s\n", attr_name, attr_val->bv_val);
  1698. prrval = PR_Write(prfd, tp, l);
  1699. if ((size_t)prrval != l)
  1700. {
  1701. LDAPDebug(LDAP_DEBUG_ANY,
  1702. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1703. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1704. rval = -1;
  1705. if (l > BUFSIZ)
  1706. slapi_ch_free_string(&tp);
  1707. goto out;
  1708. }
  1709. if (l > BUFSIZ)
  1710. slapi_ch_free_string(&tp);
  1711. }
  1712. }
  1713. if (ep+1 != NULL && *(ep+1) != NULL)
  1714. {
  1715. prrval = PR_Write(prfd, "\n", 1);
  1716. if ((int)prrval != 1)
  1717. {
  1718. LDAPDebug(LDAP_DEBUG_ANY,
  1719. "dse_conf_backup(%s): write %s failed: %d (%s)\n",
  1720. filter, PR_GetError(), slapd_pr_strerror(PR_GetError()));
  1721. rval = -1;
  1722. goto out;
  1723. }
  1724. }
  1725. }
  1726. out:
  1727. slapi_free_search_results_internal(srch_pb);
  1728. if (srch_pb)
  1729. {
  1730. slapi_pblock_destroy(srch_pb);
  1731. }
  1732. if (0 != dlen)
  1733. {
  1734. slapi_ch_free_string(&filename);
  1735. }
  1736. if (prfd)
  1737. {
  1738. prrval = PR_Close(prfd);
  1739. if (PR_SUCCESS != prrval)
  1740. {
  1741. LDAPDebug( LDAP_DEBUG_ANY,
  1742. "Fatal Error---Failed to back up dse indexes %d (%s)\n",
  1743. PR_GetError(), slapd_pr_strerror(PR_GetError()), 0);
  1744. rval = -1;
  1745. }
  1746. }
  1747. return rval;
  1748. }
  1749. int
  1750. dse_conf_backup(struct ldbminfo *li, char *dest_dir)
  1751. {
  1752. int rval = 0;
  1753. rval = dse_conf_backup_core(li, dest_dir, DSE_INSTANCE, DSE_INSTANCE_FILTER);
  1754. rval += dse_conf_backup_core(li, dest_dir, DSE_INDEX, DSE_INDEX_FILTER);
  1755. return rval;
  1756. }
  1757. /*
  1758. * read the backed up index configuration
  1759. * adjust them if the current configuration is different from it.
  1760. * this function is called from dblayer_restore (archive2ldbm)
  1761. * these functions are placed here to borrow import_get_entry
  1762. * [547427] index config must not change between backup and restore
  1763. */
  1764. int
  1765. dse_conf_verify_core(struct ldbminfo *li, char *src_dir, char *file_name, char *filter, char *log_str, char *entry_filter)
  1766. {
  1767. char *filename = NULL;
  1768. int rval = 0;
  1769. ldif_context c;
  1770. int fd = -1;
  1771. int curr_lineno = 0;
  1772. int finished = 0;
  1773. int backup_entry_len = 256;
  1774. char *search_scope = NULL;
  1775. Slapi_Entry **backup_entries = NULL;
  1776. Slapi_Entry **bep = NULL;
  1777. Slapi_Entry **curr_entries = NULL;
  1778. Slapi_PBlock srch_pb;
  1779. filename = slapi_ch_smprintf("%s/%s", src_dir, file_name);
  1780. if (PR_SUCCESS != PR_Access(filename, PR_ACCESS_READ_OK))
  1781. {
  1782. LDAPDebug(LDAP_DEBUG_ANY,
  1783. "Warning: config backup file %s not found in backup\n",
  1784. file_name, 0, 0);
  1785. rval = 0;
  1786. goto out;
  1787. }
  1788. fd = dblayer_open_huge_file(filename, O_RDONLY, 0);
  1789. if (fd < 0)
  1790. {
  1791. LDAPDebug(LDAP_DEBUG_ANY,
  1792. "Warning: can't open config backup file: %s\n", filename, 0, 0);
  1793. rval = -1;
  1794. goto out;
  1795. }
  1796. import_init_ldif(&c);
  1797. bep = backup_entries = (Slapi_Entry **)slapi_ch_calloc(1,
  1798. backup_entry_len * sizeof(Slapi_Entry *));
  1799. while (!finished)
  1800. {
  1801. char *estr = NULL;
  1802. Slapi_Entry *e = NULL;
  1803. estr = import_get_entry(&c, fd, &curr_lineno);
  1804. if (!estr)
  1805. break;
  1806. if (entry_filter != NULL) /* Single instance restoration */
  1807. {
  1808. if (NULL == strstr(estr, entry_filter))
  1809. continue;
  1810. }
  1811. e = slapi_str2entry(estr, 0);
  1812. slapi_ch_free_string(&estr);
  1813. if (!e) {
  1814. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: skipping bad LDIF entry "
  1815. "ending line %d of file \"%s\"", curr_lineno, filename, 0);
  1816. continue;
  1817. }
  1818. if (bep - backup_entries >= backup_entry_len)
  1819. {
  1820. backup_entries = (Slapi_Entry **)slapi_ch_realloc((char *)backup_entries,
  1821. 2 * backup_entry_len * sizeof(Slapi_Entry *));
  1822. bep = backup_entries + backup_entry_len;
  1823. backup_entry_len *= 2;
  1824. }
  1825. *bep = e;
  1826. bep++;
  1827. }
  1828. /* 623986: terminate the list if we reallocated backup_entries */
  1829. if (backup_entry_len > 256)
  1830. *bep = NULL;
  1831. pblock_init(&srch_pb);
  1832. if (entry_filter != NULL)
  1833. { /* Single instance restoration */
  1834. search_scope = slapi_ch_smprintf("%s,%s", entry_filter, li->li_plugin->plg_dn);
  1835. } else { /* Normal restoration */
  1836. search_scope = slapi_ch_strdup(li->li_plugin->plg_dn);
  1837. }
  1838. slapi_search_internal_set_pb(&srch_pb, search_scope,
  1839. LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, li->li_identity, 0);
  1840. slapi_search_internal_pb(&srch_pb);
  1841. slapi_pblock_get(&srch_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &curr_entries);
  1842. if (0 != slapi_entries_diff(backup_entries, curr_entries, 1 /* test_all */,
  1843. log_str, 1 /* force_update */, li->li_identity))
  1844. {
  1845. LDAPDebug(LDAP_DEBUG_ANY, "WARNING!!: current %s is "
  1846. "different from backed up configuration; "
  1847. "The backup is restored.\n", log_str, 0, 0);
  1848. }
  1849. slapi_free_search_results_internal(&srch_pb);
  1850. pblock_done(&srch_pb);
  1851. import_free_ldif(&c);
  1852. out:
  1853. for (bep = backup_entries; bep && *bep; bep++)
  1854. slapi_entry_free(*bep);
  1855. slapi_ch_free((void **)&backup_entries);
  1856. slapi_ch_free_string(&filename);
  1857. slapi_ch_free_string(&search_scope);
  1858. if (fd > 0)
  1859. close(fd);
  1860. return rval;
  1861. }
  1862. int
  1863. dse_conf_verify(struct ldbminfo *li, char *src_dir, char *bename)
  1864. {
  1865. int rval;
  1866. char *entry_filter = NULL;
  1867. char *instance_entry_filter = NULL;
  1868. if (bename != NULL) /* This was a restore of a single backend */
  1869. {
  1870. /* Entry filter string */
  1871. entry_filter = slapi_ch_smprintf("cn=%s", bename);
  1872. /* Instance search filter */
  1873. instance_entry_filter = slapi_ch_smprintf("(&%s(cn=%s))", DSE_INSTANCE_FILTER, bename);
  1874. } else {
  1875. instance_entry_filter = slapi_ch_strdup(DSE_INSTANCE_FILTER);
  1876. }
  1877. rval = dse_conf_verify_core(li, src_dir, DSE_INSTANCE, instance_entry_filter,
  1878. "Instance Config", entry_filter);
  1879. rval += dse_conf_verify_core(li, src_dir, DSE_INDEX, DSE_INDEX_FILTER,
  1880. "Index Config", entry_filter);
  1881. slapi_ch_free_string(&entry_filter);
  1882. slapi_ch_free_string(&instance_entry_filter);
  1883. return rval;
  1884. }