📄 dbreg.c
字号:
* * PUBLIC: int __dbreg_assign_id __P((DB *, int32_t)); */int__dbreg_assign_id(dbp, id) DB *dbp; int32_t id;{ DB *close_dbp; DB_ENV *dbenv; DB_LOG *dblp; FNAME *close_fnp, *fnp; LOG *lp; int ret; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; fnp = dbp->log_filename; close_dbp = NULL; close_fnp = NULL; /* The fq_mutex protects the FNAME list and id management. */ MUTEX_LOCK(dbenv, &lp->fq_mutex); /* We should only call this on DB handles that have no ID. */ DB_ASSERT(fnp->id == DB_LOGFILEID_INVALID); /* * Make sure there isn't already a file open with this ID. There can * be in recovery, if we're recovering across a point where an ID got * reused. */ if (__dbreg_id_to_fname(dblp, id, 1, &close_fnp) == 0) { /* * We want to save off any dbp we have open with this id. * We can't safely close it now, because we hold the fq_mutex, * but we should be able to rely on it being open in this * process, and we're running recovery, so no other thread * should muck with it if we just put off closing it until * we're ready to return. * * Once we have the dbp, revoke its id; we're about to * reuse it. */ ret = __dbreg_id_to_db_int(dbenv, NULL, &close_dbp, id, 0, 0); if (ret == ENOENT) { ret = 0; goto cont; } else if (ret != 0) goto err; if ((ret = __dbreg_revoke_id(close_dbp, 1, DB_LOGFILEID_INVALID)) != 0) goto err; } /* * Remove this ID from the free list, if it's there, and make sure * we don't allocate it anew. */cont: if ((ret = __dbreg_pluck_id(dbenv, id)) != 0) goto err; if (id >= lp->fid_max) lp->fid_max = id + 1; /* Now go ahead and assign the id to our dbp. */ fnp->id = id; fnp->is_durable = !F_ISSET(dbp, DB_AM_NOT_DURABLE); SH_TAILQ_INSERT_HEAD(&lp->fq, fnp, q, __fname); /* * If we get an error adding the dbentry, revoke the id. * We void the return value since we want to retain and * return the original error in ret anyway. */ if ((ret = __dbreg_add_dbentry(dbenv, dblp, dbp, id)) != 0) (void)__dbreg_revoke_id(dbp, 1, id);err: MUTEX_UNLOCK(dbenv, &lp->fq_mutex); /* There's nothing useful that our caller can do if this close fails. */ if (close_dbp != NULL) (void)__db_close(close_dbp, NULL, DB_NOSYNC); return (ret);}/* * __dbreg_revoke_id -- * Take a log id away from a dbp, in preparation for closing it, * but without logging the close. * * PUBLIC: int __dbreg_revoke_id __P((DB *, int, int32_t)); */int__dbreg_revoke_id(dbp, have_lock, force_id) DB *dbp; int have_lock; int32_t force_id;{ DB_ENV *dbenv; DB_LOG *dblp; FNAME *fnp; LOG *lp; int32_t id; int ret; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; fnp = dbp->log_filename; /* If we lack an ID, this is a null-op. */ if (fnp == NULL) return (0); /* * If we have a force_id, we had an error after allocating * the id, and putting it on the fq list, but before we * finished setting up fnp. So, if we have a force_id use it. */ if (force_id != DB_LOGFILEID_INVALID) id = force_id; else if (fnp->id == DB_LOGFILEID_INVALID) return (0); else id = fnp->id; if (!have_lock) MUTEX_LOCK(dbenv, &lp->fq_mutex); fnp->id = DB_LOGFILEID_INVALID; /* Remove the FNAME from the list of open files. */ SH_TAILQ_REMOVE(&lp->fq, fnp, q, __fname); /* Remove this id from the dbentry table. */ __dbreg_rem_dbentry(dblp, id); /* Push this id onto the free list. */ ret = __dbreg_push_id(dbenv, id); if (!have_lock) MUTEX_UNLOCK(dbenv, &lp->fq_mutex); return (ret);}/* * __dbreg_close_id -- * Take a dbreg id away from a dbp that we're closing, and log * the unregistry. * * PUBLIC: int __dbreg_close_id __P((DB *, DB_TXN *, u_int32_t)); */int__dbreg_close_id(dbp, txn, op) DB *dbp; DB_TXN *txn; u_int32_t op;{ DBT fid_dbt, r_name, *dbtp; DB_ENV *dbenv; DB_LOG *dblp; DB_LSN r_unused; FNAME *fnp; LOG *lp; int ret; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; fnp = dbp->log_filename; /* If we lack an ID, this is a null-op. */ if (fnp == NULL || fnp->id == DB_LOGFILEID_INVALID) return (0); MUTEX_LOCK(dbenv, &lp->fq_mutex); if (fnp->name_off == INVALID_ROFF) dbtp = NULL; else { memset(&r_name, 0, sizeof(r_name)); r_name.data = R_ADDR(&dblp->reginfo, fnp->name_off); r_name.size = (u_int32_t)strlen((char *)r_name.data) + 1; dbtp = &r_name; } memset(&fid_dbt, 0, sizeof(fid_dbt)); fid_dbt.data = fnp->ufid; fid_dbt.size = DB_FILE_ID_LEN; if ((ret = __dbreg_register_log(dbenv, txn, &r_unused, F_ISSET(dbp, DB_AM_NOT_DURABLE) ? DB_LOG_NOT_DURABLE : 0, op, dbtp, &fid_dbt, fnp->id, fnp->s_type, fnp->meta_pgno, TXN_INVALID)) != 0) goto err; ret = __dbreg_revoke_id(dbp, 1, DB_LOGFILEID_INVALID);err: MUTEX_UNLOCK(dbenv, &lp->fq_mutex); return (ret);}/* * __dbreg_push_id and __dbreg_pop_id -- * Dbreg ids from closed files are kept on a stack in shared memory * for recycling. (We want to reuse them as much as possible because each * process keeps open files in an array by ID.) Push them to the stack and * pop them from it, managing memory as appropriate. * * The stack is protected by the fq_mutex, and in both functions we assume * that this is already locked. */static int__dbreg_push_id(dbenv, id) DB_ENV *dbenv; int32_t id;{ DB_LOG *dblp; LOG *lp; REGINFO *infop; int32_t *stack, *newstack; int ret; dblp = dbenv->lg_handle; infop = &dblp->reginfo; lp = infop->primary; if (lp->free_fid_stack == INVALID_ROFF) { stack = NULL; DB_ASSERT(lp->free_fids_alloced == 0); } else stack = R_ADDR(infop, lp->free_fid_stack); /* Check if we have room on the stack. */ if (lp->free_fids_alloced <= lp->free_fids + 1) { R_LOCK(dbenv, infop); if ((ret = __db_shalloc(infop, (lp->free_fids_alloced + 20) * sizeof(u_int32_t), 0, &newstack)) != 0) { R_UNLOCK(dbenv, infop); return (ret); } if (stack != NULL) { memcpy(newstack, stack, lp->free_fids_alloced * sizeof(u_int32_t)); __db_shalloc_free(infop, stack); } stack = newstack; lp->free_fid_stack = R_OFFSET(infop, stack); lp->free_fids_alloced += 20; R_UNLOCK(dbenv, infop); } stack[lp->free_fids++] = id; return (0);}static int__dbreg_pop_id(dbenv, id) DB_ENV *dbenv; int32_t *id;{ DB_LOG *dblp; LOG *lp; int32_t *stack; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; /* Do we have anything to pop? */ if (lp->free_fid_stack != INVALID_ROFF && lp->free_fids > 0) { stack = R_ADDR(&dblp->reginfo, lp->free_fid_stack); *id = stack[--lp->free_fids]; } else *id = DB_LOGFILEID_INVALID; return (0);}/* * __dbreg_pluck_id -- * Remove a particular dbreg id from the stack of free ids. This is * used when we open a file, as in recovery, with a specific ID that might * be on the stack. * * Returns success whether or not the particular id was found, and like * push and pop, assumes that the fq_mutex is locked. */static int__dbreg_pluck_id(dbenv, id) DB_ENV *dbenv; int32_t id;{ DB_LOG *dblp; LOG *lp; int32_t *stack; u_int i; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; /* Do we have anything to look at? */ if (lp->free_fid_stack != INVALID_ROFF) { stack = R_ADDR(&dblp->reginfo, lp->free_fid_stack); for (i = 0; i < lp->free_fids; i++) if (id == stack[i]) { /* * Found it. Overwrite it with the top * id (which may harmlessly be itself), * and shorten the stack by one. */ stack[i] = stack[lp->free_fids - 1]; lp->free_fids--; return (0); } } return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -