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