1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2001,2008 Oracle.  All rights reserved.
5 *
6 * $Id: db_rename.c,v 12.31 2008/01/08 20:58:10 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12#include "dbinc/db_page.h"
13#include "dbinc/db_am.h"
14#include "dbinc/fop.h"
15#include "dbinc/lock.h"
16#include "dbinc/log.h"
17#include "dbinc/mp.h"
18#include "dbinc/txn.h"
19
20static int __db_rename __P((DB *, DB_THREAD_INFO *,
21	     DB_TXN *, const char *, const char *, const char *));
22static int __db_subdb_rename __P((DB *, DB_THREAD_INFO *,
23	     DB_TXN *, const char *, const char *, const char *));
24
25/*
26 * __env_dbrename_pp
27 *	ENV->dbrename pre/post processing.
28 *
29 * PUBLIC: int __env_dbrename_pp __P((DB_ENV *, DB_TXN *,
30 * PUBLIC:     const char *, const char *, const char *, u_int32_t));
31 */
32int
33__env_dbrename_pp(dbenv, txn, name, subdb, newname, flags)
34	DB_ENV *dbenv;
35	DB_TXN *txn;
36	const char *name, *subdb, *newname;
37	u_int32_t flags;
38{
39	DB *dbp;
40	DB_THREAD_INFO *ip;
41	ENV *env;
42	int handle_check, ret, t_ret, txn_local;
43
44	env = dbenv->env;
45	dbp = NULL;
46	txn_local = 0;
47
48	ENV_ILLEGAL_BEFORE_OPEN(env, "DB_ENV->dbrename");
49
50	/*
51	 * The actual argument checking is simple, do it inline, outside of
52	 * the replication block.
53	 */
54	if ((ret = __db_fchk(env, "DB->rename", flags, DB_AUTO_COMMIT)) != 0)
55		return (ret);
56
57	ENV_ENTER(env, ip);
58
59	/* Check for replication block. */
60	handle_check = IS_ENV_REPLICATED(env);
61	if (handle_check && (ret = __env_rep_enter(env, 1)) != 0) {
62		handle_check = 0;
63		goto err;
64	}
65
66	/*
67	 * Create local transaction as necessary, check for consistent
68	 * transaction usage.
69	 */
70	if (IS_ENV_AUTO_COMMIT(env, txn, flags)) {
71		if ((ret = __db_txn_auto_init(env, ip, &txn)) != 0)
72			goto err;
73		txn_local = 1;
74	} else
75		if (txn != NULL && !TXN_ON(env) &&
76		    (!CDB_LOCKING(env) || !F_ISSET(txn, TXN_CDSGROUP))) {
77			ret = __db_not_txn_env(env);
78			goto err;
79		}
80
81	LF_CLR(DB_AUTO_COMMIT);
82
83	if ((ret = __db_create_internal(&dbp, env, 0)) != 0)
84		goto err;
85
86	ret = __db_rename_int(dbp, ip, txn, name, subdb, newname);
87
88	if (txn_local) {
89		/*
90		 * We created the DBP here and when we commit/abort, we'll
91		 * release all the transactional locks, including the handle
92		 * lock; mark the handle cleared explicitly.
93		 */
94		LOCK_INIT(dbp->handle_lock);
95		dbp->locker = NULL;
96	} else if (txn != NULL) {
97		/*
98		 * We created this handle locally so we need to close it and
99		 * clean it up.  Unfortunately, it's holding transactional
100		 * or CDS group locks that need to persist until the end of
101		 * transaction.  If we invalidate the locker (dbp->locker),
102		 * then the close won't free these locks prematurely.
103		 */
104		 dbp->locker = NULL;
105	}
106
107err:	if (txn_local && (t_ret =
108	    __db_txn_auto_resolve(env, txn, 0, ret)) != 0 && ret == 0)
109		ret = t_ret;
110
111	/*
112	 * We never opened this dbp for real, so don't include a transaction
113	 * handle, and use NOSYNC to avoid calling into mpool.
114	 *
115	 * !!!
116	 * Note we're reversing the order of operations: we started the txn and
117	 * then opened the DB handle; we're resolving the txn and then closing
118	 * closing the DB handle -- it's safer.
119	 */
120	if (txn_local || txn == NULL) {
121		if (dbp != NULL &&
122		    (t_ret = __db_close(dbp, NULL, DB_NOSYNC)) != 0 && ret == 0)
123			ret = t_ret;
124	} else {
125		if (dbp != NULL && (t_ret =
126		     __txn_closeevent(env, txn, dbp)) != 0 && ret == 0)
127			ret = t_ret;
128	}
129
130	if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
131		ret = t_ret;
132
133	ENV_LEAVE(env, ip);
134	return (ret);
135}
136
137/*
138 * __db_rename_pp
139 *	DB->rename pre/post processing.
140 *
141 * PUBLIC: int __db_rename_pp __P((DB *,
142 * PUBLIC:     const char *, const char *, const char *, u_int32_t));
143 */
144int
145__db_rename_pp(dbp, name, subdb, newname, flags)
146	DB *dbp;
147	const char *name, *subdb, *newname;
148	u_int32_t flags;
149{
150	DB_THREAD_INFO *ip;
151	ENV *env;
152	int handle_check, ret, t_ret;
153
154	env = dbp->env;
155	handle_check = 0;
156
157	/*
158	 * Validate arguments, continuing to destroy the handle on failure.
159	 *
160	 * Cannot use DB_ILLEGAL_AFTER_OPEN directly because it returns.
161	 *
162	 * !!!
163	 * We have a serious problem if we're here with a handle used to open
164	 * a database -- we'll destroy the handle, and the application won't
165	 * ever be able to close the database.
166	 */
167	if (F_ISSET(dbp, DB_AM_OPEN_CALLED))
168		return (__db_mi_open(env, "DB->rename", 1));
169
170	/* Validate arguments. */
171	if ((ret = __db_fchk(env, "DB->rename", flags, 0)) != 0)
172		return (ret);
173
174	/* Check for consistent transaction usage. */
175	if ((ret = __db_check_txn(dbp, NULL, DB_LOCK_INVALIDID, 0)) != 0)
176		return (ret);
177
178	ENV_ENTER(env, ip);
179
180	handle_check = IS_ENV_REPLICATED(env);
181	if (handle_check && (ret = __db_rep_enter(dbp, 1, 1, 0)) != 0) {
182		handle_check = 0;
183		goto err;
184	}
185
186	/* Rename the file. */
187	ret = __db_rename(dbp, ip, NULL, name, subdb, newname);
188
189	if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
190		ret = t_ret;
191err:	ENV_LEAVE(env, ip);
192	return (ret);
193}
194
195/*
196 * __db_rename
197 *	DB->rename method.
198 *
199 */
200static int
201__db_rename(dbp, ip, txn, name, subdb, newname)
202	DB *dbp;
203	DB_THREAD_INFO *ip;
204	DB_TXN *txn;
205	const char *name, *subdb, *newname;
206{
207	int ret, t_ret;
208
209	ret = __db_rename_int(dbp, ip, txn, name, subdb, newname);
210
211	if (txn == NULL) {
212		if ((t_ret = __db_close(dbp, txn, DB_NOSYNC)) != 0 && ret == 0)
213			ret = t_ret;
214	} else {
215		if ((t_ret =
216		     __txn_closeevent(dbp->env, txn, dbp)) != 0 && ret == 0)
217			ret = t_ret;
218	}
219
220	return (ret);
221}
222
223/*
224 * __db_rename_int
225 *	Worker function for DB->rename method; the close of the dbp is
226 * left in the wrapper routine.
227 *
228 * PUBLIC: int __db_rename_int __P((DB *, DB_THREAD_INFO *,
229 * PUBLIC:      DB_TXN *, const char *, const char *, const char *));
230 */
231int
232__db_rename_int(dbp, ip, txn, name, subdb, newname)
233	DB *dbp;
234	DB_THREAD_INFO *ip;
235	DB_TXN *txn;
236	const char *name, *subdb, *newname;
237{
238	ENV *env;
239	int ret;
240	char *old, *real_name;
241
242	env = dbp->env;
243	real_name = NULL;
244
245	DB_TEST_RECOVERY(dbp, DB_TEST_PREDESTROY, ret, name);
246
247	if (name == NULL && subdb == NULL) {
248		__db_errx(env, "Rename on temporary files invalid");
249		ret = EINVAL;
250		goto err;
251	}
252
253	if (name == NULL)
254		MAKE_INMEM(dbp);
255	else if (subdb != NULL) {
256		ret = __db_subdb_rename(dbp, ip, txn, name, subdb, newname);
257		goto err;
258	}
259
260	/*
261	 * From here on down, this pertains to files or in-memory databases.
262	 *
263	 * Find the real name of the file.
264	 */
265	if (F_ISSET(dbp, DB_AM_INMEM)) {
266		old = (char *)subdb;
267		real_name = (char *)subdb;
268	} else {
269		if ((ret = __db_appname(env,
270		    DB_APP_DATA, name, 0, NULL, &real_name)) != 0)
271			goto err;
272		old = (char *)name;
273	}
274
275	if ((ret = __fop_remove_setup(dbp, txn, real_name, 0)) != 0)
276		goto err;
277
278	if (dbp->db_am_rename != NULL &&
279	    (ret = dbp->db_am_rename(dbp, txn, name, subdb, newname)) != 0)
280		goto err;
281
282	/*
283	 * The transactional case and non-transactional case are
284	 * quite different.  In the non-transactional case, we simply
285	 * do the rename.  In the transactional case, since we need
286	 * the ability to back out and maintain locking, we have to
287	 * create a temporary object as a placeholder.  This is all
288	 * taken care of in the fop layer.
289	 */
290	if (IS_REAL_TXN(txn)) {
291		if ((ret = __fop_dummy(dbp, txn, old, newname, 0)) != 0)
292			goto err;
293	} else {
294		if ((ret = __fop_dbrename(dbp, old, newname)) != 0)
295			goto err;
296	}
297
298	/*
299	 * I am pretty sure that we haven't gotten a dbreg id, so calling
300	 * dbreg_filelist_update is not necessary.
301	 */
302	DB_ASSERT(env, dbp->log_filename == NULL ||
303	    dbp->log_filename->id == DB_LOGFILEID_INVALID);
304
305	DB_TEST_RECOVERY(dbp, DB_TEST_POSTDESTROY, ret, newname);
306
307DB_TEST_RECOVERY_LABEL
308err:	if (!F_ISSET(dbp, DB_AM_INMEM) && real_name != NULL)
309		__os_free(env, real_name);
310
311	return (ret);
312}
313
314/*
315 * __db_subdb_rename --
316 *	Rename a subdatabase.
317 */
318static int
319__db_subdb_rename(dbp, ip, txn, name, subdb, newname)
320	DB *dbp;
321	DB_THREAD_INFO *ip;
322	DB_TXN *txn;
323	const char *name, *subdb, *newname;
324{
325	DB *mdbp;
326	ENV *env;
327	PAGE *meta;
328	int ret, t_ret;
329
330	mdbp = NULL;
331	meta = NULL;
332	env = dbp->env;
333
334	/*
335	 * We have not opened this dbp so it isn't marked as a subdb,
336	 * but it ought to be.
337	 */
338	F_SET(dbp, DB_AM_SUBDB);
339
340	/*
341	 * Rename the entry in the main database.  We need to first
342	 * get the meta-data page number (via MU_OPEN) so that we can
343	 * read the meta-data page and obtain a handle lock.  Once we've
344	 * done that, we can proceed to do the rename in the master.
345	 */
346	if ((ret = __db_master_open(dbp, ip, txn, name, 0, 0, &mdbp)) != 0)
347		goto err;
348
349	if ((ret = __db_master_update(mdbp, dbp, ip, txn, subdb, dbp->type,
350	    MU_OPEN, NULL, 0)) != 0)
351		goto err;
352
353	if ((ret = __memp_fget(mdbp->mpf, &dbp->meta_pgno,
354	    ip, txn, 0, &meta)) != 0)
355		goto err;
356	memcpy(dbp->fileid, ((DBMETA *)meta)->uid, DB_FILE_ID_LEN);
357	if ((ret = __fop_lock_handle(env,
358	    dbp, mdbp->locker, DB_LOCK_WRITE, NULL, NOWAIT_FLAG(txn))) != 0)
359		goto err;
360
361	ret = __memp_fput(mdbp->mpf, ip, meta, dbp->priority);
362	meta = NULL;
363	if (ret != 0)
364		goto err;
365
366	if ((ret = __db_master_update(mdbp, dbp, ip, txn,
367	    subdb, dbp->type, MU_RENAME, newname, 0)) != 0)
368		goto err;
369
370	DB_TEST_RECOVERY(dbp, DB_TEST_POSTDESTROY, ret, name);
371
372DB_TEST_RECOVERY_LABEL
373err:
374	if (meta != NULL && (t_ret =
375	    __memp_fput(mdbp->mpf, ip, meta, dbp->priority)) != 0 && ret == 0)
376		ret = t_ret;
377
378	if (txn == NULL) {
379		if (mdbp != NULL &&
380		    (t_ret = __db_close(mdbp, txn, DB_NOSYNC)) != 0 && ret == 0)
381			ret = t_ret;
382	} else {
383		if (mdbp != NULL && (t_ret =
384		     __txn_closeevent(env, txn, mdbp)) != 0 && ret == 0)
385			ret = t_ret;
386	}
387
388	return (ret);
389}
390