00001
00002
00003
00004
00005
00006
00007 #include "config.h"
00008
00009 #ifndef lint
00010 static const char revid[] = "$Id: log__put_8c-source.html,v 1.1 2008/06/08 10:20:26 sebdiaz Exp $";
00011 #endif
00012
00013 #ifndef NO_SYSTEM_INCLUDES
00014 #include <sys/types.h>
00015
00016 #if TIME_WITH_SYS_TIME
00017 #include <sys/time.h>
00018 #include <time.h>
00019 #else
00020 #if HAVE_SYS_TIME_H
00021 #include <sys/time.h>
00022 #else
00023 #include <time.h>
00024 #endif
00025 #endif
00026
00027 #include <errno.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 #endif
00032
00033 #ifdef HAVE_RPC
00034 #include "db_server.h"
00035 #endif
00036
00037 #include "db_int.h"
00038 #include "db_page.h"
00039 #include "log.h"
00040 #include "hash.h"
00041
00042 #ifdef HAVE_RPC
00043 #include "gen_client_ext.h"
00044 #include "rpc_client_ext.h"
00045 #endif
00046
00047 static int __log_fill __P((DB_LOG *, DB_LSN *, void *, u_int32_t));
00048 static int __log_flush __P((DB_LOG *, const DB_LSN *));
00049 static int __log_newfh __P((DB_LOG *));
00050 static int __log_putr __P((DB_LOG *, DB_LSN *, const DBT *, u_int32_t));
00051 static int __log_open_files __P((DB_ENV *));
00052 static int __log_write __P((DB_LOG *, void *, u_int32_t));
00053
00054
00055
00056
00057
00058 int
00059 CDB_log_put(dbenv, lsn, dbt, flags)
00060 DB_ENV *dbenv;
00061 DB_LSN *lsn;
00062 const DBT *dbt;
00063 u_int32_t flags;
00064 {
00065 DB_LOG *dblp;
00066 int ret;
00067
00068 #ifdef HAVE_RPC
00069 if (F_ISSET(dbenv, DB_ENV_RPCCLIENT))
00070 return (__dbcl_log_put(dbenv, lsn, dbt, flags));
00071 #endif
00072
00073 PANIC_CHECK(dbenv);
00074 ENV_REQUIRES_CONFIG(dbenv, dbenv->lg_handle, DB_INIT_LOG);
00075
00076
00077 if (flags != 0 && flags != DB_CHECKPOINT &&
00078 flags != DB_CURLSN && flags != DB_FLUSH)
00079 return (CDB___db_ferr(dbenv, "CDB_log_put", 0));
00080
00081 dblp = dbenv->lg_handle;
00082 R_LOCK(dbenv, &dblp->reginfo);
00083 ret = CDB___log_put(dbenv, lsn, dbt, flags);
00084 R_UNLOCK(dbenv, &dblp->reginfo);
00085 return (ret);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 int
00095 CDB___log_put(dbenv, lsn, dbt, flags)
00096 DB_ENV *dbenv;
00097 DB_LSN *lsn;
00098 const DBT *dbt;
00099 u_int32_t flags;
00100 {
00101 DBT t;
00102 DB_LOG *dblp;
00103 LOG *lp;
00104 u_int32_t lastoff;
00105 int ret;
00106
00107 dblp = dbenv->lg_handle;
00108 lp = dblp->reginfo.primary;
00109
00110
00111
00112
00113
00114
00115 if (flags == DB_CURLSN) {
00116 lsn->file = lp->lsn.file;
00117 lsn->offset = lp->lsn.offset;
00118 return (0);
00119 }
00120
00121
00122 if (lp->lsn.offset + sizeof(HDR) + dbt->size > lp->persist.lg_max) {
00123 if (sizeof(HDR) +
00124 sizeof(LOGP) + dbt->size > lp->persist.lg_max) {
00125 CDB___db_err(dbenv,
00126 "CDB_log_put: record larger than maximum file size");
00127 return (EINVAL);
00128 }
00129
00130
00131 if ((ret = __log_flush(dblp, NULL)) != 0)
00132 return (ret);
00133
00134
00135
00136
00137
00138 lastoff = lp->lsn.offset;
00139
00140
00141 ++lp->lsn.file;
00142 lp->lsn.offset = 0;
00143
00144
00145 lp->w_off = 0;
00146 } else
00147 lastoff = 0;
00148
00149
00150 lsn->file = lp->lsn.file;
00151 lsn->offset = lp->lsn.offset;
00152
00153
00154
00155
00156
00157
00158 if (lp->lsn.offset == 0) {
00159 t.data = &lp->persist;
00160 t.size = sizeof(LOGP);
00161 if ((ret = __log_putr(dblp, lsn,
00162 &t, lastoff == 0 ? 0 : lastoff - lp->len)) != 0)
00163 return (ret);
00164
00165
00166 if ((ret = __log_open_files(dbenv)) != 0)
00167 return (ret);
00168
00169
00170 lsn->file = lp->lsn.file;
00171 lsn->offset = lp->lsn.offset;
00172 }
00173
00174
00175 if ((ret = __log_putr(dblp, lsn, dbt, lp->lsn.offset - lp->len)) != 0)
00176 return (ret);
00177
00178
00179
00180
00181
00182
00183
00184 if (flags == DB_CHECKPOINT) {
00185 lp->chkpt_lsn = *lsn;
00186 if ((ret = __log_open_files(dbenv)) != 0)
00187 return (ret);
00188 }
00189
00190
00191
00192
00193
00194
00195 if (flags == DB_FLUSH || flags == DB_CHECKPOINT)
00196 if ((ret = __log_flush(dblp, NULL)) != 0)
00197 return (ret);
00198
00199
00200
00201
00202
00203
00204 if (flags == DB_CHECKPOINT) {
00205 (void)time(&lp->chkpt);
00206 lp->stat.st_wc_bytes = lp->stat.st_wc_mbytes = 0;
00207 }
00208 return (0);
00209 }
00210
00211
00212
00213
00214
00215 static int
00216 __log_putr(dblp, lsn, dbt, prev)
00217 DB_LOG *dblp;
00218 DB_LSN *lsn;
00219 const DBT *dbt;
00220 u_int32_t prev;
00221 {
00222 HDR hdr;
00223 LOG *lp;
00224 int ret;
00225
00226 lp = dblp->reginfo.primary;
00227
00228
00229
00230
00231
00232
00233 hdr.prev = prev;
00234 hdr.len = sizeof(HDR) + dbt->size;
00235 hdr.cksum = CDB___ham_func4(dbt->data, dbt->size);
00236
00237 if ((ret = __log_fill(dblp, lsn, &hdr, sizeof(HDR))) != 0)
00238 return (ret);
00239 lp->len = sizeof(HDR);
00240 lp->lsn.offset += sizeof(HDR);
00241
00242 if ((ret = __log_fill(dblp, lsn, dbt->data, dbt->size)) != 0)
00243 return (ret);
00244 lp->len += dbt->size;
00245 lp->lsn.offset += dbt->size;
00246 return (0);
00247 }
00248
00249
00250
00251
00252
00253 int
00254 CDB_log_flush(dbenv, lsn)
00255 DB_ENV *dbenv;
00256 const DB_LSN *lsn;
00257 {
00258 DB_LOG *dblp;
00259 int ret;
00260
00261 #ifdef HAVE_RPC
00262 if (F_ISSET(dbenv, DB_ENV_RPCCLIENT))
00263 return (__dbcl_log_flush(dbenv, lsn));
00264 #endif
00265
00266 PANIC_CHECK(dbenv);
00267 ENV_REQUIRES_CONFIG(dbenv, dbenv->lg_handle, DB_INIT_LOG);
00268
00269 dblp = dbenv->lg_handle;
00270 R_LOCK(dbenv, &dblp->reginfo);
00271 ret = __log_flush(dblp, lsn);
00272 R_UNLOCK(dbenv, &dblp->reginfo);
00273 return (ret);
00274 }
00275
00276
00277
00278
00279
00280
00281 static int
00282 __log_flush(dblp, lsn)
00283 DB_LOG *dblp;
00284 const DB_LSN *lsn;
00285 {
00286 DB_LSN t_lsn;
00287 LOG *lp;
00288 int current, ret;
00289
00290 ret = 0;
00291 lp = dblp->reginfo.primary;
00292
00293
00294
00295
00296
00297
00298 if (lsn == NULL) {
00299 t_lsn.file = lp->lsn.file;
00300 t_lsn.offset = lp->lsn.offset - lp->len;
00301 lsn = &t_lsn;
00302 } else
00303 if (lsn->file > lp->lsn.file ||
00304 (lsn->file == lp->lsn.file &&
00305 lsn->offset > lp->lsn.offset - lp->len)) {
00306 CDB___db_err(dblp->dbenv,
00307 "CDB_log_flush: LSN past current end-of-log");
00308 return (EINVAL);
00309 }
00310
00311
00312
00313
00314
00315
00316
00317 if (lsn->file < lp->s_lsn.file ||
00318 (lsn->file == lp->s_lsn.file && lsn->offset <= lp->s_lsn.offset))
00319 return (0);
00320
00321
00322
00323
00324
00325
00326 current = 0;
00327 if (lp->b_off != 0 && CDB_log_compare(lsn, &lp->f_lsn) >= 0) {
00328 if ((ret = __log_write(dblp, dblp->bufp, lp->b_off)) != 0)
00329 return (ret);
00330
00331 lp->b_off = 0;
00332 current = 1;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342 if (dblp->lfname != lp->lsn.file) {
00343 if (!current)
00344 return (0);
00345 if ((ret = __log_newfh(dblp)) != 0)
00346 return (ret);
00347 }
00348
00349
00350 if ((ret = CDB___os_fsync(dblp->dbenv, &dblp->lfh)) != 0) {
00351 CDB___db_panic(dblp->dbenv, ret);
00352 return (ret);
00353 }
00354 ++lp->stat.st_scount;
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 lp->s_lsn = lp->f_lsn;
00368 if (!current && lp->s_lsn.file != 0) {
00369 if (lp->s_lsn.offset == 0) {
00370 --lp->s_lsn.file;
00371 lp->s_lsn.offset = lp->persist.lg_max;
00372 } else
00373 --lp->s_lsn.offset;
00374 }
00375
00376 return (0);
00377 }
00378
00379
00380
00381
00382
00383 static int
00384 __log_fill(dblp, lsn, addr, len)
00385 DB_LOG *dblp;
00386 DB_LSN *lsn;
00387 void *addr;
00388 u_int32_t len;
00389 {
00390 LOG *lp;
00391 u_int32_t bsize, nrec;
00392 size_t nw, remain;
00393 int ret;
00394
00395 lp = dblp->reginfo.primary;
00396 bsize = lp->buffer_size;
00397
00398 while (len > 0) {
00399
00400
00401
00402
00403
00404
00405 if (lp->b_off == 0)
00406 lp->f_lsn = *lsn;
00407
00408
00409
00410
00411
00412 if (lp->b_off == 0 && len >= bsize) {
00413 nrec = len / bsize;
00414 if ((ret = __log_write(dblp, addr, nrec * bsize)) != 0)
00415 return (ret);
00416 addr = (u_int8_t *)addr + nrec * bsize;
00417 len -= nrec * bsize;
00418 ++lp->stat.st_wcount_fill;
00419 continue;
00420 }
00421
00422
00423 remain = bsize - lp->b_off;
00424 nw = remain > len ? len : remain;
00425 memcpy(dblp->bufp + lp->b_off, addr, nw);
00426 addr = (u_int8_t *)addr + nw;
00427 len -= nw;
00428 lp->b_off += nw;
00429
00430
00431 if (lp->b_off == bsize) {
00432 if ((ret = __log_write(dblp, dblp->bufp, bsize)) != 0)
00433 return (ret);
00434 lp->b_off = 0;
00435 ++lp->stat.st_wcount_fill;
00436 }
00437 }
00438 return (0);
00439 }
00440
00441
00442
00443
00444
00445 static int
00446 __log_write(dblp, addr, len)
00447 DB_LOG *dblp;
00448 void *addr;
00449 u_int32_t len;
00450 {
00451 LOG *lp;
00452 size_t nw;
00453 int ret;
00454
00455
00456
00457
00458
00459 lp = dblp->reginfo.primary;
00460 if (!F_ISSET(&dblp->lfh, DB_FH_VALID) || dblp->lfname != lp->lsn.file)
00461 if ((ret = __log_newfh(dblp)) != 0)
00462 return (ret);
00463
00464
00465
00466
00467
00468 if ((ret =
00469 CDB___os_seek(dblp->dbenv,
00470 &dblp->lfh, 0, 0, lp->w_off, 0, DB_OS_SEEK_SET)) != 0 ||
00471 (ret = CDB___os_write(dblp->dbenv, &dblp->lfh, addr, len, &nw)) != 0) {
00472 CDB___db_panic(dblp->dbenv, ret);
00473 return (ret);
00474 }
00475 if (nw != len) {
00476 CDB___db_err(dblp->dbenv, "Short write while writing log");
00477 return (EIO);
00478 }
00479
00480
00481 lp->w_off += len;
00482
00483
00484 if ((lp->stat.st_w_bytes += len) >= MEGABYTE) {
00485 lp->stat.st_w_bytes -= MEGABYTE;
00486 ++lp->stat.st_w_mbytes;
00487 }
00488 if ((lp->stat.st_wc_bytes += len) >= MEGABYTE) {
00489 lp->stat.st_wc_bytes -= MEGABYTE;
00490 ++lp->stat.st_wc_mbytes;
00491 }
00492 ++lp->stat.st_wcount;
00493
00494 return (0);
00495 }
00496
00497
00498
00499
00500
00501 int
00502 CDB_log_file(dbenv, lsn, namep, len)
00503 DB_ENV *dbenv;
00504 const DB_LSN *lsn;
00505 char *namep;
00506 size_t len;
00507 {
00508 DB_LOG *dblp;
00509 int ret;
00510 char *name;
00511
00512 #ifdef HAVE_RPC
00513 if (F_ISSET(dbenv, DB_ENV_RPCCLIENT))
00514 return (__dbcl_log_file(dbenv, lsn, namep, len));
00515 #endif
00516
00517 PANIC_CHECK(dbenv);
00518 ENV_REQUIRES_CONFIG(dbenv, dbenv->lg_handle, DB_INIT_LOG);
00519
00520 dblp = dbenv->lg_handle;
00521 R_LOCK(dbenv, &dblp->reginfo);
00522 ret = CDB___log_name(dblp, lsn->file, &name, NULL, 0);
00523 R_UNLOCK(dbenv, &dblp->reginfo);
00524 if (ret != 0)
00525 return (ret);
00526
00527
00528 if (len < strlen(name) + 1) {
00529 *namep = '\0';
00530 CDB___db_err(dbenv, "CDB_log_file: name buffer is too short");
00531 return (EINVAL);
00532 }
00533 (void)strcpy(namep, name);
00534 CDB___os_freestr(name);
00535
00536 return (0);
00537 }
00538
00539
00540
00541
00542
00543 static int
00544 __log_newfh(dblp)
00545 DB_LOG *dblp;
00546 {
00547 LOG *lp;
00548 int ret;
00549 char *name;
00550
00551
00552 if (F_ISSET(&dblp->lfh, DB_FH_VALID))
00553 (void)CDB___os_closehandle(&dblp->lfh);
00554
00555
00556 lp = dblp->reginfo.primary;
00557 dblp->lfname = lp->lsn.file;
00558
00559
00560
00561
00562
00563
00564
00565 if ((ret = CDB___log_name(dblp, dblp->lfname,
00566 &name, &dblp->lfh,
00567 DB_OSO_CREATE | DB_OSO_SEQ)) != 0)
00568 CDB___db_err(dblp->dbenv,
00569 "CDB_log_put: %s: %s", name, CDB_db_strerror(ret));
00570
00571 CDB___os_freestr(name);
00572 return (ret);
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582 int
00583 CDB___log_name(dblp, filenumber, namep, fhp, flags)
00584 DB_LOG *dblp;
00585 u_int32_t filenumber, flags;
00586 char **namep;
00587 DB_FH *fhp;
00588 {
00589 LOG *lp;
00590 int ret;
00591 char *oname;
00592 char old[sizeof(LFPREFIX) + 5 + 20], new[sizeof(LFPREFIX) + 10 + 20];
00593
00594 lp = dblp->reginfo.primary;
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614 (void)snprintf(new, sizeof(new), LFNAME, filenumber);
00615 if ((ret = CDB___db_appname(dblp->dbenv,
00616 DB_APP_LOG, NULL, new, 0, NULL, namep)) != 0 || fhp == NULL)
00617 return (ret);
00618
00619
00620 if ((ret = CDB___os_open(dblp->dbenv,
00621 *namep, flags, lp->persist.mode, fhp)) == 0)
00622 return (0);
00623
00624
00625
00626
00627
00628 if (!LF_ISSET(DB_OSO_RDONLY)) {
00629 CDB___db_err(dblp->dbenv,
00630 "%s: log file open failed: %s", *namep, CDB_db_strerror(ret));
00631 CDB___db_panic(dblp->dbenv, ret);
00632 return (ret);
00633 }
00634
00635
00636 (void)snprintf(old, sizeof(old), LFNAME_V1, filenumber);
00637 if ((ret = CDB___db_appname(dblp->dbenv,
00638 DB_APP_LOG, NULL, old, 0, NULL, &oname)) != 0)
00639 goto err;
00640
00641
00642
00643
00644
00645
00646 if ((ret = CDB___os_open(dblp->dbenv,
00647 oname, flags, lp->persist.mode, fhp)) == 0) {
00648 CDB___os_freestr(*namep);
00649 *namep = oname;
00650 return (0);
00651 }
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661 err: CDB___os_freestr(oname);
00662 return (ret);
00663 }
00664
00665 static int
00666 __log_open_files(dbenv)
00667 DB_ENV *dbenv;
00668 {
00669 DB_LOG *dblp;
00670 DB_LSN r_unused;
00671 DBT fid_dbt, t;
00672 FNAME *fnp;
00673 LOG *lp;
00674 int ret;
00675
00676 dblp = dbenv->lg_handle;
00677 lp = dblp->reginfo.primary;
00678
00679 for (fnp = SH_TAILQ_FIRST(&lp->fq, __fname);
00680 fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) {
00681 if (fnp->ref == 0)
00682 continue;
00683 if (fnp->name_off != INVALID_ROFF) {
00684 memset(&t, 0, sizeof(t));
00685 t.data = R_ADDR(&dblp->reginfo, fnp->name_off);
00686 t.size = strlen(t.data) + 1;
00687 }
00688 memset(&fid_dbt, 0, sizeof(fid_dbt));
00689 fid_dbt.data = fnp->ufid;
00690 fid_dbt.size = DB_FILE_ID_LEN;
00691 if ((ret = CDB___log_register_log(dbenv,
00692 NULL, &r_unused, 0, LOG_CHECKPOINT,
00693 fnp->name_off == INVALID_ROFF ? NULL : &t,
00694 &fid_dbt, fnp->id, fnp->s_type, fnp->meta_pgno)) != 0)
00695 return (ret);
00696 }
00697 return (0);
00698 }