backed_block.c revision ec0a2e83dc66d67addeb90e83144187691852a3e
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdlib.h> 18 19#include "ext4_utils.h" 20#include "backed_block.h" 21 22struct data_block { 23 u32 block; 24 u32 len; 25 u8 *data; 26 const char *filename; 27 off_t offset; 28 struct data_block *next; 29}; 30 31static struct data_block *data_blocks = NULL; 32static struct data_block *last_used = NULL; 33 34static void queue_db(struct data_block *new_db) 35{ 36 struct data_block *db; 37 38 if (data_blocks == NULL) { 39 data_blocks = new_db; 40 return; 41 } 42 43 if (data_blocks->block > new_db->block) { 44 new_db->next = data_blocks; 45 data_blocks = new_db; 46 return; 47 } 48 49 /* Optimization: blocks are mostly queued in sequence, so save the 50 pointer to the last db that was added, and start searching from 51 there if the next block number is higher */ 52 if (last_used && new_db->block > last_used->block) 53 db = last_used; 54 else 55 db = data_blocks; 56 last_used = new_db; 57 58 for (; db->next && db->next->block < new_db->block; db = db->next) 59 ; 60 61 if (db->next == NULL) { 62 db->next = new_db; 63 } else { 64 new_db->next = db->next; 65 db->next = new_db; 66 } 67} 68 69/* Queues a block of memory to be written to the specified data blocks */ 70void queue_data_block(u8 *data, u32 len, u32 block) 71{ 72 struct data_block *db = malloc(sizeof(struct data_block)); 73 if (db == NULL) 74 critical_error_errno("malloc"); 75 76 db->block = block; 77 db->len = len; 78 db->data = data; 79 db->filename = NULL; 80 db->next = NULL; 81 82 queue_db(db); 83} 84 85/* Queues a chunk of a file on disk to be written to the specified data blocks */ 86void queue_data_file(const char *filename, off_t offset, u32 len, 87 u32 block) 88{ 89 struct data_block *db = malloc(sizeof(struct data_block)); 90 if (db == NULL) 91 critical_error_errno("malloc"); 92 93 db->block = block; 94 db->len = len; 95 db->filename = strdup(filename); 96 db->offset = offset; 97 db->next = NULL; 98 99 queue_db(db); 100} 101 102/* Iterates over the queued data blocks, calling data_func for each contiguous 103 data block, and file_func for each contiguous file block */ 104void for_each_data_block(data_block_callback_t data_func, 105 data_block_file_callback_t file_func, void *priv) 106{ 107 struct data_block *db; 108 u32 last_block = 0; 109 110 for (db = data_blocks; db; db = db->next) { 111 if (db->block < last_block) 112 error("data blocks out of order: %u < %u", db->block, last_block); 113 last_block = db->block + DIV_ROUND_UP(db->len, info.block_size) - 1; 114 115 if (db->filename) 116 file_func(priv, db->block, db->filename, db->offset, db->len); 117 else 118 data_func(priv, db->block, db->data, db->len); 119 } 120} 121 122/* Frees the memory used by the linked list of data blocks */ 123void free_data_blocks() 124{ 125 struct data_block *db = data_blocks; 126 while (db) { 127 struct data_block *next = db->next; 128 free((void*)db->filename); 129 free((void*)db->data); 130 free(db); 131 db = next; 132 } 133} 134