-
Notifications
You must be signed in to change notification settings - Fork 0
/
plus.c
564 lines (483 loc) · 13 KB
/
plus.c
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdbool.h>
#include <linux/types.h>
#include <ploop/ploop_if.h>
#include <ploop/ploop1_image.h>
#include "plus.h"
#define S2B(sec) ((off_t)(sec) << PLOOP1_SECTOR_LOG)
#define PAGE_SIZE 4096
// Size of ploop on-disk image header, in 32-bit words
#define HDR_SIZE_32 16 // sizeof(struct ploop_pvd_header) / sizeof(u32)
static int p_memalign(void **memptr, size_t size)
{
int ret;
ret = posix_memalign(memptr, PAGE_SIZE, size);
if (ret) {
perror("Memory allocation failed, posix_memalign");
}
return ret;
}
static int open_delta(struct plus_image *img, const char *name, int rw)
{
int level = img->level + 1;
int fd = -1;
if (level > img->max_levels) {
fprintf(stderr, "Error: too much levels %d\n", level);
return -1;
}
fd = open(name, (rw ? O_RDWR : O_RDONLY)|O_DIRECT);
if (fd < 0) {
fprintf(stderr, "Can't open \"%s\": %m\n", name);
return -1;
}
// Read the header
int r = read(fd, img->buf, PAGE_SIZE);
if (r != PAGE_SIZE) {
perror("read");
goto err;
}
struct ploop_pvd_header *pvd = (struct ploop_pvd_header *)img->buf;
// Expect ploop disk
if (pvd->m_Type != PRL_IMAGE_COMPRESSED) {
fprintf(stderr, "Image %s doesn't look like a ploop delta file\n", name);
goto err;
}
if (memcmp(pvd->m_Sig, SIGNATURE_STRUCTURED_DISK_V2, sizeof(pvd->m_Sig))) {
if (memcmp(pvd->m_Sig, SIGNATURE_STRUCTURED_DISK_V1, sizeof(pvd->m_Sig))) {
fprintf(stderr, "Image %s is v1 image; not supported\n", name);
}
else {
fprintf(stderr, "Image %s doesn't look like a ploop delta file\n", name);
}
goto err;
}
// Check it's not in use
if (pvd->m_DiskInUse) {
fprintf(stderr, "Image %s is in use\n", name);
goto err;
}
// Figure out some metrics
u32 clusterSize, bdevSize, batSize;
clusterSize = S2B(pvd->m_Sectors);
bdevSize = pvd->m_SizeInSectors_v2 >> (ffs(pvd->m_Sectors) - 1);
batSize = pvd->m_FirstBlockOffset >> (ffs(pvd->m_Sectors) - 1);
if (level == 0) {
// cluster size can be different
if (clusterSize != DEF_CLUSTER) {
// realloc buf
free(img->buf);
if (p_memalign(img->buf, clusterSize)) {
goto err;
}
}
img->clusterSize = clusterSize;
} else {
// sanity check
if (clusterSize != img->clusterSize) {
fprintf(stderr, "Error: img %s got different "
"cluster size %d\n",
name, clusterSize);
goto err;
}
}
struct stat st;
if (fstat(fd, &st)) {
perror("stat");
goto err;
}
// Allocated size, i.e. max (last) addressable cluster in the image
img->allocSize = ((st.st_size + clusterSize - 1) / clusterSize);
// BAT table size
img->batSize = batSize;
printf("== img %s ==\n", name);
printf("level: %2d cluster: %5d bat: %5d bdev: %5d alloc: %5d\n\n",
level, clusterSize, batSize, bdevSize, img->allocSize);
if (bdevSize != img->bdevSize) {
// (re)alloc maps
int lvlsz = sizeof(*img->map_lvl);
int mapsz = sizeof(*img->map_blk);
img->map_lvl = realloc(img->map_lvl, bdevSize * lvlsz);
if (!img->map_lvl) {
perror("realloc");
goto err;
}
img->map_blk = realloc(img->map_blk, bdevSize * mapsz);
if (!img->map_blk) {
perror("realloc");
goto err;
}
if (bdevSize > img->bdevSize) {
// if we grew, zero the new map area
u32 add = bdevSize - img->bdevSize;
memset(img->map_lvl + img->bdevSize, 0, add * lvlsz);
memset(img->map_blk + img->bdevSize, 0, add * mapsz);
}
// save the size
img->bdevSize = bdevSize;
}
// Read the BAT block(s)
u32 idx = 0;
for (u32 b = 0; b < batSize; b++) {
ssize_t r = pread(fd, img->buf, clusterSize, clusterSize * b);
if (r != clusterSize) {
perror("pread");
goto err;
}
// first few BAT entries of the first block are reserved
// for the image header so we need to skip it
int i0 = (b == 0) ? HDR_SIZE_32 : 0;
// fill in the maps
u32 *bat = img->buf;
for (u32 i = i0; i < clusterSize / 4; i++, idx++) {
if (bat[i] == 0) {
continue;
}
// sanity checks
if (idx > bdevSize) { // and non-zero value
fprintf(stderr, "Error: BAT entry beyond "
"block device size "
"(%u -> %u)\n",
idx, bat[i]);
goto err;
}
if (bat[i] >= img->allocSize) {
fprintf(stderr, "Error: BAT entry points "
"past EOF (%u -> %u)\n",
idx, bat[i]);
goto err;
}
if (bat[i] < batSize) {
fprintf(stderr, "Error: BAT entry points "
"to before data blocks "
"(%u -> %u)\n",
idx, bat[i]);
goto err;
}
// assign
img->map_lvl[idx] = level;
img->map_blk[idx] = bat[i];
printf("%3d %5u -> %5u\n", level, idx, bat[i]);
}
}
img->fds[level] = fd;
img->level = level;
return 0;
err:
if (fd >= 0) {
close(fd);
}
return -1;
}
static int close_deltas(struct plus_image *img)
{
int l = img->level;
do {
close(img->fds[l]);
} while (l-- >= 0);
return 0;
}
static void mark_in_use(void *ptr, bool inuse)
{
// Mark the image as either dirty or clean
struct ploop_pvd_header *pvd = (struct ploop_pvd_header *)ptr;
pvd->m_DiskInUse = inuse ? SIGNATURE_DISK_IN_USE : 0;
// FIXME do we need msync?
if (msync(ptr, PAGE_SIZE, MS_SYNC)) {
fprintf(stderr, "%s: msync: %m\n", __func__);
}
}
struct plus_image *plus_open(int count, char **deltas, int mode)
{
// Allocate img
struct plus_image *img = calloc(1, sizeof(struct plus_image));
if (!img) {
return NULL;
}
// Initialize it
img->level = -1;
img->mode = mode;
img->max_levels = count;
img->fds = calloc(count, sizeof(*img->fds));
if (!img->fds) {
goto err;
}
// initial buffer
if (p_memalign(&img->buf, DEF_CLUSTER)) {
goto err;
}
while (count--) {
int rw = count == 0 && mode != O_RDONLY;
if (open_delta(img, *deltas++, rw) < 0) {
goto err;
}
}
// mmap top delta BAT table for efficient writes
if (mode != O_RDONLY) {
int top_level = img->level;
int wfd = img->fds[top_level];
size_t len = img->batSize * img->clusterSize;
const int prot = PROT_READ | PROT_WRITE;
img->wbat = mmap(NULL, len, prot, MAP_SHARED, wfd, 0);
if (img->wbat == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
img->wbat = NULL;
goto err;
}
// Mark the image as dirty
mark_in_use(img->wbat, true);
}
img->max_idx = (img->batSize * img->clusterSize / 4) - HDR_SIZE_32;
printf("Combined map follows:\n");
for (u32 idx = 0; idx < img->bdevSize; idx++) {
if (img->map_blk[idx]) {
printf("%5u -> %2d,%5u\n", idx,
img->map_lvl[idx],
img->map_blk[idx]);
}
}
printf("levels: %2d cluster: %5d bat: %5d (max idx: %5d) bdev: %5d alloc: %5d\n\n",
img->max_levels, img->clusterSize, img->batSize,
img->max_idx, img->bdevSize, img->allocSize);
return img;
err:
plus_close(img);
return NULL;
}
int plus_close(struct plus_image *img)
{
if (!img) {
return 0;
}
if (img->mode != O_RDONLY && img->wbat != NULL) {
// Mark the image as clean
mark_in_use(img->wbat, false);
// unmap the writeable BAT
size_t len = img->batSize * img->clusterSize;
if (munmap(img->wbat, len)) {
fprintf(stderr, "%s: error in munmap: %m\n", __func__);
}
}
free(img->buf);
close_deltas(img);
free(img->map_lvl);
free(img->map_blk);
free(img->fds);
free(img);
return 0;
}
#define MIN(a, b) ((a) < (b) ? (a) : (b))
// Sanity checks common for read and write
static inline int sanity_checks(const char *func,
struct plus_image *img, size_t size, off_t offset, void *buf)
{
if (!img) {
return -EBADF;
}
// Is everything page-aligned?
if (((size_t)buf % PAGE_SIZE) || (size % PAGE_SIZE) || (offset % PAGE_SIZE)) {
fprintf(stderr, "%s: buf, size, or offset unaligned\n", func);
return -EINVAL;
}
// Is it past EOF?
u32 idx = (size + offset) / img->clusterSize;
if (idx > img->bdevSize) {
fprintf(stderr, "%s: offset=%zd size=%zd past EOF\n",
func, size, offset);
return -EINVAL;
}
// is is past BAT table? FIXME: can it ever happen?
if (idx >= img->max_idx) {
// TODO: implement BAT growing on writes?
fprintf(stderr, "%s: offset=%zd size=%zd past BAT\n"
"(TODO: implement BAT growing)\n",
func, size, offset);
return -E2BIG;
}
printf("%s offset=%5zd size=%5zd\n", func, offset, size);
return 0;
}
static int read_block(int fd, void *buf, size_t len, off_t pos)
{
printf("pread(%d, %p, %zd, %zu) = ", fd, buf, len, pos);
ssize_t r = pread(fd, buf, len, pos);
printf("%zd (%m)\n", r);
if ((size_t)r == len) {
return 0;
}
fprintf(stderr, "Error in pread(%d, %p, %zd, %zu) = %zd: %m\n",
fd, buf, len, pos, r);
if (r < 0) { // pread set errno
return -errno;
} else { // partial read, return EIO
return -EIO;
}
}
ssize_t plus_read(struct plus_image *img, size_t size, off_t offset, void *buf)
{
int ret = sanity_checks(__func__, img, size, offset, buf);
if (ret) {
return ret;
}
u32 cluster = img->clusterSize;
size_t got = 0; // How much we have read so far
while (got < size) {
// Cluster number, and offset within it
u32 idx = offset / cluster; // cluster number
u32 off = offset % cluster; // offset within the cluster
u32 len = MIN(cluster - off, size - got); // how much to read
int lvl = img->map_lvl[idx];
int blk = img->map_blk[idx];
printf(" R %5d -> %2d, %5d off=%5d size=%5d\n",
idx, lvl, blk, off, len);
if (blk) {
// do actual read
// offset in the delta file
off_t pos = blk * cluster + off;
int ret = read_block(img->fds[lvl], buf + got, len, pos);
if (ret) {
return ret;
}
}
else {
// just zero out buf
// memset(buf + got, 0, len);
}
got += len;
offset += len;
}
return got;
}
static int write_bat_entry(struct plus_image *img, u32 idx, u32 cluster)
{
u32 *bat = (u32*)img->wbat + HDR_SIZE_32;
if (bat[idx] != 0) {
fprintf(stderr, "%s: unexpected BAT entry %d -> %d\n",
__func__, idx, bat[idx]);
return -1;
}
bat[idx] = cluster;
return 0;
}
ssize_t plus_write(struct plus_image *img, size_t size, off_t offset, void *buf)
{
int ret = sanity_checks(__func__, img, size, offset, buf);
if (ret) {
return ret;
}
if (img->mode == O_RDONLY) {
return -EROFS;
}
u32 cluster = img->clusterSize;
size_t got = 0; // How much have we wrote so far
int top_level = img->level;
int wfd = img->fds[top_level];
u32 allocSize = img->allocSize;
while (got < size) {
// Cluster number, and offset within it
u32 idx = offset / cluster; // cluster number
u32 off = offset % cluster; // offset within the cluster
u32 len = MIN(cluster - off, size - got); // how much to write
int lvl = img->map_lvl[idx];
int blk = img->map_blk[idx];
if (blk && lvl == img->level) {
// top level, existing block, proceed with rewrite
printf(" W %5d -> %2d, %5d off=%5d size=%5d\n",
idx, lvl, blk, off, len);
// offset in the delta file
off_t pos = blk * cluster + off;
printf("pwrite(%d, %p, %d, %zu) = ",
wfd, buf + got, len, pos);
ssize_t r = pwrite(wfd, buf + got, len, pos);
printf("%zd (%m)\n", r);
if (r != len) {
fprintf(stderr, "%s: error in pwrite(%d, %p, %d, %zu) = %zd: %m\n",
__func__, wfd, buf + got, len, pos, r);
if (r < 0) { // pwrite set errno
return -errno;
} else { // wtf just happened? return EIO
return -EIO;
}
}
} else { // Allocate a new cluster
void *wbuf = buf + got;
// 1. Grow image size by one cluster
printf(" G %5d\n", allocSize);
if (ftruncate(wfd, allocSize * cluster)) {
fprintf(stderr, "Error in ftruncate: %m\n");
ret = -errno;
goto err;
}
// 2. Prepare data to be written, note that since
// this is a new cluster we need to write a whole one
if (len < cluster) {
// partial write, need to reconstruct a cluster
wbuf = img->buf;
if (blk) {
// read the old data
ret = read_block(img->fds[lvl], wbuf,
cluster, blk * cluster);
if (ret) {
goto err;
}
} else {
// just zero out the data
memset(wbuf, 0, cluster);
}
// copy the new data in
memcpy(wbuf + off, buf + got, len);
}
// 3. Write the cluster
printf(" W %5d -> %2d, %5d off=%5d size=%5d\n",
idx, top_level, allocSize, allocSize*cluster, cluster);
ssize_t r = pwrite(wfd, wbuf, cluster, allocSize * cluster);
if (r != cluster) {
fprintf(stderr, "Error in pwrite: %m\n");
if (r < 0) {
ret = -errno;
} else {
ret = -EIO;
}
goto err;
}
// FIXME: steps 4 and 5 need to be moved
// to after writing all the data.
// 4. Add a BAT entry to internal table
img->map_lvl[idx] = top_level;
img->map_blk[idx] = allocSize;
// 5. Write the new BAT entry
ret = write_bat_entry(img, idx, allocSize);
if (ret) {
goto err;
}
// 6. Update allocSize
allocSize++;
}
got += len;
offset += len;
}
// All data written successfully, need to write metadata
if (allocSize > img->allocSize) {
// Update the size
img->allocSize = allocSize;
}
return got;
err:
if (allocSize > img->allocSize) {
// FIXME how to undo the mapping?
// ftruncate back to old size
if (ftruncate(wfd, img->allocSize * cluster)) {
fprintf(stderr, "Error in ftruncate: %m\n");
// we already have a (more serious) error,
// so don't overwrite its code
}
}
return ret;
}