summaryrefslogtreecommitdiff
path: root/src/block/block_mgr.c
blob: 2d34c8efeacc6908523c7758d342f2cb334da6a5 (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
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

static int
__bm_invalid(WT_SESSION_IMPL *session)
{
	WT_RET_MSG(session, EINVAL, "invalid block manager handle");
}

/*
 * __wt_bm_addr_valid --
 *	Return if an address cookie is valid.
 */
int
__wt_bm_addr_valid(
    WT_SESSION_IMPL *session, const uint8_t *addr, uint32_t addr_size)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_addr_valid(session, block, addr, addr_size));
}

#ifdef HAVE_DIAGNOSTIC
/*
 * __wt_bm_addr_stderr --
 *	Print an address on stderr.
 */
int
__wt_bm_addr_stderr(
    WT_SESSION_IMPL *session, const uint8_t *addr, uint32_t addr_size)
{
	WT_BLOCK *block;
	WT_BUF *buf;
	int ret;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	WT_RET(__wt_scr_alloc(session, 0, &buf));
	ret = __wt_block_addr_string(session, block, buf, addr, addr_size);
	if (ret == 0)
		fprintf(stderr, "%s\n", (char *)buf->data);
	__wt_scr_free(&buf);
	return (ret);
}
#endif

/*
 * __wt_bm_addr_string
 *	Return a printable string representation of an address cookie.
 */
int
__wt_bm_addr_string(WT_SESSION_IMPL *session,
    WT_BUF *buf, const uint8_t *addr, uint32_t addr_size)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (
	    __wt_block_addr_string(session, block, buf, addr, addr_size));
}

/*
 * __wt_bm_create --
 *	Create a new file.
 */
int
__wt_bm_create(WT_SESSION_IMPL *session, const char *filename)
{
	return (__wt_block_create(session, filename));
}

/*
 * __wt_bm_open --
 *	Open a file.
 */
int
__wt_bm_open(WT_SESSION_IMPL *session,
    const char *filename, const char *config, const char *cfg[], int salvage)
{
	return (__wt_block_open(
	    session, filename, config, cfg, salvage, &session->btree->block));
}

/*
 * __wt_bm_close --
 *	Close a file.
 */
int
__wt_bm_close(WT_SESSION_IMPL *session)
{
	WT_BLOCK *block;
	int ret;

	if ((block = session->btree->block) == NULL)
		return (0);

	ret = __wt_block_close(session, block);
	session->btree->block = NULL;

	return (ret);
}

/*
 * __wt_bm_truncate --
 *	Truncate a file.
 */
int
__wt_bm_truncate(WT_SESSION_IMPL *session, const char *filename)
{
	return (__wt_block_truncate(session, filename));
}

/*
 * __wt_bm_free --
 *	Free a block of space to the underlying file.
 */
int
__wt_bm_free(WT_SESSION_IMPL *session, const uint8_t *addr, uint32_t addr_size)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_free_buf(session, block, addr, addr_size));
}

/*
 * __wt_bm_read --
 *	Read a address cookie-referenced block into a buffer.
 */
int
__wt_bm_read(WT_SESSION_IMPL *session,
    WT_BUF *buf, const uint8_t *addr, uint32_t addr_size)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_read_buf(session, block, buf, addr, addr_size));
}

/*
 * __wt_bm_block_header --
 *	Return the size of the block manager's header.
 */
int
__wt_bm_block_header(WT_SESSION_IMPL *session, uint32_t *headerp)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_header(session, block, headerp));
}

/*
 * __wt_bm_write_size --
 *	Return the buffer size required to write a block.
 */
int
__wt_bm_write_size(WT_SESSION_IMPL *session, uint32_t *sizep)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_write_size(session, block, sizep));
}

/*
 * __wt_bm_write --
 *	Write a buffer into a block, returning the block's address cookie.
 */
int
__wt_bm_write(
    WT_SESSION_IMPL *session, WT_BUF *buf, uint8_t *addr, uint32_t *addr_size)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_write_buf(session, block, buf, addr, addr_size));
}

/*
 * __wt_bm_stat --
 *	Block-manager statistics.
 */
int
__wt_bm_stat(WT_SESSION_IMPL *session)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	__wt_block_stat(session, block);
	return (0);
}

/*
 * __wt_bm_salvage_start --
 *	Start a block manager salvage.
 */
int
__wt_bm_salvage_start(WT_SESSION_IMPL *session)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_salvage_start(session, block));
}

/*
 * __wt_bm_salvage_next --
 *	Return the next block from the file.
 */
int
__wt_bm_salvage_next(WT_SESSION_IMPL *session, WT_BUF *buf,
    uint8_t *addr, uint32_t *addr_sizep, uint64_t *write_genp, int *eofp)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_salvage_next(
	    session, block, buf, addr, addr_sizep, write_genp, eofp));
}

/*
 * __wt_bm_salvage_end --
 *	End a block manager salvage.
 */
int
__wt_bm_salvage_end(WT_SESSION_IMPL *session, int success)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_salvage_end(session, block, success));
}

/*
 * __wt_bm_verify_start --
 *	Start a block manager salvage.
 */
int
__wt_bm_verify_start(WT_SESSION_IMPL *session, int *emptyp)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_verify_start(session, block, emptyp));
}

/*
 * __wt_bm_verify_end --
 *	End a block manager salvage.
 */
int
__wt_bm_verify_end(WT_SESSION_IMPL *session)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_verify_end(session, block));
}

/*
 * __wt_bm_verify_addr --
 *	Verify an address.
 */
int
__wt_bm_verify_addr(WT_SESSION_IMPL *session,
     const uint8_t *addr, uint32_t addr_size)
{
	WT_BLOCK *block;

	if ((block = session->btree->block) == NULL)
		return (__bm_invalid(session));

	return (__wt_block_verify_addr(session, block, addr, addr_size));
}