14 #if !defined(ENABLE_LIBSPARSE)
15 static errcode_t sparse_open(const char *name EXT2FS_ATTR((unused)),
16 int flags EXT2FS_ATTR((unused)),
17 io_channel *channel EXT2FS_ATTR((unused)))
19 return EXT2_ET_UNIMPLEMENTED;
21 static errcode_t sparse_close(io_channel channel EXT2FS_ATTR((unused)))
23 return EXT2_ET_UNIMPLEMENTED;
25 static struct struct_io_manager struct_sparse_manager = {
26 .magic = EXT2_ET_MAGIC_IO_MANAGER,
27 .name = "Android sparse I/O Manager",
29 .close = sparse_close,
31 static struct struct_io_manager struct_sparsefd_manager = {
32 .magic = EXT2_ET_MAGIC_IO_MANAGER,
33 .name = "Android sparse fd I/O Manager",
35 .close = sparse_close,
38 #include <sparse/sparse.h>
44 uint64_t blocks_count;
46 struct sparse_file *sparse_file;
50 struct sparse_io_params {
53 uint64_t blocks_count;
54 unsigned int block_size;
57 static errcode_t sparse_write_blk(io_channel channel, unsigned long block,
58 int count, const void *buf);
60 static void free_sparse_blocks(struct sparse_map *sm)
64 for (i = 0; i < sm->blocks_count; ++i)
70 static int sparse_import_segment(void *priv, const void *data, size_t len,
71 unsigned int block, unsigned int nr_blocks)
73 struct sparse_map *sm = priv;
75 /* Ignore chunk headers, only write the data */
76 if (!nr_blocks || len % sm->block_size)
79 return sparse_write_blk(sm->channel, block, nr_blocks, data);
82 static errcode_t io_manager_import_sparse(struct sparse_io_params *params,
83 struct sparse_map *sm, io_channel io)
87 struct sparse_file *sparse_file;
90 fd = open(params->file, O_RDONLY);
97 sparse_file = sparse_file_import(fd, false, false);
103 sm->block_size = sparse_file_block_size(sparse_file);
104 sm->blocks_count = (sparse_file_len(sparse_file, 0, 0) - 1)
105 / sm->block_size + 1;
106 sm->blocks = calloc(sm->blocks_count, sizeof(char*));
111 io->block_size = sm->block_size;
113 retval = sparse_file_foreach_chunk(sparse_file, true, false,
114 sparse_import_segment, sm);
117 free_sparse_blocks(sm);
119 sparse_file_destroy(sparse_file);
126 static errcode_t io_manager_configure(struct sparse_io_params *params,
127 int flags, io_channel io)
131 struct sparse_map *sm = calloc(1, sizeof(*sm));
133 return EXT2_ET_NO_MEMORY;
135 sm->file = params->file;
137 io->private_data = sm;
138 retval = io_manager_import_sparse(params, sm, io);
140 if (!params->block_size || !params->blocks_count) {
144 sm->block_size = params->block_size;
145 sm->blocks_count = params->blocks_count;
146 sm->blocks = calloc(params->blocks_count, sizeof(void*));
148 retval = EXT2_ET_NO_MEMORY;
152 io->block_size = sm->block_size;
153 img_size = (uint64_t)sm->block_size * sm->blocks_count;
155 if (flags & IO_FLAG_RW) {
156 sm->sparse_file = sparse_file_new(sm->block_size, img_size);
157 if (!sm->sparse_file) {
158 retval = EXT2_ET_NO_MEMORY;
161 if (params->fd < 0) {
162 sm->fd = open(params->file, O_CREAT | O_RDWR | O_TRUNC | O_BINARY,
172 sm->sparse_file = NULL;
177 sparse_file_destroy(sm->sparse_file);
179 free_sparse_blocks(sm);
185 static errcode_t sparse_open_channel(struct sparse_io_params *sparse_params,
186 int flags, io_channel *channel)
191 io = calloc(1, sizeof(struct struct_io_channel));
192 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
196 retval = io_manager_configure(sparse_params, flags, io);
206 static errcode_t read_sparse_argv(const char *name, bool is_fd,
207 struct sparse_io_params *sparse_params)
210 sparse_params->fd = -1;
211 sparse_params->block_size = 0;
212 sparse_params->blocks_count = 0;
214 sparse_params->file = malloc(strlen(name) + 1);
215 if (!sparse_params->file) {
216 fprintf(stderr, "failed to alloc %zu\n", strlen(name) + 1);
217 return EXT2_ET_NO_MEMORY;
221 ret = sscanf(name, "(%d):%llu:%u", &sparse_params->fd,
222 (unsigned long long *)&sparse_params->blocks_count,
223 &sparse_params->block_size);
225 ret = sscanf(name, "(%[^)])%*[:]%llu%*[:]%u", sparse_params->file,
226 (unsigned long long *)&sparse_params->blocks_count,
227 &sparse_params->block_size);
231 free(sparse_params->file);
237 static errcode_t sparse_open(const char *name, int flags, io_channel *channel)
240 struct sparse_io_params sparse_params;
242 retval = read_sparse_argv(name, false, &sparse_params);
244 return EXT2_ET_BAD_DEVICE_NAME;
246 retval = sparse_open_channel(&sparse_params, flags, channel);
249 (*channel)->manager = sparse_io_manager;
254 static errcode_t sparsefd_open(const char *name, int flags, io_channel *channel)
257 struct sparse_io_params sparse_params;
259 retval = read_sparse_argv(name, true, &sparse_params);
261 return EXT2_ET_BAD_DEVICE_NAME;
263 retval = sparse_open_channel(&sparse_params, flags, channel);
266 (*channel)->manager = sparsefd_io_manager;
271 static errcode_t sparse_merge_blocks(struct sparse_map *sm, uint64_t start,
276 unsigned int block_size = sm->block_size;
277 errcode_t retval = 0;
279 buf = calloc(num, block_size);
281 fprintf(stderr, "failed to alloc %llu\n",
282 (unsigned long long)num * block_size);
283 return EXT2_ET_NO_MEMORY;
286 for (i = 0; i < num; i++) {
287 memcpy(buf + i * block_size, sm->blocks[start + i] , block_size);
288 free(sm->blocks[start + i]);
289 sm->blocks[start + i] = NULL;
292 /* free_sparse_blocks will release this buf. */
293 sm->blocks[start] = buf;
295 retval = sparse_file_add_data(sm->sparse_file, sm->blocks[start],
296 block_size * num, start);
301 static errcode_t sparse_close_channel(io_channel channel)
304 errcode_t retval = 0;
305 struct sparse_map *sm = channel->private_data;
307 if (sm->sparse_file) {
308 int64_t chunk_start = (sm->blocks[0] == NULL) ? -1 : 0;
309 for (i = 0; i < sm->blocks_count; ++i) {
310 if (!sm->blocks[i] && chunk_start != -1) {
311 retval = sparse_merge_blocks(sm, chunk_start, i - chunk_start);
313 } else if (sm->blocks[i] && chunk_start == -1) {
319 if (chunk_start != -1) {
320 retval = sparse_merge_blocks(sm, chunk_start,
321 sm->blocks_count - chunk_start);
325 retval = sparse_file_write(sm->sparse_file, sm->fd,
326 /*gzip*/0, /*sparse*/1, /*crc*/0);
331 sparse_file_destroy(sm->sparse_file);
332 free_sparse_blocks(sm);
339 static errcode_t sparse_close(io_channel channel)
342 struct sparse_map *sm = channel->private_data;
345 retval = sparse_close_channel(channel);
352 static errcode_t sparse_set_blksize(io_channel channel, int blksize)
354 channel->block_size = blksize;
358 static blk64_t block_to_sparse_block(blk64_t block, blk64_t *offset,
359 io_channel channel, struct sparse_map *sm)
364 ratio = sm->block_size / channel->block_size;
366 *offset = (block % ratio) * channel->block_size;
371 static errcode_t check_block_size(io_channel channel, struct sparse_map *sm)
373 if (sm->block_size >= channel->block_size)
375 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
378 static errcode_t sparse_read_blk64(io_channel channel, blk64_t block,
379 int count, void *buf)
383 blk64_t offset = 0, cur_block;
384 struct sparse_map *sm = channel->private_data;
386 if (check_block_size(channel, sm))
387 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
389 if (count < 0) { //partial read
391 cur_block = block_to_sparse_block(block, &offset, channel, sm);
392 if (sm->blocks[cur_block])
393 memcpy(out, (sm->blocks[cur_block]) + offset, count);
395 memset(out, 0, count);
397 for (i = 0; i < count; ++i) {
398 cur_block = block_to_sparse_block(block + i, &offset,
400 if (sm->blocks[cur_block])
401 memcpy(out + (i * channel->block_size),
402 sm->blocks[cur_block] + offset,
403 channel->block_size);
405 memset(out + (i * channel->block_size), 0,
406 channel->block_size);
412 static errcode_t sparse_read_blk(io_channel channel, unsigned long block,
413 int count, void *buf)
415 return sparse_read_blk64(channel, block, count, buf);
418 static errcode_t sparse_write_blk64(io_channel channel, blk64_t block,
419 int count, const void *buf)
422 blk64_t offset = 0, cur_block;
423 const char *in = buf;
424 struct sparse_map *sm = channel->private_data;
426 if (check_block_size(channel, sm))
427 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
429 if (count < 0) { //partial write
431 cur_block = block_to_sparse_block(block, &offset, channel,
433 if (!sm->blocks[cur_block]) {
434 sm->blocks[cur_block] = calloc(1, sm->block_size);
435 if (!sm->blocks[cur_block])
436 return EXT2_ET_NO_MEMORY;
438 memcpy(sm->blocks[cur_block] + offset, in, count);
440 for (i = 0; i < count; ++i) {
441 if (block + i >= sm->blocks_count)
443 cur_block = block_to_sparse_block(block + i, &offset,
445 if (!sm->blocks[cur_block]) {
446 sm->blocks[cur_block] =
447 calloc(1, sm->block_size);
448 if (!sm->blocks[cur_block])
449 return EXT2_ET_NO_MEMORY;
451 memcpy(sm->blocks[cur_block] + offset,
452 in + (i * channel->block_size),
453 channel->block_size);
459 static errcode_t sparse_write_blk(io_channel channel, unsigned long block,
460 int count, const void *buf)
462 return sparse_write_blk64(channel, block, count, buf);
465 static errcode_t sparse_discard(io_channel channel __attribute__((unused)),
466 blk64_t blk, unsigned long long count)
468 blk64_t cur_block, offset;
469 struct sparse_map *sm = channel->private_data;
471 if (check_block_size(channel, sm))
472 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
474 for (unsigned long long i = 0; i < count; ++i) {
475 if (blk + i >= sm->blocks_count)
477 cur_block = block_to_sparse_block(blk + i, &offset, channel,
479 if (!sm->blocks[cur_block])
481 free(sm->blocks[cur_block]);
482 sm->blocks[cur_block] = NULL;
487 static errcode_t sparse_zeroout(io_channel channel, blk64_t blk,
488 unsigned long long count)
490 return sparse_discard(channel, blk, count);
493 static errcode_t sparse_flush(io_channel channel __attribute__((unused)))
498 static errcode_t sparse_set_option(io_channel channel __attribute__((unused)),
499 const char *option __attribute__((unused)),
500 const char *arg __attribute__((unused)))
505 static errcode_t sparse_cache_readahead(
506 io_channel channel __attribute__((unused)),
507 blk64_t blk __attribute__((unused)),
508 unsigned long long count __attribute__((unused)))
513 static struct struct_io_manager struct_sparse_manager = {
514 .magic = EXT2_ET_MAGIC_IO_MANAGER,
515 .name = "Android sparse I/O Manager",
517 .close = sparse_close,
518 .set_blksize = sparse_set_blksize,
519 .read_blk = sparse_read_blk,
520 .write_blk = sparse_write_blk,
521 .flush = sparse_flush,
523 .set_option = sparse_set_option,
525 .read_blk64 = sparse_read_blk64,
526 .write_blk64 = sparse_write_blk64,
527 .discard = sparse_discard,
528 .cache_readahead = sparse_cache_readahead,
529 .zeroout = sparse_zeroout,
532 static struct struct_io_manager struct_sparsefd_manager = {
533 .magic = EXT2_ET_MAGIC_IO_MANAGER,
534 .name = "Android sparse fd I/O Manager",
535 .open = sparsefd_open,
536 .close = sparse_close,
537 .set_blksize = sparse_set_blksize,
538 .read_blk = sparse_read_blk,
539 .write_blk = sparse_write_blk,
540 .flush = sparse_flush,
542 .set_option = sparse_set_option,
544 .read_blk64 = sparse_read_blk64,
545 .write_blk64 = sparse_write_blk64,
546 .discard = sparse_discard,
547 .cache_readahead = sparse_cache_readahead,
548 .zeroout = sparse_zeroout,
553 io_manager sparse_io_manager = &struct_sparse_manager;
554 io_manager sparsefd_io_manager = &struct_sparsefd_manager;