1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 273 274 275 276 277 278 279 280 281 282 283 284 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 311 312 313 314 315 316 317 318
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2001-2002
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char revid[] = "$Id: db_remove.c,v 1.1.1.1 2003/11/20 22:13:14 toshok Exp $";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#endif
#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/fop.h"
#include "dbinc/btree.h"
#include "dbinc/hash.h"
#include "dbinc/db_shash.h"
#include "dbinc/lock.h"
static int __db_subdb_remove __P((DB *, DB_TXN *, const char *, const char *));
static int __db_dbtxn_remove __P((DB *, DB_TXN *, const char *));
/*
* __dbenv_dbremove
* Remove method for DB_ENV.
*
* PUBLIC: int __dbenv_dbremove __P((DB_ENV *,
* PUBLIC: DB_TXN *, const char *, const char *, u_int32_t));
*/
int
__dbenv_dbremove(dbenv, txn, name, subdb, flags)
DB_ENV *dbenv;
DB_TXN *txn;
const char *name, *subdb;
u_int32_t flags;
{
DB *dbp;
int ret, t_ret, txn_local;
txn_local = 0;
PANIC_CHECK(dbenv);
ENV_ILLEGAL_BEFORE_OPEN(dbenv, "DB_ENV->dbremove");
/* Validate arguments. */
if ((ret = __db_fchk(dbenv, "DB->remove", flags, DB_AUTO_COMMIT)) != 0)
return (ret);
if ((ret = db_create(&dbp, dbenv, 0)) != 0)
return (ret);
/*
* Create local transaction as necessary, check for consistent
* transaction usage.
*/
if (IS_AUTO_COMMIT(dbenv, txn, flags)) {
if ((ret = __db_txn_auto(dbp, &txn)) != 0)
return (ret);
txn_local = 1;
} else
if (txn != NULL && !TXN_ON(dbenv))
return (__db_not_txn_env(dbenv));
ret = __db_remove_i(dbp, txn, name, subdb);
/* Commit for DB_AUTO_COMMIT. */
if (txn_local) {
if (ret == 0)
ret = txn->commit(txn, 0);
else
if ((t_ret = txn->abort(txn)) != 0)
ret = __db_panic(dbenv, t_ret);
/*
* We created the DBP here and when we committed/aborted,
* we release all the tranasctional locks, which includes
* the handle lock; mark the handle cleared explicitly.
*/
LOCK_INIT(dbp->handle_lock);
dbp->lid = DB_LOCK_INVALIDID;
}
/*
* We never opened this dbp for real, so don't call the transactional
* version of DB->close, and use NOSYNC to avoid calling into mpool.
*/
if ((t_ret = dbp->close(dbp, DB_NOSYNC)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
/*
* __db_remove
* Remove method for DB.
*
* PUBLIC: int __db_remove __P((DB *, const char *, const char *, u_int32_t));
*/
int
__db_remove(dbp, name, subdb, flags)
DB *dbp;
const char *name, *subdb;
u_int32_t flags;
{
DB_ENV *dbenv;
int ret, t_ret;
dbenv = dbp->dbenv;
PANIC_CHECK(dbenv);
/*
* Validate arguments, continuing to destroy the handle on failure.
*
* Cannot use DB_ILLEGAL_AFTER_OPEN directly because it returns.
*
* !!!
* We have a serious problem if we're here with a handle used to open
* a database -- we'll destroy the handle, and the application won't
* ever be able to close the database.
*/
if (F_ISSET(dbp, DB_AM_OPEN_CALLED)) {
ret = __db_mi_open(dbenv, "DB->remove", 1);
goto err;
}
/* Validate arguments. */
if ((ret = __db_fchk(dbenv, "DB->remove", flags, 0)) != 0)
goto err;
/* Check for consistent transaction usage. */
if ((ret = __db_check_txn(dbp, NULL, DB_LOCK_INVALIDID, 0)) != 0)
goto err;
/* Remove the file. */
ret = __db_remove_i(dbp, NULL, name, subdb);
/*
* We never opened this dbp for real, use NOSYNC to avoid calling into
* mpool.
*/
err: if ((t_ret = dbp->close(dbp, DB_NOSYNC)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
/*
* __db_remove_i
* Internal remove method for DB.
*
* PUBLIC: int __db_remove_i __P((DB *, DB_TXN *, const char *, const char *));
*/
int
__db_remove_i(dbp, txn, name, subdb)
DB *dbp;
DB_TXN *txn;
const char *name, *subdb;
{
DB_ENV *dbenv;
DB_LSN newlsn;
int ret;
char *real_name;
dbenv = dbp->dbenv;
real_name = NULL;
/* Handle subdatabase removes separately. */
if (subdb != NULL)
return (__db_subdb_remove(dbp, txn, name, subdb));
/* Handle transactional file removes separately. */
if (txn != NULL)
return (__db_dbtxn_remove(dbp, txn, name));
/*
* The remaining case is a non-transactional file remove.
*
* Find the real name of the file.
*/
if ((ret = __db_appname(dbenv,
DB_APP_DATA, name, 0, NULL, &real_name)) != 0)
return (ret);
if ((ret = __fop_remove_setup(dbp, NULL, real_name, 0)) != 0)
goto err;
if (dbp->db_am_remove != NULL &&
(ret = dbp->db_am_remove(dbp, NULL, name, subdb, &newlsn)) != 0)
goto err;
ret = __fop_remove(dbenv, NULL, dbp->fileid, name, DB_APP_DATA);
err:
if (real_name != NULL)
__os_free(dbenv, real_name);
return (ret);
}
/*
* __db_subdb_remove --
* Remove a subdatabase.
*/
static int
__db_subdb_remove(dbp, txn, name, subdb)
DB *dbp;
DB_TXN *txn;
const char *name, *subdb;
{
DB *mdbp, *sdbp;
int ret, t_ret;
mdbp = sdbp = NULL;
/* Open the subdatabase. */
if ((ret = db_create(&sdbp, dbp->dbenv, 0)) != 0)
goto err;
if ((ret = __db_open(sdbp,
txn, name, subdb, DB_UNKNOWN, DB_WRITEOPEN, 0)) != 0)
goto err;
DB_TEST_RECOVERY(sdbp, DB_TEST_PREDESTROY, ret, name);
/* Free up the pages in the subdatabase. */
switch (sdbp->type) {
case DB_BTREE:
case DB_RECNO:
if ((ret = __bam_reclaim(sdbp, txn)) != 0)
goto err;
break;
case DB_HASH:
if ((ret = __ham_reclaim(sdbp, txn)) != 0)
goto err;
break;
default:
ret = __db_unknown_type(
sdbp->dbenv, "__db_subdb_remove", sdbp->type);
goto err;
}
/*
* Remove the entry from the main database and free the subdatabase
* metadata page.
*/
if ((ret = __db_master_open(sdbp, txn, name, 0, 0, &mdbp)) != 0)
goto err;
if ((ret = __db_master_update(
mdbp, sdbp, txn, subdb, sdbp->type, MU_REMOVE, NULL, 0)) != 0)
goto err;
DB_TEST_RECOVERY(sdbp, DB_TEST_POSTDESTROY, ret, name);
DB_TEST_RECOVERY_LABEL
err:
/* Close the main and subdatabases. */
if ((t_ret = __db_close_i(sdbp, txn, 0)) != 0 && ret == 0)
ret = t_ret;
if (mdbp != NULL &&
(t_ret = __db_close_i(mdbp, txn, 0)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
static int
__db_dbtxn_remove(dbp, txn, name)
DB *dbp;
DB_TXN *txn;
const char *name;
{
DB_ENV *dbenv;
DB_LSN newlsn;
int ret;
char *tmpname;
dbenv = dbp->dbenv;
tmpname = NULL;
/*
* This is a transactional rename, so we have to keep the name
* of the file locked until the transaction commits. As a result,
* we implement remove by renaming the file to some other name
* (which creates a dummy named file as a placeholder for the
* file being rename/dremoved) and then deleting that file as
* a delayed remove at commit.
*/
if ((ret = __db_backup_name(dbenv, name, txn, &tmpname)) != 0)
return (ret);
DB_TEST_RECOVERY(dbp, DB_TEST_PREDESTROY, ret, name);
if ((ret = __db_rename_i(dbp, txn, name, NULL, tmpname)) != 0)
goto err;
/* The internal removes will also translate into delayed removes. */
if (dbp->db_am_remove != NULL &&
(ret = dbp->db_am_remove(dbp, txn, tmpname, NULL, &newlsn)) != 0)
goto err;
ret = __fop_remove(dbenv, txn, dbp->fileid, tmpname, DB_APP_DATA);
DB_TEST_RECOVERY(dbp, DB_TEST_POSTDESTROY, ret, name);
err:
DB_TEST_RECOVERY_LABEL
if (tmpname != NULL)
__os_free(dbenv, tmpname);
return (ret);
}
|