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 "ext4_utils.h" 18 #include "backed_block.h" 19 20 #include <stdlib.h> 21 22 struct data_block { 23 u32 block; 24 u32 len; 25 u8 *data; 26 const char *filename; 27 off64_t offset; 28 struct data_block *next; 29 u32 fill_val; 30 u8 fill; 31 u8 pad1; 32 u16 pad2; 33 }; 34 35 static struct data_block *data_blocks = NULL; 36 static struct data_block *last_used = NULL; 37 38 static void queue_db(struct data_block *new_db) 39 { 40 struct data_block *db; 41 42 if (data_blocks == NULL) { 43 data_blocks = new_db; 44 return; 45 } 46 47 if (data_blocks->block > new_db->block) { 48 new_db->next = data_blocks; 49 data_blocks = new_db; 50 return; 51 } 52 53 /* Optimization: blocks are mostly queued in sequence, so save the 54 pointer to the last db that was added, and start searching from 55 there if the next block number is higher */ 56 if (last_used && new_db->block > last_used->block) 57 db = last_used; 58 else 59 db = data_blocks; 60 last_used = new_db; 61 62 for (; db->next && db->next->block < new_db->block; db = db->next) 63 ; 64 65 if (db->next == NULL) { 66 db->next = new_db; 67 } else { 68 new_db->next = db->next; 69 db->next = new_db; 70 } 71 } 72 73 /* Queues a fill block of memory to be written to the specified data blocks */ 74 void queue_fill_block(u32 fill_val, u32 len, u32 block) 75 { 76 struct data_block *db = malloc(sizeof(struct data_block)); 77 if (db == NULL) 78 critical_error_errno("malloc"); 79 80 db->block = block; 81 db->len = len; 82 db->fill = 1; 83 db->fill_val = fill_val; 84 db->data = NULL; 85 db->filename = NULL; 86 db->next = NULL; 87 88 queue_db(db); 89 } 90 91 /* Queues a block of memory to be written to the specified data blocks */ 92 void queue_data_block(u8 *data, u32 len, u32 block) 93 { 94 struct data_block *db = malloc(sizeof(struct data_block)); 95 if (db == NULL) 96 critical_error_errno("malloc"); 97 98 db->block = block; 99 db->len = len; 100 db->data = data; 101 db->filename = NULL; 102 db->fill = 0; 103 db->next = NULL; 104 105 queue_db(db); 106 } 107 108 /* Queues a chunk of a file on disk to be written to the specified data blocks */ 109 void queue_data_file(const char *filename, off64_t offset, u32 len, 110 u32 block) 111 { 112 struct data_block *db = malloc(sizeof(struct data_block)); 113 if (db == NULL) 114 critical_error_errno("malloc"); 115 116 db->block = block; 117 db->len = len; 118 db->filename = strdup(filename); 119 db->offset = offset; 120 db->data = NULL; 121 db->fill = 0; 122 db->next = NULL; 123 124 queue_db(db); 125 } 126 127 /* Iterates over the queued data blocks, calling data_func for each contiguous 128 data block, and file_func for each contiguous file block */ 129 void for_each_data_block(data_block_callback_t data_func, 130 data_block_file_callback_t file_func, 131 data_block_fill_callback_t fill_func, void *priv) 132 { 133 struct data_block *db; 134 u32 last_block = 0; 135 136 for (db = data_blocks; db; db = db->next) { 137 if (db->block < last_block) 138 error("data blocks out of order: %u < %u", db->block, last_block); 139 last_block = db->block + DIV_ROUND_UP(db->len, info.block_size) - 1; 140 141 if (db->filename) 142 file_func(priv, (u64)db->block * info.block_size, db->filename, db->offset, db->len); 143 else if (db->fill) 144 fill_func(priv, (u64)db->block * info.block_size, db->fill_val, db->len); 145 else 146 data_func(priv, (u64)db->block * info.block_size, db->data, db->len); 147 } 148 } 149 150 /* Frees the memory used by the linked list of data blocks */ 151 void free_data_blocks() 152 { 153 if (!data_blocks) return; 154 struct data_block *db = data_blocks; 155 while (db) { 156 struct data_block *next = db->next; 157 free((void*)db->filename); 158 159 // There used to be a free() of db->data here, but it 160 // made the function crash since queue_data_block() is 161 // sometimes passed pointers it can't take ownership of 162 // (like a pointer into the middle of an allocated 163 // block). It's not clear what the queue_data_block 164 // contract is supposed to be, but we'd rather leak 165 // memory than crash. 166 167 free(db); 168 db = next; 169 } 170 data_blocks = NULL; 171 last_used = NULL; 172 } 173