summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/data_writer.c
blob: 43d5851659f3cf2e53383560260c2d5bf8a210d3 (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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * data_writer.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "sqfs/block_processor.h"
#include "data_writer.h"
#include "highlevel.h"
#include "util.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <zlib.h>

#define MK_BLK_SIG(chksum, size) \
	(((uint64_t)(size) << 32) | (uint64_t)(chksum))

#define BLK_SIZE(sig) ((sig) >> 32)

#define INIT_BLOCK_COUNT (128)

typedef struct {
	uint64_t offset;
	uint64_t signature;
} blk_info_t;

typedef struct {
	uint32_t index;
	uint32_t offset;
	uint64_t signature;
} frag_info_t;

struct data_writer_t {
	sqfs_block_t *frag_block;
	sqfs_fragment_t *fragments;
	size_t num_fragments;
	size_t max_fragments;

	size_t devblksz;
	uint64_t start;

	sqfs_block_processor_t *proc;
	sqfs_compressor_t *cmp;
	sqfs_super_t *super;
	sqfs_file_t *file;

	size_t file_start;
	size_t num_blocks;
	size_t max_blocks;
	blk_info_t *blocks;

	size_t frag_list_num;
	size_t frag_list_max;
	frag_info_t *frag_list;

	data_writer_stats_t stats;
};

enum {
	BLK_FIRST_BLOCK = SQFS_BLK_USER,
	BLK_LAST_BLOCK = SQFS_BLK_USER << 1,
	BLK_ALLIGN = SQFS_BLK_USER << 2,
	BLK_FRAGMENT_BLOCK = SQFS_BLK_USER << 3,
};

static int allign_file(data_writer_t *data)
{
	return padd_sqfs(data->file, data->file->get_size(data->file),
			 data->devblksz);
}

static int store_block_location(data_writer_t *data, uint64_t offset,
				uint32_t size, uint32_t chksum)
{
	size_t new_sz;
	void *new;

	if (data->num_blocks == data->max_blocks) {
		new_sz = data->max_blocks * 2;
		new = realloc(data->blocks, sizeof(data->blocks[0]) * new_sz);

		if (new == NULL) {
			perror("growing data block checksum table");
			return -1;
		}

		data->blocks = new;
		data->max_blocks = new_sz;
	}

	data->blocks[data->num_blocks].offset = offset;
	data->blocks[data->num_blocks].signature = MK_BLK_SIG(chksum, size);
	data->num_blocks += 1;
	return 0;
}

static size_t deduplicate_blocks(data_writer_t *data, size_t count)
{
	size_t i, j;

	for (i = 0; i < data->file_start; ++i) {
		for (j = 0; j < count; ++j) {
			if (data->blocks[i + j].signature !=
			    data->blocks[data->file_start + j].signature)
				break;
		}

		if (j == count)
			break;
	}

	return i;
}

static int block_callback(void *user, sqfs_block_t *blk)
{
	file_info_t *fi = blk->user;
	data_writer_t *data = user;
	size_t start, count;
	uint64_t offset;
	uint32_t out;

	if (blk->flags & BLK_FIRST_BLOCK) {
		data->start = data->file->get_size(data->file);
		data->file_start = data->num_blocks;

		if ((blk->flags & BLK_ALLIGN) && allign_file(data) != 0)
			return -1;
	}

	if (blk->size != 0) {
		out = blk->size;
		if (!(blk->flags & SQFS_BLK_IS_COMPRESSED))
			out |= 1 << 24;

		offset = data->file->get_size(data->file);

		if (blk->flags & BLK_FRAGMENT_BLOCK) {
			data->fragments[blk->index].start_offset = htole64(offset);
			data->fragments[blk->index].pad0 = 0;
			data->fragments[blk->index].size = htole32(out);

			data->super->flags &= ~SQFS_FLAG_NO_FRAGMENTS;
			data->super->flags |= SQFS_FLAG_ALWAYS_FRAGMENTS;

			data->stats.frag_blocks_written += 1;
		} else {
			fi->block_size[blk->index] = htole32(out);

			if (store_block_location(data, offset, out,
						 blk->checksum))
				return -1;

			data->stats.blocks_written += 1;
		}

		if (data->file->write_at(data->file, offset,
					 blk->data, blk->size)) {
			return -1;
		}
	}

	if (blk->flags & BLK_LAST_BLOCK) {
		if ((blk->flags & BLK_ALLIGN) && allign_file(data) != 0)
			return -1;

		count = data->num_blocks - data->file_start;
		start = deduplicate_blocks(data, count);

		fi->startblock = data->blocks[start].offset;

		if (start + count < data->file_start) {
			data->num_blocks = data->file_start;

			data->stats.duplicate_blocks += count;

			if (data->file->truncate(data->file, data->start)) {
				perror("truncating squashfs image after "
				       "file deduplication");
				return -1;
			}
		}
	}

	return 0;
}

/*****************************************************************************/

static int flush_fragment_block(data_writer_t *data)
{
	size_t newsz;
	void *new;
	int ret;

	if (data->num_fragments == data->max_fragments) {
		newsz = data->max_fragments ? data->max_fragments * 2 : 16;
		new = realloc(data->fragments,
			      sizeof(data->fragments[0]) * newsz);

		if (new == NULL) {
			perror("appending to fragment table");
			return -1;
		}

		data->max_fragments = newsz;
		data->fragments = new;
	}

	data->frag_block->index = data->num_fragments++;

	ret = sqfs_block_processor_enqueue(data->proc, data->frag_block);
	data->frag_block = NULL;
	return ret;
}

static int handle_fragment(data_writer_t *data, sqfs_block_t *frag)
{
	file_info_t *fi = frag->user;
	size_t i, size, new_sz;
	uint64_t signature;
	void *new;

	frag->checksum = crc32(0, frag->data, frag->size);
	signature = MK_BLK_SIG(frag->checksum, frag->size);

	for (i = 0; i < data->frag_list_num; ++i) {
		if (data->frag_list[i].signature == signature) {
			fi->fragment_offset = data->frag_list[i].offset;
			fi->fragment = data->frag_list[i].index;
			free(frag);

			data->stats.frag_dup += 1;
			return 0;
		}
	}

	if (data->frag_block != NULL) {
		size = data->frag_block->size + frag->size;

		if (size > data->super->block_size) {
			if (flush_fragment_block(data))
				goto fail;
		}
	}

	if (data->frag_block == NULL) {
		size = sizeof(sqfs_block_t) + data->super->block_size;

		data->frag_block = calloc(1, size);
		if (data->frag_block == NULL) {
			perror("creating fragment block");
			goto fail;
		}

		data->frag_block->flags = BLK_FRAGMENT_BLOCK;
	}

	if (data->frag_list_num == data->frag_list_max) {
		new_sz = data->frag_list_max * 2;
		new = realloc(data->frag_list,
			      sizeof(data->frag_list[0]) * new_sz);

		if (new == NULL) {
			perror("growing fragment checksum table");
			return -1;
		}

		data->frag_list = new;
		data->frag_list_max = new_sz;
	}

	data->frag_list[data->frag_list_num].index = data->num_fragments;
	data->frag_list[data->frag_list_num].offset = data->frag_block->size;
	data->frag_list[data->frag_list_num].signature = signature;
	data->frag_list_num += 1;

	fi->fragment_offset = data->frag_block->size;
	fi->fragment = data->num_fragments;

	data->frag_block->flags |= (frag->flags & SQFS_BLK_DONT_COMPRESS);
	memcpy(data->frag_block->data + data->frag_block->size,
	       frag->data, frag->size);

	data->frag_block->size += frag->size;
	free(frag);

	data->stats.frag_count += 1;
	return 0;
fail:
	free(frag);
	return -1;
}

static bool is_zero_block(unsigned char *ptr, size_t size)
{
	return ptr[0] == 0 && memcmp(ptr, ptr + 1, size - 1) == 0;
}

static int add_sentinel_block(data_writer_t *data, file_info_t *fi,
			      uint32_t flags)
{
	sqfs_block_t *blk = calloc(1, sizeof(*blk));

	if (blk == NULL) {
		perror("creating sentinel block");
		return -1;
	}

	blk->user = fi;
	blk->flags = SQFS_BLK_DONT_COMPRESS | SQFS_BLK_DONT_CHECKSUM | flags;

	return sqfs_block_processor_enqueue(data->proc, blk);
}

static sqfs_block_t *create_block(file_info_t *fi, int fd, size_t size,
				  uint32_t flags)
{
	sqfs_block_t *blk = alloc_flex(sizeof(*blk), 1, size);

	if (blk == NULL) {
		perror(fi->input_file);
		return NULL;
	}

	if (fd >= 0) {
		if (read_data(fi->input_file, fd, blk->data, size)) {
			free(blk);
			return NULL;
		}
	}

	blk->size = size;
	blk->user = fi;
	blk->flags = flags;
	return blk;
}

int write_data_from_fd(data_writer_t *data, file_info_t *fi,
		       int infd, int flags)
{
	uint32_t blk_flags = BLK_FIRST_BLOCK;
	uint64_t file_size = fi->size;
	size_t diff, i = 0;
	sqfs_block_t *blk;

	if (flags & DW_DONT_COMPRESS)
		blk_flags |= SQFS_BLK_DONT_COMPRESS;

	if (flags & DW_ALLIGN_DEVBLK)
		blk_flags |= BLK_ALLIGN;

	for (; file_size > 0; file_size -= diff) {
		if (file_size > data->super->block_size) {
			diff = data->super->block_size;
		} else {
			diff = file_size;
		}

		blk = create_block(fi, infd, diff, blk_flags);
		if (blk == NULL)
			return -1;

		blk->index = i++;

		if (is_zero_block(blk->data, blk->size)) {
			data->stats.sparse_blocks += 1;

			fi->block_size[blk->index] = 0;
			free(blk);
			continue;
		}

		if (diff < data->super->block_size &&
		    !(flags & DW_DONT_FRAGMENT)) {
			if (!(blk_flags & (BLK_FIRST_BLOCK | BLK_LAST_BLOCK))) {
				blk_flags |= BLK_LAST_BLOCK;

				if (add_sentinel_block(data, fi, blk_flags)) {
					free(blk);
					return -1;
				}
			}

			if (handle_fragment(data, blk))
				return -1;
		} else {
			if (sqfs_block_processor_enqueue(data->proc, blk))
				return -1;

			blk_flags &= ~BLK_FIRST_BLOCK;
		}
	}

	if (!(blk_flags & (BLK_FIRST_BLOCK | BLK_LAST_BLOCK))) {
		blk_flags |= BLK_LAST_BLOCK;

		if (add_sentinel_block(data, fi, blk_flags))
			return -1;
	}

	data->stats.bytes_read += fi->size;
	data->stats.file_count += 1;
	return 0;
}

static int check_map_valid(const sparse_map_t *map, file_info_t *fi)
{
	const sparse_map_t *m;
	uint64_t offset;

	if (map != NULL) {
		offset = map->offset;

		for (m = map; m != NULL; m = m->next) {
			if (m->offset < offset)
				goto fail_map;
			offset = m->offset + m->count;
		}

		if (offset > fi->size)
			goto fail_map_size;
	}

	return 0;
fail_map_size:
	fprintf(stderr, "%s: sparse file map spans beyond file size\n",
		fi->input_file);
	return -1;
fail_map:
	fprintf(stderr,
		"%s: sparse file map is unordered or self overlapping\n",
		fi->input_file);
	return -1;
}

static int get_sparse_block(sqfs_block_t *blk, file_info_t *fi, int infd,
			    sparse_map_t **sparse_map, uint64_t offset,
			    size_t diff)
{
	sparse_map_t *map = *sparse_map;
	size_t start, count;

	while (map != NULL && map->offset < offset + diff) {
		start = 0;
		count = map->count;

		if (map->offset < offset)
			count -= offset - map->offset;

		if (map->offset > offset)
			start = map->offset - offset;

		if (start + count > diff)
			count = diff - start;

		if (read_data(fi->input_file, infd, blk->data + start, count))
			return -1;

		map = map->next;
	}

	*sparse_map = map;
	return 0;
}

int write_data_from_fd_condensed(data_writer_t *data, file_info_t *fi,
				 int infd, sparse_map_t *map, int flags)
{
	uint32_t blk_flags = BLK_FIRST_BLOCK;
	size_t diff, i = 0;
	sqfs_block_t *blk;
	uint64_t offset;

	if (check_map_valid(map, fi))
		return -1;

	if (flags & DW_DONT_COMPRESS)
		blk_flags |= SQFS_BLK_DONT_COMPRESS;

	if (flags & DW_ALLIGN_DEVBLK)
		blk_flags |= BLK_ALLIGN;

	for (offset = 0; offset < fi->size; offset += diff) {
		if (fi->size - offset > (uint64_t)data->super->block_size) {
			diff = data->super->block_size;
		} else {
			diff = fi->size - offset;
		}

		blk = alloc_flex(sizeof(*blk), 1, diff);
		blk->size = diff;
		blk->index = i++;
		blk->user = fi;
		blk->flags = blk_flags;

		if (get_sparse_block(blk, fi, infd, &map, offset, diff)) {
			free(blk);
			return -1;
		}

		if (is_zero_block(blk->data, blk->size)) {
			data->stats.sparse_blocks += 1;

			fi->block_size[blk->index] = 0;
			free(blk);
			continue;
		}

		if (diff < data->super->block_size &&
		    !(flags & DW_DONT_FRAGMENT)) {
			if (!(blk_flags & (BLK_FIRST_BLOCK | BLK_LAST_BLOCK))) {
				blk_flags |= BLK_LAST_BLOCK;

				if (add_sentinel_block(data, fi, blk_flags)) {
					free(blk);
					return -1;
				}
			}

			if (handle_fragment(data, blk))
				return -1;
		} else {
			if (sqfs_block_processor_enqueue(data->proc, blk))
				return -1;

			blk_flags &= ~BLK_FIRST_BLOCK;
		}
	}

	if (!(blk_flags & (BLK_FIRST_BLOCK | BLK_LAST_BLOCK))) {
		blk_flags |= BLK_LAST_BLOCK;

		if (add_sentinel_block(data, fi, blk_flags))
			return -1;
	}

	data->stats.bytes_read += fi->size;
	data->stats.file_count += 1;
	return 0;
}

data_writer_t *data_writer_create(sqfs_super_t *super, sqfs_compressor_t *cmp,
				  sqfs_file_t *file, size_t devblksize,
				  unsigned int num_jobs, size_t max_backlog)
{
	data_writer_t *data = calloc(1, sizeof(*data));

	if (data == NULL) {
		perror("creating data writer");
		return NULL;
	}

	data->max_blocks = INIT_BLOCK_COUNT;
	data->frag_list_max = INIT_BLOCK_COUNT;

	data->blocks = alloc_array(sizeof(data->blocks[0]),
				   data->max_blocks);

	if (data->blocks == NULL) {
		perror("creating data writer");
		free(data);
		return NULL;
	}

	data->frag_list = alloc_array(sizeof(data->frag_list[0]),
				      data->frag_list_max);

	if (data->frag_list == NULL) {
		perror("creating data writer");
		free(data->blocks);
		free(data);
		return NULL;
	}

	data->proc = sqfs_block_processor_create(super->block_size, cmp,
						 num_jobs, max_backlog, data,
						 block_callback);
	if (data->proc == NULL) {
		perror("creating data block processor");
		free(data->frag_list);
		free(data->blocks);
		free(data);
		return NULL;
	}

	data->cmp = cmp;
	data->super = super;
	data->file = file;
	data->devblksz = devblksize;
	return data;
}

void data_writer_destroy(data_writer_t *data)
{
	sqfs_block_processor_destroy(data->proc);
	free(data->fragments);
	free(data->blocks);
	free(data);
}

int data_writer_write_fragment_table(data_writer_t *data)
{
	uint64_t start;
	size_t size;
	int ret;

	if (data->num_fragments == 0) {
		data->super->fragment_entry_count = 0;
		data->super->fragment_table_start = 0xFFFFFFFFFFFFFFFFUL;
		return 0;
	}

	size = sizeof(data->fragments[0]) * data->num_fragments;
	ret = sqfs_write_table(data->file, data->cmp,
			       data->fragments, size, &start);
	if (ret)
		return -1;

	data->super->fragment_entry_count = data->num_fragments;
	data->super->fragment_table_start = start;
	return 0;
}

int data_writer_sync(data_writer_t *data)
{
	if (data->frag_block != NULL) {
		if (flush_fragment_block(data))
			return -1;
	}

	return sqfs_block_processor_finish(data->proc);
}

data_writer_stats_t *data_writer_get_stats(data_writer_t *data)
{
	return &data->stats;
}