qam_open.c 8.04 KB
Newer Older
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
jimw@mysql.com's avatar
jimw@mysql.com committed
4
 * Copyright (c) 1999-2005
5
 *	Sleepycat Software.  All rights reserved.
jimw@mysql.com's avatar
jimw@mysql.com committed
6
 *
jimw@mysql.com's avatar
jimw@mysql.com committed
7
 * $Id: qam_open.c,v 12.4 2005/10/15 00:56:55 bostic Exp $
8 9 10 11 12 13 14 15 16 17 18
 */

#include "db_config.h"

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <string.h>
#endif

#include "db_int.h"
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
19 20 21 22 23 24
#include "dbinc/crypto.h"
#include "dbinc/db_page.h"
#include "dbinc/db_shash.h"
#include "dbinc/db_swap.h"
#include "dbinc/db_am.h"
#include "dbinc/lock.h"
jimw@mysql.com's avatar
jimw@mysql.com committed
25
#include "dbinc/mp.h"
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
26 27 28 29
#include "dbinc/qam.h"
#include "dbinc/fop.h"

static int __qam_init_meta __P((DB *, QMETA *));
30 31 32 33

/*
 * __qam_open
 *
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
34 35
 * PUBLIC: int __qam_open __P((DB *,
 * PUBLIC:     DB_TXN *, const char *, db_pgno_t, int, u_int32_t));
36 37
 */
int
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
38
__qam_open(dbp, txn, name, base_pgno, mode, flags)
39
	DB *dbp;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
40
	DB_TXN *txn;
41 42 43 44 45 46
	const char *name;
	db_pgno_t base_pgno;
	int mode;
	u_int32_t flags;
{
	DBC *dbc;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
47
	DB_ENV *dbenv;
48
	DB_LOCK metalock;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
49
	DB_MPOOLFILE *mpf;
50
	QMETA *qmeta;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
51
	QUEUE *t;
52 53
	int ret, t_ret;

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
54 55
	dbenv = dbp->dbenv;
	mpf = dbp->mpf;
56
	t = dbp->q_internal;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
57 58
	ret = 0;
	qmeta = NULL;
59

jimw@mysql.com's avatar
jimw@mysql.com committed
60 61 62 63 64 65
	if (name == NULL && t->page_ext != 0) {
		__db_err(dbenv,
	"Extent size may not be specified for in-memory queue database");
		return (EINVAL);
	}

66 67 68 69 70 71 72 73 74 75
	/* Initialize the remaining fields/methods of the DB. */
	dbp->db_am_remove = __qam_remove;
	dbp->db_am_rename = __qam_rename;

	/*
	 * Get a cursor.  If DB_CREATE is specified, we may be creating
	 * pages, and to do that safely in CDB we need a write cursor.
	 * In STD_LOCKING mode, we'll synchronize using the meta page
	 * lock instead.
	 */
jimw@mysql.com's avatar
jimw@mysql.com committed
76 77 78
	if ((ret = __db_cursor(dbp, txn, &dbc,
	    LF_ISSET(DB_CREATE) && CDB_LOCKING(dbenv) ?
	    DB_WRITECURSOR : 0)) != 0)
79 80
		return (ret);

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
81 82 83 84 85
	/*
	 * Get the meta data page.  It must exist, because creates of
	 * files/databases come in through the __qam_new_file interface
	 * and queue doesn't support subdatabases.
	 */
86 87 88
	if ((ret =
	    __db_lget(dbc, 0, base_pgno, DB_LOCK_READ, 0, &metalock)) != 0)
		goto err;
jimw@mysql.com's avatar
jimw@mysql.com committed
89
	if ((ret = __memp_fget(mpf, &base_pgno, 0, &qmeta)) != 0)
90 91
		goto err;

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
92 93 94
	/* If the magic number is incorrect, that's a fatal error. */
	if (qmeta->dbmeta.magic != DB_QAMMAGIC) {
		__db_err(dbenv, "%s: unexpected file type or format", name);
95 96 97 98 99
		ret = EINVAL;
		goto err;
	}

	/* Setup information needed to open extents. */
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
100
	t->page_ext = qmeta->page_ext;
101

jimw@mysql.com's avatar
jimw@mysql.com committed
102 103
	if (t->page_ext != 0 && (ret = __qam_set_ext_data(dbp, name)) != 0)
		goto err;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
104

jimw@mysql.com's avatar
jimw@mysql.com committed
105
	if (mode == 0)
jimw@mysql.com's avatar
jimw@mysql.com committed
106
		mode = __db_omode("rw-rw----");
jimw@mysql.com's avatar
jimw@mysql.com committed
107
	t->mode = mode;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
108 109 110 111 112 113 114
	t->re_pad = qmeta->re_pad;
	t->re_len = qmeta->re_len;
	t->rec_page = qmeta->rec_page;

	t->q_meta = base_pgno;
	t->q_root = base_pgno + 1;

jimw@mysql.com's avatar
jimw@mysql.com committed
115 116
err:	if (qmeta != NULL &&
	    (t_ret = __memp_fput(mpf, qmeta, 0)) != 0 && ret == 0)
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
117 118
		ret = t_ret;

119
	/* Don't hold the meta page long term. */
jimw@mysql.com's avatar
jimw@mysql.com committed
120 121
	if ((t_ret = __LPUT(dbc, metalock)) != 0 && ret == 0)
		ret = t_ret;
122

jimw@mysql.com's avatar
jimw@mysql.com committed
123
	if ((t_ret = __db_c_close(dbc)) != 0 && ret == 0)
124 125 126 127 128
		ret = t_ret;

	return (ret);
}

