summaryrefslogtreecommitdiff
path: root/src/session/session_root.c
blob: 5d7d8b7654070981e66988625bec5e09a512f029 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

static int __btree_get_root(
	WT_SESSION_IMPL *, const char *, const char **, int *, int *);
static int __btree_get_turtle(WT_SESSION_IMPL *, const char **, int *, int *);
static int __btree_parse_root(
	WT_SESSION_IMPL *, const char *, const char **, int *, int *);
static int __btree_set_root(WT_SESSION_IMPL *, const char *, WT_ITEM *);
static int __btree_set_turtle(WT_SESSION_IMPL *, WT_ITEM *);

#define	WT_TURTLE_MSG		"The turtle."

#define	WT_SCHEMA_TURTLE	"WiredTiger.turtle"	/* Schema root page */
#define	WT_SCHEMA_TURTLE_SET	"WiredTiger.turtle.set"	/* Schema root prep */

/*
 * __wt_btree_get_root --
 *	Get the file's root address.
 */
int
__wt_btree_get_root(WT_SESSION_IMPL *session, WT_ITEM *addr)
{
	WT_BTREE *btree;
	int majorv, minorv, ret;
	const char *v;

	btree = session->btree;
	v = NULL;
	ret = 0;

	/* If there's no root address, return a NULL with a size of 0. */
	addr->data = NULL;
	addr->size = 0;

	/*
	 * If we don't find a file, we're creating a new one, at the current
	 * version.
	 */
	majorv = WT_BTREE_MAJOR_VERSION;
	minorv = WT_BTREE_MINOR_VERSION;

	/* Get the root address and major/minor numbers. */
	WT_ERR(strcmp(btree->filename, WT_SCHEMA_FILENAME) == 0 ?
	    __btree_get_turtle(session, &v, &majorv, &minorv) :
	    __btree_get_root(session, btree->filename, &v, &majorv, &minorv));

	if (majorv > WT_BTREE_MAJOR_VERSION ||
	    (majorv == WT_BTREE_MAJOR_VERSION &&
	    minorv > WT_BTREE_MINOR_VERSION))
		WT_ERR_MSG(session, EACCES,
		    "%s is an unsupported version of a WiredTiger file",
		    btree->filename);

	if (v != NULL && strlen(v) != 0 && strcmp(v, WT_NOADDR) != 0)
		WT_ERR(__wt_hex_to_raw(session, v, addr));

err:	if (ret != 0)
		__wt_errx(session,
		    "unable to find %s file's root address", btree->filename);

	__wt_free(session, v);
	return (ret);
}

/*
 * __wt_btree_free_root --
 *	Free the file's root address.
 */
int
__wt_btree_free_root(WT_SESSION_IMPL *session)
{
	WT_ITEM *addr, *as;
	WT_BTREE *btree;
	int ret;

	btree = session->btree;
	addr = as = NULL;

	WT_RET(__wt_scr_alloc(session, WT_BM_MAX_ADDR_COOKIE, &addr));
	WT_ERR(__wt_btree_get_root(session, addr));
	if (addr->data != NULL) {
		WT_RET(__wt_scr_alloc(session, 0, &as));
		WT_VERBOSE(session, verify, "free %s root %s",
		    btree->filename,
		    __wt_addr_string(session, as, addr->data, addr->size));

		WT_ERR(__wt_bm_free(session, addr->data, addr->size));
	}

err:	__wt_scr_free(&addr);
	__wt_scr_free(&as);
	return (ret);
}

/*
 * __wt_btree_set_root --
 *	Set the file's root address.
 */
int
__wt_btree_set_root(WT_SESSION_IMPL *session,
    const char *filename, const uint8_t *addr, uint32_t size)
{
	WT_ITEM *v;
	int ret;

	v = NULL;
	ret = 0;

	/*
	 * Every bytes is encoded as 2 bytes, plus a trailing nul byte,
	 * and it needs to hold the "no address" string.
	 */
	WT_RET(__wt_scr_alloc(
	    session, size * 2 + 1 + WT_STORE_SIZE(strlen(WT_NOADDR)), &v));

	WT_VERBOSE(session, verify, "set %s root %s",
	    filename, __wt_addr_string(session, v, addr, size));

	/*
	 * We're not using the WT_ITEM as a buffer going forward, but fill
	 * in the values anyway, just for safety.
	 */
	if (addr == NULL) {
		v->data = WT_NOADDR;
		v->size = WT_STORE_SIZE(strlen(WT_NOADDR)) + 1;
	} else
		WT_ERR(__wt_raw_to_hex(session, addr, size, v));

	WT_ERR(strcmp(filename, WT_SCHEMA_FILENAME) == 0 ?
	    __btree_set_turtle(session, v) :
	    __btree_set_root(session, filename, v));

err:	/*
	 * If we are unrolling a failed create, we may have already removed
	 * the schema table entry.  If no entry is found to update and we're
	 * trying to clear the root, just ignore it.
	 */
	if (ret == WT_NOTFOUND && addr == NULL)
		ret = 0;
	if (ret != 0)
		__wt_errx(session,
		    "unable to update %s file's root address", filename);

	__wt_scr_free(&v);
	return (ret);
}

/*
 * __btree_get_turtle --
 *	Get the schema file's root address.
 */
