import-threads.c 71 KB

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