jimw@mysql.com's avatar
jimw@mysql.com committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/*
 * __qam_set_ext_data --
 *	Setup DBP data for opening queue extents.
 *
 * PUBLIC: int __qam_set_ext_data __P((DB*, const char *));
 */
int
__qam_set_ext_data(dbp, name)
	DB *dbp;
	const char *name;
{
	QUEUE *t;
	int ret;

	t = dbp->q_internal;
	t->pginfo.db_pagesize = dbp->pgsize;
	t->pginfo.flags =
	    F_ISSET(dbp, (DB_AM_CHKSUM | DB_AM_ENCRYPT | DB_AM_SWAP));
	t->pginfo.type = dbp->type;
	t->pgcookie.data = &t->pginfo;
	t->pgcookie.size = sizeof(DB_PGINFO);

	if ((ret = __os_strdup(dbp->dbenv, name,  &t->path)) != 0)
		return (ret);
	t->dir = t->path;
	if ((t->name = __db_rpath(t->path)) == NULL) {
		t->name = t->path;
		t->dir = PATH_DOT;
	} else
		*t->name++ = '\0';

	return (0);
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/*
 * __qam_metachk --
 *
 * PUBLIC: int __qam_metachk __P((DB *, const char *, QMETA *));
 */
int
__qam_metachk(dbp, name, qmeta)
	DB *dbp;
	const char *name;
	QMETA *qmeta;
{
	DB_ENV *dbenv;
	u_int32_t vers;
	int ret;

	dbenv = dbp->dbenv;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
179
	ret = 0;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	/*
	 * At this point, all we know is that the magic number is for a Queue.
	 * Check the version, the database may be out of date.
	 */
	vers = qmeta->dbmeta.version;
	if (F_ISSET(dbp, DB_AM_SWAP))
		M_32_SWAP(vers);
	switch (vers) {
	case 1:
	case 2:
		__db_err(dbenv,
		    "%s: queue version %lu requires a version upgrade",
		    name, (u_long)vers);
		return (DB_OLD_VERSION);
	case 3:
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
196
	case 4:
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		break;
	default:
		__db_err(dbenv,
		    "%s: unsupported qam version: %lu", name, (u_long)vers);
		return (EINVAL);
	}

	/* Swap the page if we need to. */
	if (F_ISSET(dbp, DB_AM_SWAP) && (ret = __qam_mswap((PAGE *)qmeta)) != 0)
		return (ret);

	/* Check the type. */
	if (dbp->type != DB_QUEUE && dbp->type != DB_UNKNOWN)
		return (EINVAL);
	dbp->type = DB_QUEUE;
	DB_ILLEGAL_METHOD(dbp, DB_OK_QUEUE);

	/* Set the page size. */
	dbp->pgsize = qmeta->dbmeta.pagesize;

	/* Copy the file's ID. */
	memcpy(dbp->fileid, qmeta->dbmeta.uid, DB_FILE_ID_LEN);

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	/* Set up AM-specific methods that do not require an open. */
	dbp->db_am_rename = __qam_rename;
	dbp->db_am_remove = __qam_remove;

	return (ret);
}

/*
 * __qam_init_meta --
 *	Initialize the meta-data for a Queue database.
 */
static int
__qam_init_meta(dbp, meta)
	DB *dbp;
	QMETA *meta;
{
	QUEUE *t;

	t = dbp->q_internal;

	memset(meta, 0, sizeof(QMETA));
	LSN_NOT_LOGGED(meta->dbmeta.lsn);
	meta->dbmeta.pgno = PGNO_BASE_MD;
	meta->dbmeta.last_pgno = 0;
	meta->dbmeta.magic = DB_QAMMAGIC;
	meta->dbmeta.version = DB_QAMVERSION;
	meta->dbmeta.pagesize = dbp->pgsize;
	if (F_ISSET(dbp, DB_AM_CHKSUM))
		FLD_SET(meta->dbmeta.metaflags, DBMETA_CHKSUM);
	if (F_ISSET(dbp, DB_AM_ENCRYPT)) {
		meta->dbmeta.encrypt_alg =
		   ((DB_CIPHER *)dbp->dbenv->crypto_handle)->alg;
		DB_ASSERT(meta->dbmeta.encrypt_alg != 0);
		meta->crypto_magic = meta->dbmeta.magic;
	}
	meta->dbmeta.type = P_QAMMETA;
	meta->re_pad = t->re_pad;
	meta->re_len = t->re_len;
	meta->rec_page = CALC_QAM_RECNO_PER_PAGE(dbp);
	meta->cur_recno = 1;
	meta->first_recno = 1;
	meta->page_ext = t->page_ext;
	t->rec_page = meta->rec_page;
	memcpy(meta->dbmeta.uid, dbp->fileid, DB_FILE_ID_LEN);

	/* Verify that we can fit at least one record per page. */
	if (QAM_RECNO_PER_PAGE(dbp) < 1) {
		__db_err(dbp->dbenv,
		    "Record size of %lu too large for page size of %lu",
		    (u_long)t->re_len, (u_long)dbp->pgsize);
		return (EINVAL);
	}

273 274
	return (0);
}
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
275 276 277 278 279 280 281 282

/*
 * __qam_new_file --
 * Create the necessary pages to begin a new queue database file.
 *
 * This code appears more complex than it is because of the two cases (named
 * and unnamed).  The way to read the code is that for each page being created,
 * there are three parts: 1) a "get page" chunk (which either uses malloc'd
jimw@mysql.com's avatar
jimw@mysql.com committed
283 284
 * memory or calls __memp_fget), 2) the initialization, and 3) the "put page"
 * chunk which either does a fop write or an __memp_fput.
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
 *
 * PUBLIC: int __qam_new_file __P((DB *, DB_TXN *, DB_FH *, const char *));
 */
int
__qam_new_file(dbp, txn, fhp, name)
	DB *dbp;
	DB_TXN *txn;
	DB_FH *fhp;
	const char *name;
{
	QMETA *meta;
	DB_ENV *dbenv;
	DB_MPOOLFILE *mpf;
	DB_PGINFO pginfo;
	DBT pdbt;
	db_pgno_t pgno;
	int ret;
	void *buf;

	dbenv = dbp->dbenv;
	mpf = dbp->mpf;
	buf = NULL;
	meta = NULL;

	/* Build meta-data page. */

jimw@mysql.com's avatar
jimw@mysql.com committed
311
	if (F_ISSET(dbp, DB_AM_INMEM)) {
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
312
		pgno = PGNO_BASE_MD;
jimw@mysql.com's avatar
jimw@mysql.com committed
313
		ret = __memp_fget(mpf, &pgno, DB_MPOOL_CREATE, &meta);
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
314
	} else {
jimw@mysql.com's avatar
jimw@mysql.com committed
315
		ret = __os_calloc(dbenv, 1, dbp->pgsize, &buf);
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
316 317 318 319 320 321 322 323
		meta = (QMETA *)buf;
	}
	if (ret != 0)
		return (ret);

	if ((ret = __qam_init_meta(dbp, meta)) != 0)
		goto err;

jimw@mysql.com's avatar
jimw@mysql.com committed
324 325 326 327
	if (F_ISSET(dbp, DB_AM_INMEM)) {
		if ((ret = __db_log_page(dbp,
		    txn, &meta->dbmeta.lsn, pgno, (PAGE *)meta)) != 0)
			goto err;
jimw@mysql.com's avatar
jimw@mysql.com committed
328
		ret = __memp_fput(mpf, meta, DB_MPOOL_DIRTY);
jimw@mysql.com's avatar
jimw@mysql.com committed
329
	} else {
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
330 331 332 333 334 335 336 337
		pginfo.db_pagesize = dbp->pgsize;
		pginfo.flags =
		    F_ISSET(dbp, (DB_AM_CHKSUM | DB_AM_ENCRYPT | DB_AM_SWAP));
		pginfo.type = DB_QUEUE;
		pdbt.data = &pginfo;
		pdbt.size = sizeof(pginfo);
		if ((ret = __db_pgout(dbenv, PGNO_BASE_MD, meta, &pdbt)) != 0)
			goto err;
jimw@mysql.com's avatar
jimw@mysql.com committed
338 339 340
		ret = __fop_write(dbenv, txn, name,
		    DB_APP_DATA, fhp, dbp->pgsize, 0, 0, buf, dbp->pgsize, 1,
		    F_ISSET(dbp, DB_AM_NOT_DURABLE) ? DB_LOG_NOT_DURABLE : 0);
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
341 342 343 344 345 346 347 348
	}
	if (ret != 0)
		goto err;
	meta = NULL;

err:	if (name != NULL)
		__os_free(dbenv, buf);
	else if (meta != NULL)
jimw@mysql.com's avatar
jimw@mysql.com committed
349
		(void)__memp_fput(mpf, meta, 0);
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
350 351
	return (ret);
}