1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1996,2008 Oracle. All rights reserved. 5 * 6 * $Id: dbreg.c,v 12.38 2008/03/12 20:46:37 mbrey Exp $ 7 */ 8 9#include "db_config.h" 10 11#include "db_int.h" 12#include "dbinc/db_page.h" 13#include "dbinc/log.h" 14#include "dbinc/txn.h" 15#include "dbinc/db_am.h" 16 17static int __dbreg_push_id __P((ENV *, int32_t)); 18static int __dbreg_pop_id __P((ENV *, int32_t *)); 19static int __dbreg_pluck_id __P((ENV *, int32_t)); 20 21/* 22 * The dbreg subsystem, as its name implies, registers database handles so 23 * that we can associate log messages with them without logging a filename 24 * or a full, unique DB ID. Instead, we assign each dbp an int32_t which is 25 * easy and cheap to log, and use this subsystem to map back and forth. 26 * 27 * Overview of how dbreg ids are managed: 28 * 29 * OPEN 30 * dbreg_setup (Creates FNAME struct.) 31 * dbreg_new_id (Assigns new ID to dbp and logs it. May be postponed 32 * until we attempt to log something else using that dbp, if the dbp 33 * was opened on a replication client.) 34 * 35 * CLOSE 36 * dbreg_close_id (Logs closure of dbp/revocation of ID.) 37 * dbreg_revoke_id (As name implies, revokes ID.) 38 * dbreg_teardown (Destroys FNAME.) 39 * 40 * RECOVERY 41 * dbreg_setup 42 * dbreg_assign_id (Assigns a particular ID we have in the log to a dbp.) 43 * 44 * sometimes: dbreg_revoke_id; dbreg_teardown 45 * other times: normal close path 46 * 47 * A note about locking: 48 * 49 * FNAME structures are referenced only by their corresponding dbp's 50 * until they have a valid id. 51 * 52 * Once they have a valid id, they must get linked into the log 53 * region list so they can get logged on checkpoints. 54 * 55 * An FNAME that may/does have a valid id must be accessed under 56 * protection of the mtx_filelist, with the following exception: 57 * 58 * We don't want to have to grab the mtx_filelist on every log 59 * record, and it should be safe not to do so when we're just 60 * looking at the id, because once allocated, the id should 61 * not change under a handle until the handle is closed. 62 * 63 * If a handle is closed during an attempt by another thread to 64 * log with it, well, the application doing the close deserves to 65 * go down in flames and a lot else is about to fail anyway. 66 * 67 * When in the course of logging we encounter an invalid id 68 * and go to allocate it lazily, we *do* need to check again 69 * after grabbing the mutex, because it's possible to race with 70 * another thread that has also decided that it needs to allocate 71 * a id lazily. 72 * 73 * See SR #5623 for further discussion of the new dbreg design. 74 */ 75 76/* 77 * __dbreg_setup -- 78 * Allocate and initialize an FNAME structure. The FNAME structures 79 * live in the log shared region and map one-to-one with open database handles. 80 * When the handle needs to be logged, the FNAME should have a valid fid 81 * allocated. If the handle currently isn't logged, it still has an FNAME 82 * entry. If we later discover that the handle needs to be logged, we can 83 * allocate a id for it later. (This happens when the handle is on a 84 * replication client that later becomes a master.) 85 * 86 * PUBLIC: int __dbreg_setup __P((DB *, const char *, const char *, u_int32_t)); 87 */ 88int 89__dbreg_setup(dbp, fname, dname, create_txnid) 90 DB *dbp; 91 const char *fname, *dname; 92 u_int32_t create_txnid; 93{ 94 DB_LOG *dblp; 95 ENV *env; 96 FNAME *fnp; 97 REGINFO *infop; 98 int ret; 99 size_t len; 100 void *p; 101 102 env = dbp->env; 103 dblp = env->lg_handle; 104 infop = &dblp->reginfo; 105 106 fnp = NULL; 107 p = NULL; 108 109 /* Allocate an FNAME and, if necessary, a buffer for the name itself. */ 110 LOG_SYSTEM_LOCK(env); 111 if ((ret = __env_alloc(infop, sizeof(FNAME), &fnp)) != 0) 112 goto err; 113 memset(fnp, 0, sizeof(FNAME)); 114 if (fname == NULL) 115 fnp->fname_off = INVALID_ROFF; 116 else { 117 len = strlen(fname) + 1; 118 if ((ret = __env_alloc(infop, len, &p)) != 0) 119 goto err; 120 fnp->fname_off = R_OFFSET(infop, p); 121 memcpy(p, fname, len); 122 } 123 if (dname == NULL) 124 fnp->dname_off = INVALID_ROFF; 125 else { 126 len = strlen(dname) + 1; 127 if ((ret = __env_alloc(infop, len, &p)) != 0) 128 goto err; 129 fnp->dname_off = R_OFFSET(infop, p); 130 memcpy(p, dname, len); 131 } 132 LOG_SYSTEM_UNLOCK(env); 133 134 /* 135 * Fill in all the remaining info that we'll need later to register 136 * the file, if we use it for logging. 137 */ 138 fnp->id = fnp->old_id = DB_LOGFILEID_INVALID; 139 fnp->s_type = dbp->type; 140 memcpy(fnp->ufid, dbp->fileid, DB_FILE_ID_LEN); 141 fnp->meta_pgno = dbp->meta_pgno; 142 fnp->create_txnid = create_txnid; 143 dbp->dbenv->thread_id(dbp->dbenv, &fnp->pid, NULL); 144 145 if (F_ISSET(dbp, DB_AM_INMEM)) 146 F_SET(fnp, DB_FNAME_INMEM); 147 if (F_ISSET(dbp, DB_AM_RECOVER)) 148 F_SET(fnp, DB_FNAME_RECOVER); 149 fnp->txn_ref = 1; 150 fnp->mutex = dbp->mutex; 151 152 dbp->log_filename = fnp; 153 154 return (0); 155 156err: LOG_SYSTEM_UNLOCK(env); 157 if (ret == ENOMEM) 158 __db_errx(env, 159 "Logging region out of memory; you may need to increase its size"); 160 161 return (ret); 162} 163 164/* 165 * __dbreg_teardown -- 166 * Destroy a DB handle's FNAME struct. This is only called when closing 167 * the DB. 168 * 169 * PUBLIC: int __dbreg_teardown __P((DB *)); 170 */ 171int 172__dbreg_teardown(dbp) 173 DB *dbp; 174{ 175 int ret; 176 177 /* 178 * We may not have an FNAME if we were never opened. This is not an 179 * error. 180 */ 181 if (dbp->log_filename == NULL) 182 return (0); 183 184 ret = __dbreg_teardown_int(dbp->env, dbp->log_filename); 185 186 /* We freed the copy of the mutex from the FNAME. */ 187 dbp->log_filename = NULL; 188 dbp->mutex = MUTEX_INVALID; 189 190 return (ret); 191} 192 193/* 194 * __dbreg_teardown_int -- 195 * Destroy an FNAME struct. 196 * 197 * PUBLIC: int __dbreg_teardown_int __P((ENV *, FNAME *)); 198 */ 199int 200__dbreg_teardown_int(env, fnp) 201 ENV *env; 202 FNAME *fnp; 203{ 204 DB_LOG *dblp; 205 REGINFO *infop; 206 int ret; 207 208 if (F_ISSET(fnp, DB_FNAME_NOTLOGGED)) 209 return (0); 210 dblp = env->lg_handle; 211 infop = &dblp->reginfo; 212 213 DB_ASSERT(env, fnp->id == DB_LOGFILEID_INVALID); 214 ret = __mutex_free(env, &fnp->mutex); 215 216 LOG_SYSTEM_LOCK(env); 217 if (fnp->fname_off != INVALID_ROFF) 218 __env_alloc_free(infop, R_ADDR(infop, fnp->fname_off)); 219 if (fnp->dname_off != INVALID_ROFF) 220 __env_alloc_free(infop, R_ADDR(infop, fnp->dname_off)); 221 __env_alloc_free(infop, fnp); 222 LOG_SYSTEM_UNLOCK(env); 223 224 return (ret); 225} 226 227/* 228 * __dbreg_new_id -- 229 * Get an unused dbreg id to this database handle. 230 * Used as a wrapper to acquire the mutex and 231 * only set the id on success. 232 * 233 * PUBLIC: int __dbreg_new_id __P((DB *, DB_TXN *)); 234 */ 235int 236__dbreg_new_id(dbp, txn) 237 DB *dbp; 238 DB_TXN *txn; 239{ 240 DB_LOG *dblp; 241 ENV *env; 242 FNAME *fnp; 243 LOG *lp; 244 int32_t id; 245 int ret; 246 247 env = dbp->env; 248 dblp = env->lg_handle; 249 lp = dblp->reginfo.primary; 250 fnp = dbp->log_filename; 251 252 /* The mtx_filelist protects the FNAME list and id management. */ 253 MUTEX_LOCK(env, lp->mtx_filelist); 254 if (fnp->id != DB_LOGFILEID_INVALID) { 255 MUTEX_UNLOCK(env, lp->mtx_filelist); 256 return (0); 257 } 258 if ((ret = __dbreg_get_id(dbp, txn, &id)) == 0) 259 fnp->id = id; 260 MUTEX_UNLOCK(env, lp->mtx_filelist); 261 return (ret); 262} 263 264/* 265 * __dbreg_get_id -- 266 * Assign an unused dbreg id to this database handle. 267 * Assume the caller holds the mtx_filelist locked. Assume the 268 * caller will set the fnp->id field with the id we return. 269 * 270 * PUBLIC: int __dbreg_get_id __P((DB *, DB_TXN *, int32_t *)); 271 */ 272int 273__dbreg_get_id(dbp, txn, idp) 274 DB *dbp; 275 DB_TXN *txn; 276 int32_t *idp; 277{ 278 DB_LOG *dblp; 279 ENV *env; 280 FNAME *fnp; 281 LOG *lp; 282 int32_t id; 283 int ret; 284 285 env = dbp->env; 286 dblp = env->lg_handle; 287 lp = dblp->reginfo.primary; 288 fnp = dbp->log_filename; 289 290 /* 291 * It's possible that after deciding we needed to call this function, 292 * someone else allocated an ID before we grabbed the lock. Check 293 * to make sure there was no race and we have something useful to do. 294 */ 295 /* Get an unused ID from the free list. */ 296 if ((ret = __dbreg_pop_id(env, &id)) != 0) 297 goto err; 298 299 /* If no ID was found, allocate a new one. */ 300 if (id == DB_LOGFILEID_INVALID) 301 id = lp->fid_max++; 302 303 /* If the file is durable (i.e., not, not-durable), mark it as such. */ 304 if (!F_ISSET(dbp, DB_AM_NOT_DURABLE)) 305 F_SET(fnp, DB_FNAME_DURABLE); 306 307 /* Hook the FNAME into the list of open files. */ 308 SH_TAILQ_INSERT_HEAD(&lp->fq, fnp, q, __fname); 309 310 /* 311 * Log the registry. We should only request a new ID in situations 312 * where logging is reasonable. 313 */ 314 DB_ASSERT(env, !F_ISSET(dbp, DB_AM_RECOVER)); 315 316 if ((ret = __dbreg_log_id(dbp, txn, id, 0)) != 0) 317 goto err; 318 319 /* 320 * Once we log the create_txnid, we need to make sure we never 321 * log it again (as might happen if this is a replication client 322 * that later upgrades to a master). 323 */ 324 fnp->create_txnid = TXN_INVALID; 325 326 DB_ASSERT(env, dbp->type == fnp->s_type); 327 DB_ASSERT(env, dbp->meta_pgno == fnp->meta_pgno); 328 329 if ((ret = __dbreg_add_dbentry(env, dblp, dbp, id)) != 0) 330 goto err; 331 /* 332 * If we have a successful call, set the ID. Otherwise 333 * we have to revoke it and remove it from all the lists 334 * it has been added to, and return an invalid id. 335 */ 336err: 337 if (ret != 0 && id != DB_LOGFILEID_INVALID) { 338 (void)__dbreg_revoke_id(dbp, 1, id); 339 id = DB_LOGFILEID_INVALID; 340 } 341 *idp = id; 342 return (ret); 343} 344 345/* 346 * __dbreg_assign_id -- 347 * Assign a particular dbreg id to this database handle. 348 * 349 * PUBLIC: int __dbreg_assign_id __P((DB *, int32_t)); 350 */ 351int 352__dbreg_assign_id(dbp, id) 353 DB *dbp; 354 int32_t id; 355{ 356 DB *close_dbp; 357 DB_LOG *dblp; 358 ENV *env; 359 FNAME *close_fnp, *fnp; 360 LOG *lp; 361 int ret; 362 363 env = dbp->env; 364 dblp = env->lg_handle; 365 lp = dblp->reginfo.primary; 366 fnp = dbp->log_filename; 367 368 close_dbp = NULL; 369 close_fnp = NULL; 370 371 /* The mtx_filelist protects the FNAME list and id management. */ 372 MUTEX_LOCK(env, lp->mtx_filelist); 373 374 /* We should only call this on DB handles that have no ID. */ 375 DB_ASSERT(env, fnp->id == DB_LOGFILEID_INVALID); 376 377 /* 378 * Make sure there isn't already a file open with this ID. There can 379 * be in recovery, if we're recovering across a point where an ID got 380 * reused. 381 */ 382 if (__dbreg_id_to_fname(dblp, id, 1, &close_fnp) == 0) { 383 /* 384 * We want to save off any dbp we have open with this id. We 385 * can't safely close it now, because we hold the mtx_filelist, 386 * but we should be able to rely on it being open in this 387 * process, and we're running recovery, so no other thread 388 * should muck with it if we just put off closing it until 389 * we're ready to return. 390 * 391 * Once we have the dbp, revoke its id; we're about to 392 * reuse it. 393 */ 394 ret = __dbreg_id_to_db(env, NULL, &close_dbp, id, 0); 395 if (ret == ENOENT) { 396 ret = 0; 397 goto cont; 398 } else if (ret != 0) 399 goto err; 400 401 if ((ret = __dbreg_revoke_id(close_dbp, 1, 402 DB_LOGFILEID_INVALID)) != 0) 403 goto err; 404 } 405 406 /* 407 * Remove this ID from the free list, if it's there, and make sure 408 * we don't allocate it anew. 409 */ 410cont: if ((ret = __dbreg_pluck_id(env, id)) != 0) 411 goto err; 412 if (id >= lp->fid_max) 413 lp->fid_max = id + 1; 414 415 /* Now go ahead and assign the id to our dbp. */ 416 fnp->id = id; 417 /* If the file is durable (i.e., not, not-durable), mark it as such. */ 418 if (!F_ISSET(dbp, DB_AM_NOT_DURABLE)) 419 F_SET(fnp, DB_FNAME_DURABLE); 420 SH_TAILQ_INSERT_HEAD(&lp->fq, fnp, q, __fname); 421 422 /* 423 * If we get an error adding the dbentry, revoke the id. 424 * We void the return value since we want to retain and 425 * return the original error in ret anyway. 426 */ 427 if ((ret = __dbreg_add_dbentry(env, dblp, dbp, id)) != 0) 428 (void)__dbreg_revoke_id(dbp, 1, id); 429 430err: MUTEX_UNLOCK(env, lp->mtx_filelist); 431 432 /* There's nothing useful that our caller can do if this close fails. */ 433 if (close_dbp != NULL) 434 (void)__db_close(close_dbp, NULL, DB_NOSYNC); 435 436 return (ret); 437} 438 439/* 440 * __dbreg_revoke_id -- 441 * Take a log id away from a dbp, in preparation for closing it, 442 * but without logging the close. 443 * 444 * PUBLIC: int __dbreg_revoke_id __P((DB *, int, int32_t)); 445 */ 446int 447__dbreg_revoke_id(dbp, have_lock, force_id) 448 DB *dbp; 449 int have_lock; 450 int32_t force_id; 451{ 452 DB_REP *db_rep; 453 ENV *env; 454 int push; 455 456 env = dbp->env; 457 458 /* 459 * If we are not in recovery but the file was opened for a recovery 460 * operation, then this process aborted a transaction for another 461 * process and the id may still be in use, so don't reuse this id. 462 * If our fid generation in replication has changed, this fid 463 * should not be reused 464 */ 465 db_rep = env->rep_handle; 466 push = (!F_ISSET(dbp, DB_AM_RECOVER) || IS_RECOVERING(env)) && 467 (!REP_ON(env) || ((REP *)db_rep->region)->gen == dbp->fid_gen); 468 469 return (__dbreg_revoke_id_int(dbp->env, 470 dbp->log_filename, have_lock, push, force_id)); 471} 472/* 473 * __dbreg_revoke_id_int -- 474 * Revoke a log, in preparation for closing it, but without logging 475 * the close. 476 * 477 * PUBLIC: int __dbreg_revoke_id_int 478 * PUBLIC: __P((ENV *, FNAME *, int, int, int32_t)); 479 */ 480int 481__dbreg_revoke_id_int(env, fnp, have_lock, push, force_id) 482 ENV *env; 483 FNAME *fnp; 484 int have_lock, push; 485 int32_t force_id; 486{ 487 DB_LOG *dblp; 488 LOG *lp; 489 int32_t id; 490 int ret; 491 492 dblp = env->lg_handle; 493 lp = dblp->reginfo.primary; 494 ret = 0; 495 496 /* If we lack an ID, this is a null-op. */ 497 if (fnp == NULL) 498 return (0); 499 500 /* 501 * If we have a force_id, we had an error after allocating 502 * the id, and putting it on the fq list, but before we 503 * finished setting up fnp. So, if we have a force_id use it. 504 */ 505 if (force_id != DB_LOGFILEID_INVALID) 506 id = force_id; 507 else if (fnp->id == DB_LOGFILEID_INVALID) { 508 if (fnp->old_id == DB_LOGFILEID_INVALID) 509 return (0); 510 id = fnp->old_id; 511 } else 512 id = fnp->id; 513 if (!have_lock) 514 MUTEX_LOCK(env, lp->mtx_filelist); 515 516 fnp->id = DB_LOGFILEID_INVALID; 517 fnp->old_id = DB_LOGFILEID_INVALID; 518 519 /* Remove the FNAME from the list of open files. */ 520 SH_TAILQ_REMOVE(&lp->fq, fnp, q, __fname); 521 522 /* 523 * This FNAME may be for a DBP which is already closed. Its ID may 524 * still be in use by an aborting transaction. If not, 525 * remove this id from the dbentry table and push it onto the 526 * free list. 527 */ 528 if (!F_ISSET(fnp, DB_FNAME_CLOSED) && 529 (ret = __dbreg_rem_dbentry(dblp, id)) == 0 && push) 530 ret = __dbreg_push_id(env, id); 531 532 if (!have_lock) 533 MUTEX_UNLOCK(env, lp->mtx_filelist); 534 return (ret); 535} 536 537/* 538 * __dbreg_close_id -- 539 * Take a dbreg id away from a dbp that we're closing, and log 540 * the unregistry if the refcount goes to 0. 541 * 542 * PUBLIC: int __dbreg_close_id __P((DB *, DB_TXN *, u_int32_t)); 543 */ 544int 545__dbreg_close_id(dbp, txn, op) 546 DB *dbp; 547 DB_TXN *txn; 548 u_int32_t op; 549{ 550 DB_LOG *dblp; 551 ENV *env; 552 FNAME *fnp; 553 LOG *lp; 554 int ret, t_ret; 555 556 env = dbp->env; 557 dblp = env->lg_handle; 558 lp = dblp->reginfo.primary; 559 fnp = dbp->log_filename; 560 561 /* If we lack an ID, this is a null-op. */ 562 if (fnp == NULL) 563 return (0); 564 565 if (fnp->id == DB_LOGFILEID_INVALID) { 566 ret = __dbreg_revoke_id(dbp, 0, DB_LOGFILEID_INVALID); 567 goto done; 568 } 569 570 /* 571 * If we are the last reference to this db then we need to log it 572 * as closed. Otherwise the last transaction will do the logging. 573 * Remove the DBP from the db entry table since it can nolonger 574 * be used. If we abort it will have to be reopened. 575 */ 576 ret = 0; 577 DB_ASSERT(env, fnp->txn_ref > 0); 578 if (fnp->txn_ref > 1) { 579 MUTEX_LOCK(env, dbp->mutex); 580 if (fnp->txn_ref > 1) { 581 if (!F_ISSET(fnp, DB_FNAME_CLOSED) && 582 (t_ret = __dbreg_rem_dbentry( 583 env->lg_handle, fnp->id)) != 0 && ret == 0) 584 ret = t_ret; 585 586 /* 587 * The DB handle has been closed in the logging system. 588 * Transactions may still have a ref to this name. 589 * Mark it so that if recovery reopens the file id 590 * the transaction will not close the wrong handle. 591 */ 592 F_SET(fnp, DB_FNAME_CLOSED); 593 fnp->txn_ref--; 594 MUTEX_UNLOCK(env, dbp->mutex); 595 /* The mutex now lives only in the FNAME. */ 596 dbp->mutex = MUTEX_INVALID; 597 dbp->log_filename = NULL; 598 goto no_log; 599 } 600 } 601 MUTEX_LOCK(env, lp->mtx_filelist); 602 603 if ((ret = __dbreg_log_close(env, fnp, txn, op)) != 0) 604 goto err; 605 ret = __dbreg_revoke_id(dbp, 1, DB_LOGFILEID_INVALID); 606 607err: MUTEX_UNLOCK(env, lp->mtx_filelist); 608 609done: if ((t_ret = __dbreg_teardown(dbp)) != 0 && ret == 0) 610 ret = t_ret; 611no_log: 612 return (ret); 613} 614/* 615 * __dbreg_close_id_int -- 616 * Close down a dbreg id and log the unregistry. This is called only 617 * when a transaction has the last ref to the fname. 618 * 619 * PUBLIC: int __dbreg_close_id_int __P((ENV *, FNAME *, u_int32_t, int)); 620 */ 621int 622__dbreg_close_id_int(env, fnp, op, locked) 623 ENV *env; 624 FNAME *fnp; 625 u_int32_t op; 626 int locked; 627{ 628 DB_LOG *dblp; 629 LOG *lp; 630 int ret, t_ret; 631 632 DB_ASSERT(env, fnp->txn_ref == 1); 633 dblp = env->lg_handle; 634 lp = dblp->reginfo.primary; 635 636 if (fnp->id == DB_LOGFILEID_INVALID) 637 return (__dbreg_revoke_id_int(env, 638 fnp, locked, 1, DB_LOGFILEID_INVALID)); 639 640 if (F_ISSET(fnp, DB_FNAME_RECOVER)) 641 return (__dbreg_close_file(env, fnp)); 642 /* 643 * If log_close fails then it will mark the name DB_FNAME_NOTLOGGED 644 * and the id must persist. 645 */ 646 if (!locked) 647 MUTEX_LOCK(env, lp->mtx_filelist); 648 if ((ret = __dbreg_log_close(env, fnp, NULL, op)) != 0) 649 goto err; 650 651 ret = __dbreg_revoke_id_int(env, fnp, 1, 1, DB_LOGFILEID_INVALID); 652 653err: if (!locked) 654 MUTEX_UNLOCK(env, lp->mtx_filelist); 655 656 if ((t_ret = __dbreg_teardown_int(env, fnp)) != 0 && ret == 0) 657 ret = t_ret; 658 return (ret); 659} 660 661/* 662 * __dbreg_failchk -- 663 * 664 * Look for entries that belong to dead processes and either close them 665 * out or, if there are pending transactions, just remove the mutex which 666 * will get discarded later. 667 * 668 * PUBLIC: int __dbreg_failchk __P((ENV *)); 669 */ 670int 671__dbreg_failchk(env) 672 ENV *env; 673{ 674 DB_ENV *dbenv; 675 DB_LOG *dblp; 676 FNAME *fnp, *nnp; 677 LOG *lp; 678 int ret, t_ret; 679 char buf[DB_THREADID_STRLEN]; 680 681 if ((dblp = env->lg_handle) == NULL) 682 return (0); 683 684 lp = dblp->reginfo.primary; 685 dbenv = env->dbenv; 686 ret = 0; 687 688 MUTEX_LOCK(env, lp->mtx_filelist); 689 for (fnp = SH_TAILQ_FIRST(&lp->fq, __fname); fnp != NULL; fnp = nnp) { 690 nnp = SH_TAILQ_NEXT(fnp, q, __fname); 691 if (dbenv->is_alive(dbenv, fnp->pid, 0, DB_MUTEX_PROCESS_ONLY)) 692 continue; 693 MUTEX_LOCK(env, fnp->mutex); 694 __db_msg(env, 695 "Freeing log information for process: %s, (ref %lu)", 696 dbenv->thread_id_string(dbenv, fnp->pid, 0, buf), 697 (u_long)fnp->txn_ref); 698 if (fnp->txn_ref > 1 || F_ISSET(fnp, DB_FNAME_CLOSED)) { 699 if (!F_ISSET(fnp, DB_FNAME_CLOSED)) { 700 fnp->txn_ref--; 701 F_SET(fnp, DB_FNAME_CLOSED); 702 } 703 MUTEX_UNLOCK(env, fnp->mutex); 704 fnp->mutex = MUTEX_INVALID; 705 fnp->pid = 0; 706 } else { 707 F_SET(fnp, DB_FNAME_CLOSED); 708 if ((t_ret = __dbreg_close_id_int(env, 709 fnp, DBREG_CLOSE, 1)) && ret == 0) 710 ret = t_ret; 711 } 712 } 713 714 MUTEX_UNLOCK(env, lp->mtx_filelist); 715 return (ret); 716} 717/* 718 * __dbreg_log_close -- 719 * 720 * Log a close of a database. Called when closing a file or when a 721 * replication client is becoming a master. That closes all the 722 * files it previously had open. 723 * 724 * Assumes caller holds the lp->mutex_filelist lock already. 725 * 726 * PUBLIC: int __dbreg_log_close __P((ENV *, FNAME *, 727 * PUBLIC: DB_TXN *, u_int32_t)); 728 */ 729int 730__dbreg_log_close(env, fnp, txn, op) 731 ENV *env; 732 FNAME *fnp; 733 DB_TXN *txn; 734 u_int32_t op; 735{ 736 DBT fid_dbt, r_name, *dbtp; 737 DB_LOG *dblp; 738 DB_LSN r_unused; 739 int ret; 740 741 dblp = env->lg_handle; 742 ret = 0; 743 744 if (fnp->fname_off == INVALID_ROFF) 745 dbtp = NULL; 746 else { 747 memset(&r_name, 0, sizeof(r_name)); 748 r_name.data = R_ADDR(&dblp->reginfo, fnp->fname_off); 749 r_name.size = (u_int32_t)strlen((char *)r_name.data) + 1; 750 dbtp = &r_name; 751 } 752 memset(&fid_dbt, 0, sizeof(fid_dbt)); 753 fid_dbt.data = fnp->ufid; 754 fid_dbt.size = DB_FILE_ID_LEN; 755 if ((ret = __dbreg_register_log(env, txn, &r_unused, 756 F_ISSET(fnp, DB_FNAME_DURABLE) ? 0 : DB_LOG_NOT_DURABLE, 757 op, dbtp, &fid_dbt, fnp->id, 758 fnp->s_type, fnp->meta_pgno, TXN_INVALID)) != 0) { 759 /* 760 * We are trying to close, but the log write failed. 761 * Unfortunately, close needs to plow forward, because 762 * the application can't do anything with the handle. 763 * Make the entry in the shared memory region so that 764 * when we close the environment, we know that this 765 * happened. Also, make sure we remove this from the 766 * per-process table, so that we don't try to close it 767 * later. 768 */ 769 F_SET(fnp, DB_FNAME_NOTLOGGED); 770 (void)__dbreg_rem_dbentry(dblp, fnp->id); 771 } 772 return (ret); 773} 774 775/* 776 * __dbreg_push_id and __dbreg_pop_id -- 777 * Dbreg ids from closed files are kept on a stack in shared memory 778 * for recycling. (We want to reuse them as much as possible because each 779 * process keeps open files in an array by ID.) Push them to the stack and 780 * pop them from it, managing memory as appropriate. 781 * 782 * The stack is protected by the mtx_filelist, and both functions assume it 783 * is already locked. 784 */ 785static int 786__dbreg_push_id(env, id) 787 ENV *env; 788 int32_t id; 789{ 790 DB_LOG *dblp; 791 LOG *lp; 792 REGINFO *infop; 793 int32_t *stack, *newstack; 794 int ret; 795 796 dblp = env->lg_handle; 797 infop = &dblp->reginfo; 798 lp = infop->primary; 799 800 if (id == lp->fid_max - 1) { 801 lp->fid_max--; 802 return (0); 803 } 804 805 /* Check if we have room on the stack. */ 806 if (lp->free_fid_stack == INVALID_ROFF || 807 lp->free_fids_alloced <= lp->free_fids + 1) { 808 LOG_SYSTEM_LOCK(env); 809 if ((ret = __env_alloc(infop, 810 (lp->free_fids_alloced + 20) * sizeof(u_int32_t), 811 &newstack)) != 0) { 812 LOG_SYSTEM_UNLOCK(env); 813 return (ret); 814 } 815 816 if (lp->free_fid_stack != INVALID_ROFF) { 817 stack = R_ADDR(infop, lp->free_fid_stack); 818 memcpy(newstack, stack, 819 lp->free_fids_alloced * sizeof(u_int32_t)); 820 __env_alloc_free(infop, stack); 821 } 822 lp->free_fid_stack = R_OFFSET(infop, newstack); 823 lp->free_fids_alloced += 20; 824 LOG_SYSTEM_UNLOCK(env); 825 } 826 827 stack = R_ADDR(infop, lp->free_fid_stack); 828 stack[lp->free_fids++] = id; 829 return (0); 830} 831 832static int 833__dbreg_pop_id(env, id) 834 ENV *env; 835 int32_t *id; 836{ 837 DB_LOG *dblp; 838 LOG *lp; 839 int32_t *stack; 840 841 dblp = env->lg_handle; 842 lp = dblp->reginfo.primary; 843 844 /* Do we have anything to pop? */ 845 if (lp->free_fid_stack != INVALID_ROFF && lp->free_fids > 0) { 846 stack = R_ADDR(&dblp->reginfo, lp->free_fid_stack); 847 *id = stack[--lp->free_fids]; 848 } else 849 *id = DB_LOGFILEID_INVALID; 850 851 return (0); 852} 853 854/* 855 * __dbreg_pluck_id -- 856 * Remove a particular dbreg id from the stack of free ids. This is 857 * used when we open a file, as in recovery, with a specific ID that might 858 * be on the stack. 859 * 860 * Returns success whether or not the particular id was found, and like 861 * push and pop, assumes that the mtx_filelist is locked. 862 */ 863static int 864__dbreg_pluck_id(env, id) 865 ENV *env; 866 int32_t id; 867{ 868 DB_LOG *dblp; 869 LOG *lp; 870 int32_t *stack; 871 u_int i; 872 873 dblp = env->lg_handle; 874 lp = dblp->reginfo.primary; 875 876 if (id >= lp->fid_max) 877 return (0); 878 879 /* Do we have anything to look at? */ 880 if (lp->free_fid_stack != INVALID_ROFF) { 881 stack = R_ADDR(&dblp->reginfo, lp->free_fid_stack); 882 for (i = 0; i < lp->free_fids; i++) 883 if (id == stack[i]) { 884 /* 885 * Found it. Overwrite it with the top 886 * id (which may harmlessly be itself), 887 * and shorten the stack by one. 888 */ 889 stack[i] = stack[lp->free_fids - 1]; 890 lp->free_fids--; 891 return (0); 892 } 893 } 894 895 return (0); 896} 897 898/* 899 * __dbreg_log_id -- 900 * Used for in-memory named files. They are created in mpool and 901 * are given id's early in the open process so that we can read and 902 * create pages in the mpool for the files. However, at the time that 903 * the mpf is created, the file may not be fully created and/or its 904 * meta-data may not be fully known, so we can't do a full dbregister. 905 * This is a routine exported that will log a complete dbregister 906 * record that will allow for both recovery and replication. 907 * 908 * PUBLIC: int __dbreg_log_id __P((DB *, DB_TXN *, int32_t, int)); 909 */ 910int 911__dbreg_log_id(dbp, txn, id, needlock) 912 DB *dbp; 913 DB_TXN *txn; 914 int32_t id; 915 int needlock; 916{ 917 DBT fid_dbt, r_name; 918 DB_LOG *dblp; 919 DB_LSN unused; 920 ENV *env; 921 FNAME *fnp; 922 LOG *lp; 923 u_int32_t op; 924 int ret; 925 926 env = dbp->env; 927 dblp = env->lg_handle; 928 lp = dblp->reginfo.primary; 929 fnp = dbp->log_filename; 930 931 /* Verify that the fnp has been initialized. */ 932 if (fnp->s_type == DB_UNKNOWN) { 933 memcpy(fnp->ufid, dbp->fileid, DB_FILE_ID_LEN); 934 fnp->s_type = dbp->type; 935 } 936 937 /* 938 * Log the registry. We should only request a new ID in situations 939 * where logging is reasonable. 940 */ 941 memset(&fid_dbt, 0, sizeof(fid_dbt)); 942 memset(&r_name, 0, sizeof(r_name)); 943 944 if (needlock) 945 MUTEX_LOCK(env, lp->mtx_filelist); 946 947 if (fnp->fname_off != INVALID_ROFF) { 948 r_name.data = R_ADDR(&dblp->reginfo, fnp->fname_off); 949 r_name.size = (u_int32_t)strlen((char *)r_name.data) + 1; 950 } 951 952 fid_dbt.data = dbp->fileid; 953 fid_dbt.size = DB_FILE_ID_LEN; 954 955 op = !F_ISSET(dbp, DB_AM_OPEN_CALLED) ? DBREG_PREOPEN : 956 (F_ISSET(dbp, DB_AM_INMEM) ? DBREG_REOPEN : DBREG_OPEN); 957 ret = __dbreg_register_log(env, txn, &unused, 958 F_ISSET(dbp, DB_AM_NOT_DURABLE) ? DB_LOG_NOT_DURABLE : 0, 959 op, r_name.size == 0 ? NULL : &r_name, &fid_dbt, id, 960 fnp->s_type, fnp->meta_pgno, fnp->create_txnid); 961 962 if (needlock) 963 MUTEX_UNLOCK(env, lp->mtx_filelist); 964 965 return (ret); 966} 967