1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000, 2013 Oracle and/or its affiliates. All rights reserved.
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_swap.h"
14 #include "dbinc/db_am.h"
15 #include "dbinc/mp.h"
16
17 /*
18 * __env_fileid_reset_pp --
19 * ENV->fileid_reset pre/post processing.
20 *
21 * PUBLIC: int __env_fileid_reset_pp __P((DB_ENV *, const char *, u_int32_t));
22 */
23 int
__env_fileid_reset_pp(dbenv,name,flags)24 __env_fileid_reset_pp(dbenv, name, flags)
25 DB_ENV *dbenv;
26 const char *name;
27 u_int32_t flags;
28 {
29 DB_THREAD_INFO *ip;
30 ENV *env;
31 int ret;
32
33 env = dbenv->env;
34
35 ENV_ILLEGAL_BEFORE_OPEN(env, "DB_ENV->fileid_reset");
36
37 /*
38 * !!!
39 * The actual argument checking is simple, do it inline, outside of
40 * the replication block.
41 */
42 if (flags != 0 && flags != DB_ENCRYPT)
43 return (__db_ferr(env, "DB_ENV->fileid_reset", 0));
44
45 ENV_ENTER(env, ip);
46 REPLICATION_WRAP(env,
47 (__env_fileid_reset(env, ip, name, LF_ISSET(DB_ENCRYPT) ? 1 : 0)),
48 1, ret);
49 ENV_LEAVE(env, ip);
50 return (ret);
51 }
52
53 /*
54 * __env_fileid_reset --
55 * Reset the file IDs for every database in the file.
56 * PUBLIC: int __env_fileid_reset
57 * PUBLIC: __P((ENV *, DB_THREAD_INFO *, const char *, int));
58 */
59 int
__env_fileid_reset(env,ip,name,encrypted)60 __env_fileid_reset(env, ip, name, encrypted)
61 ENV *env;
62 DB_THREAD_INFO *ip;
63 const char *name;
64 int encrypted;
65 {
66 DB *dbp;
67 DBC *dbcp;
68 DBMETA *meta;
69 DBT key, data;
70 DB_FH *fhp;
71 DB_MPOOLFILE *mpf;
72 DB_PGINFO cookie;
73 db_pgno_t pgno;
74 int subdb, t_ret, ret;
75 size_t n;
76 char *real_name;
77 u_int8_t fileid[DB_FILE_ID_LEN], mbuf[DBMETASIZE];
78 void *pagep;
79
80 dbp = NULL;
81 dbcp = NULL;
82 fhp = NULL;
83 real_name = NULL;
84
85 /* Get the real backing file name. */
86 if ((ret = __db_appname(env,
87 DB_APP_DATA, name, NULL, &real_name)) != 0)
88 return (ret);
89
90 /* Get a new file ID. */
91 if ((ret = __os_fileid(env, real_name, 1, fileid)) != 0)
92 goto err;
93
94 /*
95 * The user may have physically copied a file currently open in the
96 * cache, which means if we open this file through the cache before
97 * updating the file ID on page 0, we might connect to the file from
98 * which the copy was made.
99 */
100 if ((ret = __os_open(env, real_name, 0, 0, 0, &fhp)) != 0) {
101 __db_err(env, ret, "%s", real_name);
102 goto err;
103 }
104 if ((ret = __os_read(env, fhp, mbuf, sizeof(mbuf), &n)) != 0)
105 goto err;
106
107 if (n != sizeof(mbuf)) {
108 ret = EINVAL;
109 __db_errx(env, DB_STR_A("0675",
110 "__env_fileid_reset: %s: unexpected file type or format",
111 "%s"), real_name);
112 goto err;
113 }
114
115 /*
116 * Create the DB object.
117 */
118 if ((ret = __db_create_internal(&dbp, env, 0)) != 0)
119 goto err;
120
121 /* If configured with a password, the databases are encrypted. */
122 if (encrypted && (ret = __db_set_flags(dbp, DB_ENCRYPT)) != 0)
123 goto err;
124
125 if ((ret = __db_meta_setup(env,
126 dbp, real_name, (DBMETA *)mbuf, 0, DB_CHK_META)) != 0)
127 goto err;
128
129 meta = (DBMETA *)mbuf;
130 if (FLD_ISSET(meta->metaflags,
131 DBMETA_PART_RANGE | DBMETA_PART_CALLBACK) && (ret =
132 __part_fileid_reset(env, ip, name, meta->nparts, encrypted)) != 0)
133 goto err;
134
135 subdb = meta->type == P_BTREEMETA && F_ISSET(meta, BTM_SUBDB);
136
137 memcpy(meta->uid, fileid, DB_FILE_ID_LEN);
138 cookie.db_pagesize = sizeof(mbuf);
139 cookie.flags = dbp->flags;
140 cookie.type = dbp->type;
141 key.data = &cookie;
142
143 if ((ret = __db_pgout(env->dbenv, 0, mbuf, &key)) != 0)
144 goto err;
145 if ((ret = __os_seek(env, fhp, 0, 0, 0)) != 0)
146 goto err;
147 if ((ret = __os_write(env, fhp, mbuf, sizeof(mbuf), &n)) != 0)
148 goto err;
149 if ((ret = __os_fsync(env, fhp)) != 0)
150 goto err;
151
152 /*
153 * Page 0 of the file has an updated file ID, and we can open it in
154 * the cache without connecting to a different, existing file. Open
155 * the file in the cache, and update the file IDs for subdatabases.
156 */
157
158 /*
159 * If the database file doesn't support subdatabases, we only have
160 * to update a single metadata page. Otherwise, we have to open a
161 * cursor and step through the master database, and update all of
162 * the subdatabases' metadata pages.
163 */
164 if (!subdb)
165 goto err;
166
167 /*
168 * Open the DB file.
169 *
170 * !!!
171 * Note DB_RDWRMASTER flag, we need to open the master database file
172 * for writing in this case.
173 */
174 if ((ret = __db_open(dbp, ip, NULL,
175 name, NULL, DB_UNKNOWN, DB_RDWRMASTER, 0, PGNO_BASE_MD)) != 0)
176 goto err;
177
178 mpf = dbp->mpf;
179 memset(&key, 0, sizeof(key));
180 memset(&data, 0, sizeof(data));
181 if ((ret = __db_cursor(dbp, ip, NULL, &dbcp, 0)) != 0)
182 goto err;
183 while ((ret = __dbc_get(dbcp, &key, &data, DB_NEXT)) == 0) {
184 /*
185 * XXX
186 * We're handling actual data, not on-page meta-data, so it
187 * hasn't been converted to/from opposite endian architectures.
188 * Do it explicitly, now.
189 */
190 memcpy(&pgno, data.data, sizeof(db_pgno_t));
191 DB_NTOHL_SWAP(env, &pgno);
192 if ((ret = __memp_fget(mpf, &pgno, ip, NULL,
193 DB_MPOOL_DIRTY, &pagep)) != 0)
194 goto err;
195 memcpy(((DBMETA *)pagep)->uid, fileid, DB_FILE_ID_LEN);
196 if ((ret = __memp_fput(mpf, ip, pagep, dbcp->priority)) != 0)
197 goto err;
198 }
199 if (ret == DB_NOTFOUND)
200 ret = 0;
201
202 err: if (dbcp != NULL && (t_ret = __dbc_close(dbcp)) != 0 && ret == 0)
203 ret = t_ret;
204 if (dbp != NULL && (t_ret = __db_close(dbp, NULL, 0)) != 0 && ret == 0)
205 ret = t_ret;
206 if (fhp != NULL &&
207 (t_ret = __os_closehandle(env, fhp)) != 0 && ret == 0)
208 ret = t_ret;
209 if (real_name != NULL)
210 __os_free(env, real_name);
211
212 return (ret);
213 }
214