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

#include "wt_internal.h"

/*
 * __block_buffer_to_addr --
 *	Convert a filesystem address cookie into its components, UPDATING the
 * caller's buffer reference so it can be called repeatedly to load a buffer.
 */
static int
__block_buffer_to_addr(WT_BLOCK *block,
    const uint8_t **pp, off_t *offsetp, uint32_t *sizep, uint32_t *cksump)
{
	uint64_t o, s, c;

	WT_RET(__wt_vunpack_uint(pp, 0, &o));
	WT_RET(__wt_vunpack_uint(pp, 0, &s));
	WT_RET(__wt_vunpack_uint(pp, 0, &c));

	/*
	 * To avoid storing large offsets, we minimize the value by subtracting
	 * 512B (the size of the description sector), and then storing a count
	 * of block allocation units.   That implies there is no such thing as
	 * an "invalid" offset though, they could all be valid (other than very
	 * large numbers), which is what we didn't want to store in the first
	 * place.  Use the size: writing a block of size 0 makes no sense, so
	 * that's the out-of-band value.  Once we're out of this function and
	 * are working with a real file offset, size and checksum triplet, there
	 * are invalid offsets, that's simpler than testing sizes of 0 all over
	 * the place.
	 */
	if (s == 0) {
		*offsetp = 0;
		*sizep = *cksump = 0;
	} else {
		*offsetp = (off_t)o * block->allocsize + WT_BLOCK_DESC_SECTOR;
		*sizep = (uint32_t)s * block->allocsize;
		*cksump = (uint32_t)c;
	}
	return (0);
}

/*
 * __wt_block_addr_to_buffer --
 *	Convert the filesystem components into its address cookie.
 */
int
__wt_block_addr_to_buffer(WT_BLOCK *block,
    uint8_t **pp, off_t offset, uint32_t size, uint32_t cksum)
{
	uint64_t o, s, c;

	/* See the comment above: this is the reverse operation. */
	if (size == 0) {
		o = WT_BLOCK_INVALID_OFFSET;
		s = c = 0;
	} else {
		o = (uint64_t)
		    (offset - WT_BLOCK_DESC_SECTOR) / block->allocsize;
		s = size / block->allocsize;
		c = cksum;
	}
	WT_RET(__wt_vpack_uint(pp, 0, o));
	WT_RET(__wt_vpack_uint(pp, 0, s));
	WT_RET(__wt_vpack_uint(pp, 0, c));
	return (0);
}

/*
 * __wt_block_buffer_to_addr --
 *	Convert a filesystem address cookie into its components NOT UPDATING
 * the caller's buffer reference.
 */
int
__wt_block_buffer_to_addr(WT_BLOCK *block,
    const uint8_t *p, off_t *offsetp, uint32_t *sizep, uint32_t *cksump)
{
	return (__block_buffer_to_addr(block, &p, offsetp, sizep, cksump));
}

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

	WT_UNUSED(session);
	WT_UNUSED(addr_size);

	/* Crack the cookie. */
	WT_RET(__wt_block_buffer_to_addr(block, addr, &offset, &size, &cksum));

	/* All we care about is if it's past the end of the file. */
	return (offset + size > block->fh->file_size ? 0 : 1);
}

/*
 * __wt_block_addr_string --
 *	Return a printable string representation of an address cookie.
 */
int
__wt_block_addr_string(WT_SESSION_IMPL *session,
    WT_BLOCK *block, WT_ITEM *buf, const uint8_t *addr, uint32_t addr_size)
{
	off_t offset;
	uint32_t cksum, size;

	WT_UNUSED(addr_size);

	/* Crack the cookie. */
	WT_RET(__wt_block_buffer_to_addr(block, addr, &offset, &size, &cksum));

	/* Printable representation. */
	WT_RET(__wt_buf_fmt(session, buf,
	    "[%" PRIuMAX "-%" PRIuMAX ", %" PRIu32 ", %" PRIu32 "]",
	    (uintmax_t)offset, (uintmax_t)offset + size, size, cksum));

	return (0);
}

/*
 * __wt_block_buffer_to_snapshot --
 *	Convert a filesystem snapshot cookie into its components.
 */
int
__wt_block_buffer_to_snapshot(WT_SESSION_IMPL *session,
    WT_BLOCK *block, const uint8_t *p, WT_BLOCK_SNAPSHOT *si)
{
	uint64_t a;
	const uint8_t **pp;

	si->version = *p++;
	if (si->version != WT_BM_SNAPSHOT_VERSION)
		WT_RET_MSG(session, WT_ERROR, "illegal snapshot address");

	pp = &p;
	WT_RET(__block_buffer_to_addr(block, pp,
	    &si->root_offset, &si->root_size, &si->root_cksum));
	WT_RET(__block_buffer_to_addr(block, pp,
	    &si->alloc.offset, &si->alloc.size, &si->alloc.cksum));
	WT_RET(__block_buffer_to_addr(block, pp,
	    &si->avail.offset, &si->avail.size, &si->avail.cksum));
	WT_RET(__block_buffer_to_addr(block, pp,
	    &si->discard.offset, &si->discard.size, &si->discard.cksum));
	WT_RET(__wt_vunpack_uint(pp, 0, &a));
	si->file_size = (off_t)a;
	WT_RET(__wt_vunpack_uint(pp, 0, &a));
	si->snapshot_size = a;
	WT_RET(__wt_vunpack_uint(pp, 0, &a));
	si->write_gen = a;

	return (0);
}

/*
 * __wt_block_snapshot_to_buffer --
 *	Convert the filesystem components into its snapshot cookie.
 */
int
__wt_block_snapshot_to_buffer(WT_SESSION_IMPL *session,
    WT_BLOCK *block, uint8_t **pp, WT_BLOCK_SNAPSHOT *si)
{
	uint64_t a;

	if (si->version != WT_BM_SNAPSHOT_VERSION)
		WT_RET_MSG(session, WT_ERROR, "illegal snapshot address");

	(*pp)[0] = si->version;
	(*pp)++;

	WT_RET(__wt_block_addr_to_buffer(block, pp,
	    si->root_offset, si->root_size, si->root_cksum));
	WT_RET(__wt_block_addr_to_buffer(block, pp,
	    si->alloc.offset, si->alloc.size, si->alloc.cksum));
	WT_RET(__wt_block_addr_to_buffer(block, pp,
	    si->avail.offset, si->avail.size, si->avail.cksum));
	WT_RET(__wt_block_addr_to_buffer(block, pp,
	    si->discard.offset, si->discard.size, si->discard.cksum));
	a = (uint64_t)si->file_size;
	WT_RET(__wt_vpack_uint(pp, 0, a));
	a = (uint64_t)si->snapshot_size;
	WT_RET(__wt_vpack_uint(pp, 0, a));
	a = si->write_gen;
	WT_RET(__wt_vpack_uint(pp, 0, a));

	return (0);
}