static int
__btree_get_turtle(
    WT_SESSION_IMPL *session, const char **vp, int *majorp, int *minorp)
{
	FILE *fp;
	int ret;
	const char *path;
	char line[1024], *p;

	*vp = NULL;

	fp = NULL;
	ret = 0;
	path = NULL;

	WT_RET(__wt_filename(session, WT_SCHEMA_TURTLE, &path));
	if ((fp = fopen(path, "r")) == NULL)
		goto done;
	while (fgets(line, (int)sizeof(line), fp) != NULL) {
		if ((p = strchr(line, '\n')) == NULL)
			break;
		*p = '\0';
		if (strcmp(line, WT_TURTLE_MSG) == 0)
			continue;

		WT_ERR(__btree_parse_root(session, line, vp, majorp, minorp));
		goto done;
	}

	if (ferror(fp))
		ret = __wt_errno();
err:	if (ret == 0)
		ret = WT_ERROR;
	__wt_errx(session, "the %s file is corrupted", path);

done:	if (fp != NULL)
		WT_TRET(fclose(fp));
	__wt_free(session, path);

	return (ret);
}

/*
 * __btree_set_turtle --
 *	Set the schema file's root address.
 */
static int
__btree_set_turtle(WT_SESSION_IMPL *session, WT_ITEM *v)
{
	WT_ITEM *buf;
	FILE *fp;
	size_t len;
	int ret;
	const char *path;

	buf = NULL;
	ret = 0;
	path = NULL;

	WT_ERR(__wt_filename(session, WT_SCHEMA_TURTLE_SET, &path));
	WT_ERR_TEST((fp = fopen(path, "w")) == NULL, WT_ERROR);

	WT_RET(__wt_scr_alloc(session, 0, &buf));
	WT_ERR(__wt_buf_fmt(session, buf,
	    "%s\n"
	    "root=%.*s,version=(major=%d,minor=%d)\n", WT_TURTLE_MSG,
	    (int)v->size, (const char *)v->data,
	    WT_BTREE_MAJOR_VERSION, WT_BTREE_MINOR_VERSION));
	len = (size_t)fprintf(fp, "%s", (char *)buf->data);
	if (len != buf->size)
		ret = WT_ERROR;

	WT_TRET(fflush(fp));
	WT_TRET(fclose(fp));

	if (ret == 0)
		ret = __wt_rename(
		    session, WT_SCHEMA_TURTLE_SET, WT_SCHEMA_TURTLE);
	else
		(void)__wt_remove(session, WT_SCHEMA_TURTLE_SET);

err:	if (path != NULL)
		__wt_free(session, path);
	__wt_scr_free(&buf);
	return (ret);
}

/*
 * __btree_parse_root --
 *	Parse a btree config string to extract the version
 */
static int
__btree_parse_root(WT_SESSION_IMPL *session,
    const char *config, const char **vp, int *majorp, int *minorp)
{
	WT_CONFIG_ITEM subv, v;

	*vp = NULL;
	*majorp = *minorp = 0;

	WT_RET(__wt_config_getones(session, config, "version", &v));
	WT_RET(__wt_config_subgets(session, &v, "major", &subv));
	*majorp = (int)subv.val;

	WT_RET(__wt_config_subgets(session, &v, "minor", &subv));
	*minorp = (int)subv.val;

	WT_RET(__wt_config_getones(session, config, "root", &v));
	if (v.len > 0)
		WT_RET(__wt_strndup(session, v.str, v.len, vp));

	return (0);
}

/*
 * __btree_get_root --
 *	Parse a btree config string to extract the version
 */
static int
__btree_get_root(WT_SESSION_IMPL *session,
    const char *filename, const char **vp, int *majorp, int *minorp)
{
	WT_ITEM *key;
	const char *config;
	int ret;

	config = NULL;
	key = NULL;
	*vp = NULL;
	*majorp = *minorp = 0;

	WT_ERR(__wt_scr_alloc(session, 0, &key));
	WT_ERR(__wt_buf_fmt(session, key, "file:%s", filename));
	WT_ERR(__wt_schema_table_read(session, key->data, &config));
	WT_ERR(__btree_parse_root(session, config, vp, majorp, minorp));

err:	__wt_scr_free(&key);
	__wt_free(session, config);
	return (ret == WT_NOTFOUND ? 0 : ret);
}

/*
 * __btree_set_root --
 *	Set a non-schema file's root address.
 */
static int
__btree_set_root(WT_SESSION_IMPL *session, const char *filename, WT_ITEM *v)
{
	WT_ITEM *key, *newv;
	const char *cfg[3], *newcfg;
	int ret;

	key = newv = NULL;
	cfg[0] = newcfg = NULL;

	WT_ERR(__wt_scr_alloc(session, 0, &key));
	WT_ERR(__wt_buf_fmt(session, key, "file:%s", filename));
	WT_ERR(__wt_schema_table_read(session, key->data, &cfg[0]));
	WT_ERR(__wt_scr_alloc(session, 0, &newv));
	WT_ERR(__wt_buf_fmt(session, newv, "root=%.*s",
	    (int)v->size, (const char *)v->data));
	cfg[1] = newv->data;
	cfg[2] = NULL;
	WT_ERR(__wt_config_collapse(session, cfg, &newcfg));
	WT_ERR(__wt_schema_table_update(session, key->data, newcfg));

err:	__wt_scr_free(&key);
	__wt_scr_free(&newv);
	__wt_free(session, cfg[0]);
	__wt_free(session, newcfg);
	return (ret);
}