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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright 2023 Red Hat
*/
#include "io-factory.h"
#include <linux/atomic.h>
#include <linux/blkdev.h>
#include <linux/err.h>
#include <linux/mount.h>
#include "logger.h"
#include "memory-alloc.h"
#include "numeric.h"
/*
* The I/O factory object manages access to index storage, which is a contiguous range of blocks on
* a block device.
*
* The factory holds the open device and is responsible for closing it. The factory has methods to
* make helper structures that can be used to access sections of the index.
*/
struct io_factory {
struct block_device *bdev;
atomic_t ref_count;
};
/* The buffered reader allows efficient I/O by reading page-sized segments into a buffer. */
struct buffered_reader {
struct io_factory *factory;
struct dm_bufio_client *client;
struct dm_buffer *buffer;
sector_t limit;
sector_t block_number;
u8 *start;
u8 *end;
};
#define MAX_READ_AHEAD_BLOCKS 4
/*
* The buffered writer allows efficient I/O by buffering writes and committing page-sized segments
* to storage.
*/
struct buffered_writer {
struct io_factory *factory;
struct dm_bufio_client *client;
struct dm_buffer *buffer;
sector_t limit;
sector_t block_number;
u8 *start;
u8 *end;
int error;
};
static void uds_get_io_factory(struct io_factory *factory)
{
atomic_inc(&factory->ref_count);
}
int uds_make_io_factory(struct block_device *bdev, struct io_factory **factory_ptr)
{
int result;
struct io_factory *factory;
result = vdo_allocate(1, struct io_factory, __func__, &factory);
if (result != VDO_SUCCESS)
return result;
factory->bdev = bdev;
atomic_set_release(&factory->ref_count, 1);
*factory_ptr = factory;
return UDS_SUCCESS;
}
int uds_replace_storage(struct io_factory *factory, struct block_device *bdev)
{
factory->bdev = bdev;
return UDS_SUCCESS;
}
/* Free an I/O factory once all references have been released. */
void uds_put_io_factory(struct io_factory *factory)
{
if (atomic_add_return(-1, &factory->ref_count) <= 0)
vdo_free(factory);
}
size_t uds_get_writable_size(struct io_factory *factory)
{
return bdev_nr_bytes(factory->bdev);
}
/* Create a struct dm_bufio_client for an index region starting at offset. */
int uds_make_bufio(struct io_factory *factory, off_t block_offset, size_t block_size,
unsigned int reserved_buffers, struct dm_bufio_client **client_ptr)
{
struct dm_bufio_client *client;
client = dm_bufio_client_create(factory->bdev, block_size, reserved_buffers, 0,
NULL, NULL, 0);
if (IS_ERR(client))
return -PTR_ERR(client);
dm_bufio_set_sector_offset(client, block_offset * SECTORS_PER_BLOCK);
*client_ptr = client;
return UDS_SUCCESS;
}
static void read_ahead(struct buffered_reader *reader, sector_t block_number)
{
if (block_number < reader->limit) {
sector_t read_ahead = min((sector_t) MAX_READ_AHEAD_BLOCKS,
reader->limit - block_number);
dm_bufio_prefetch(reader->client, block_number, read_ahead);
}
}
void uds_free_buffered_reader(struct buffered_reader *reader)
{
if (reader == NULL)
return;
if (reader->buffer != NULL)
dm_bufio_release(reader->buffer);
dm_bufio_client_destroy(reader->client);
uds_put_io_factory(reader->factory);
vdo_free(reader);
}
/* Create a buffered reader for an index region starting at offset. */
int uds_make_buffered_reader(struct io_factory *factory, off_t offset, u64 block_count,
struct buffered_reader **reader_ptr)
{
int result;
struct dm_bufio_client *client = NULL;
struct buffered_reader *reader = NULL;
result = uds_make_bufio(factory, offset, UDS_BLOCK_SIZE, 1, &client);
if (result != UDS_SUCCESS)
return result;
result = vdo_allocate(1, struct buffered_reader, "buffered reader", &reader);
if (result != VDO_SUCCESS) {
dm_bufio_client_destroy(client);
return result;
}
*reader = (struct buffered_reader) {
.factory = factory,
.client = client,
.buffer = NULL,
.limit = block_count,
.block_number = 0,
.start = NULL,
.end = NULL,
};
read_ahead(reader, 0);
uds_get_io_factory(factory);
*reader_ptr = reader;
return UDS_SUCCESS;
}
static int position_reader(struct buffered_reader *reader, sector_t block_number,
off_t offset)
{
struct dm_buffer *buffer = NULL;
void *data;
if ((reader->end == NULL) || (block_number != reader->block_number)) {
if (block_number >= reader->limit)
return UDS_OUT_OF_RANGE;
if (reader->buffer != NULL)
dm_bufio_release(vdo_forget(reader->buffer));
data = dm_bufio_read(reader->client, block_number, &buffer);
if (IS_ERR(data))
return -PTR_ERR(data);
reader->buffer = buffer;
reader->start = data;
if (block_number == reader->block_number + 1)
read_ahead(reader, block_number + 1);
}
reader->block_number = block_number;
reader->end = reader->start + offset;
return UDS_SUCCESS;
}
static size_t bytes_remaining_in_read_buffer(struct buffered_reader *reader)
{
return (reader->end == NULL) ? 0 : reader->start + UDS_BLOCK_SIZE - reader->end;
}
static int reset_reader(struct buffered_reader *reader)
{
sector_t block_number;
if (bytes_remaining_in_read_buffer(reader) > 0)
return UDS_SUCCESS;
block_number = reader->block_number;
if (reader->end != NULL)
block_number++;
return position_reader(reader, block_number, 0);
}
int uds_read_from_buffered_reader(struct buffered_reader *reader, u8 *data,
size_t length)
{
int result = UDS_SUCCESS;
size_t chunk_size;
while (length > 0) {
result = reset_reader(reader);
if (result != UDS_SUCCESS)
return result;
chunk_size = min(length, bytes_remaining_in_read_buffer(reader));
memcpy(data, reader->end, chunk_size);
length -= chunk_size;
data += chunk_size;
reader->end += chunk_size;
}
return UDS_SUCCESS;
}
/*
* Verify that the next data on the reader matches the required value. If the value matches, the
* matching contents are consumed. If the value does not match, the reader state is unchanged.
*/
int uds_verify_buffered_data(struct buffered_reader *reader, const u8 *value,
size_t length)
{
int result = UDS_SUCCESS;
size_t chunk_size;
sector_t start_block_number = reader->block_number;
int start_offset = reader->end - reader->start;
while (length > 0) {
result = reset_reader(reader);
if (result != UDS_SUCCESS) {
result = UDS_CORRUPT_DATA;
break;
}
chunk_size = min(length, bytes_remaining_in_read_buffer(reader));
if (memcmp(value, reader->end, chunk_size) != 0) {
result = UDS_CORRUPT_DATA;
break;
}
length -= chunk_size;
value += chunk_size;
reader->end += chunk_size;
}
if (result != UDS_SUCCESS)
position_reader(reader, start_block_number, start_offset);
return result;
}
/* Create a buffered writer for an index region starting at offset. */
int uds_make_buffered_writer(struct io_factory *factory, off_t offset, u64 block_count,
struct buffered_writer **writer_ptr)
{
int result;
struct dm_bufio_client *client = NULL;
struct buffered_writer *writer;
result = uds_make_bufio(factory, offset, UDS_BLOCK_SIZE, 1, &client);
if (result != UDS_SUCCESS)
return result;
result = vdo_allocate(1, struct buffered_writer, "buffered writer", &writer);
if (result != VDO_SUCCESS) {
dm_bufio_client_destroy(client);
return result;
}
*writer = (struct buffered_writer) {
.factory = factory,
.client = client,
.buffer = NULL,
.limit = block_count,
.start = NULL,
.end = NULL,
.block_number = 0,
.error = UDS_SUCCESS,
};
uds_get_io_factory(factory);
*writer_ptr = writer;
return UDS_SUCCESS;
}
static size_t get_remaining_write_space(struct buffered_writer *writer)
{
return writer->start + UDS_BLOCK_SIZE - writer->end;
}
static int __must_check prepare_next_buffer(struct buffered_writer *writer)
{
struct dm_buffer *buffer = NULL;
void *data;
if (writer->block_number >= writer->limit) {
writer->error = UDS_OUT_OF_RANGE;
return UDS_OUT_OF_RANGE;
}
data = dm_bufio_new(writer->client, writer->block_number, &buffer);
if (IS_ERR(data)) {
writer->error = -PTR_ERR(data);
return writer->error;
}
writer->buffer = buffer;
writer->start = data;
writer->end = data;
return UDS_SUCCESS;
}
static int flush_previous_buffer(struct buffered_writer *writer)
{
size_t available;
if (writer->buffer == NULL)
return writer->error;
if (writer->error == UDS_SUCCESS) {
available = get_remaining_write_space(writer);
if (available > 0)
memset(writer->end, 0, available);
dm_bufio_mark_buffer_dirty(writer->buffer);
}
dm_bufio_release(writer->buffer);
writer->buffer = NULL;
writer->start = NULL;
writer->end = NULL;
writer->block_number++;
return writer->error;
}
void uds_free_buffered_writer(struct buffered_writer *writer)
{
int result;
if (writer == NULL)
return;
flush_previous_buffer(writer);
result = -dm_bufio_write_dirty_buffers(writer->client);
if (result != UDS_SUCCESS)
vdo_log_warning_strerror(result, "%s: failed to sync storage", __func__);
dm_bufio_client_destroy(writer->client);
uds_put_io_factory(writer->factory);
vdo_free(writer);
}
/*
* Append data to the buffer, writing as needed. If no data is provided, zeros are written instead.
* If a write error occurs, it is recorded and returned on every subsequent write attempt.
*/
int uds_write_to_buffered_writer(struct buffered_writer *writer, const u8 *data,
size_t length)
{
int result = writer->error;
size_t chunk_size;
while ((length > 0) && (result == UDS_SUCCESS)) {
if (writer->buffer == NULL) {
result = prepare_next_buffer(writer);
continue;
}
chunk_size = min(length, get_remaining_write_space(writer));
if (data == NULL) {
memset(writer->end, 0, chunk_size);
} else {
memcpy(writer->end, data, chunk_size);
data += chunk_size;
}
length -= chunk_size;
writer->end += chunk_size;
if (get_remaining_write_space(writer) == 0)
result = uds_flush_buffered_writer(writer);
}
return result;
}
int uds_flush_buffered_writer(struct buffered_writer *writer)
{
if (writer->error != UDS_SUCCESS)
return writer->error;
return flush_previous_buffer(writer);
}
|