| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 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 | * |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 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 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 17 | #include "extent.h" |
| 18 | |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
| Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | |
| Tao Bao | 018ef1b | 2016-10-05 12:44:18 -0700 | [diff] [blame] | 23 | #include <sparse/sparse.h> |
| 24 | |
| 25 | #include "allocate.h" |
| 26 | #include "ext4_utils/ext4_utils.h" |
| Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 27 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 28 | /* Creates data buffers for the first backing_len bytes of a block allocation |
| 29 | and queues them to be written */ |
| 30 | static u8 *extent_create_backing(struct block_allocation *alloc, |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 31 | u64 backing_len) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 32 | { |
| 33 | u8 *data = calloc(backing_len, 1); |
| 34 | if (!data) |
| 35 | critical_error_errno("calloc"); |
| 36 | |
| 37 | u8 *ptr = data; |
| 38 | for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) { |
| 39 | u32 region_block; |
| 40 | u32 region_len; |
| 41 | u32 len; |
| 42 | get_region(alloc, ®ion_block, ®ion_len); |
| 43 | |
| 44 | len = min(region_len * info.block_size, backing_len); |
| 45 | |
| Colin Cross | 782879a | 2014-01-23 13:08:16 -0800 | [diff] [blame] | 46 | sparse_file_add_data(ext4_sparse_file, ptr, len, region_block); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 47 | ptr += len; |
| 48 | backing_len -= len; |
| 49 | } |
| 50 | |
| 51 | return data; |
| 52 | } |
| 53 | |
| 54 | /* Queues each chunk of a file to be written to contiguous data block |
| 55 | regions */ |
| 56 | static void extent_create_backing_file(struct block_allocation *alloc, |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 57 | u64 backing_len, const char *filename) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 58 | { |
| Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 59 | off64_t offset = 0; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 60 | for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) { |
| 61 | u32 region_block; |
| 62 | u32 region_len; |
| 63 | u32 len; |
| 64 | get_region(alloc, ®ion_block, ®ion_len); |
| 65 | |
| 66 | len = min(region_len * info.block_size, backing_len); |
| 67 | |
| Colin Cross | 782879a | 2014-01-23 13:08:16 -0800 | [diff] [blame] | 68 | sparse_file_add_file(ext4_sparse_file, filename, offset, len, |
| Colin Cross | f0ee37f | 2012-04-24 17:48:43 -0700 | [diff] [blame] | 69 | region_block); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 70 | offset += len; |
| 71 | backing_len -= len; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static struct block_allocation *do_inode_allocate_extents( |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 76 | struct ext4_inode *inode, u64 len, struct block_allocation *prealloc) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 77 | { |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 78 | u32 block_len = DIV_ROUND_UP(len, info.block_size), prealloc_block_len; |
| 79 | struct block_allocation *alloc; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 80 | u32 extent_block = 0; |
| 81 | u32 file_block = 0; |
| 82 | struct ext4_extent *extent; |
| 83 | u64 blocks; |
| 84 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 85 | if (!prealloc) { |
| 86 | alloc = allocate_blocks(block_len + 1); |
| 87 | if (alloc == NULL) { |
| 88 | error("Failed to allocate %d blocks\n", block_len + 1); |
| 89 | return NULL; |
| 90 | } |
| 91 | } else { |
| 92 | prealloc_block_len = block_allocation_len(prealloc); |
| 93 | if (block_len + 1 > prealloc_block_len) { |
| 94 | alloc = allocate_blocks(block_len + 1 - prealloc_block_len); |
| 95 | if (alloc == NULL) { |
| 96 | error("Failed to allocate %d blocks\n", |
| 97 | block_len + 1 - prealloc_block_len); |
| 98 | return NULL; |
| 99 | } |
| Mohamad Ayyash | f7124d6 | 2016-04-29 11:14:02 -0700 | [diff] [blame] | 100 | region_list_merge(&prealloc->list, &alloc->list); |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 101 | free(alloc); |
| 102 | } |
| 103 | alloc = prealloc; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int allocation_len = block_allocation_num_regions(alloc); |
| 107 | if (allocation_len <= 3) { |
| 108 | reduce_allocation(alloc, 1); |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 109 | // IMPORTANT: reduce_allocation may have changed allocation |
| 110 | // length, otherwise file corruption happens when fs thinks |
| 111 | // a block is missing from extent header. |
| 112 | allocation_len = block_allocation_num_regions(alloc); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 113 | } else { |
| 114 | reserve_oob_blocks(alloc, 1); |
| 115 | extent_block = get_oob_block(alloc, 0); |
| 116 | } |
| 117 | |
| 118 | if (!extent_block) { |
| 119 | struct ext4_extent_header *hdr = |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 120 | (struct ext4_extent_header *)&inode->i_block[0]; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 121 | hdr->eh_magic = EXT4_EXT_MAGIC; |
| 122 | hdr->eh_entries = allocation_len; |
| 123 | hdr->eh_max = 3; |
| 124 | hdr->eh_generation = 0; |
| 125 | hdr->eh_depth = 0; |
| 126 | |
| 127 | extent = (struct ext4_extent *)&inode->i_block[3]; |
| 128 | } else { |
| 129 | struct ext4_extent_header *hdr = |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 130 | (struct ext4_extent_header *)&inode->i_block[0]; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 131 | hdr->eh_magic = EXT4_EXT_MAGIC; |
| 132 | hdr->eh_entries = 1; |
| 133 | hdr->eh_max = 3; |
| 134 | hdr->eh_generation = 0; |
| 135 | hdr->eh_depth = 1; |
| 136 | |
| 137 | struct ext4_extent_idx *idx = |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 138 | (struct ext4_extent_idx *)&inode->i_block[3]; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 139 | idx->ei_block = 0; |
| 140 | idx->ei_leaf_lo = extent_block; |
| 141 | idx->ei_leaf_hi = 0; |
| 142 | idx->ei_unused = 0; |
| 143 | |
| 144 | u8 *data = calloc(info.block_size, 1); |
| 145 | if (!data) |
| 146 | critical_error_errno("calloc"); |
| 147 | |
| Colin Cross | 782879a | 2014-01-23 13:08:16 -0800 | [diff] [blame] | 148 | sparse_file_add_data(ext4_sparse_file, data, info.block_size, |
| Colin Cross | f0ee37f | 2012-04-24 17:48:43 -0700 | [diff] [blame] | 149 | extent_block); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 150 | |
| 151 | if (((int)(info.block_size - sizeof(struct ext4_extent_header) / |
| 152 | sizeof(struct ext4_extent))) < allocation_len) { |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 153 | error("File size %"PRIu64" is too big to fit in a single extent block\n", |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 154 | len); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | hdr = (struct ext4_extent_header *)data; |
| 159 | hdr->eh_magic = EXT4_EXT_MAGIC; |
| 160 | hdr->eh_entries = allocation_len; |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 161 | hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) / |
| 162 | sizeof(struct ext4_extent); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 163 | hdr->eh_generation = 0; |
| 164 | hdr->eh_depth = 0; |
| 165 | |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 166 | extent = (struct ext4_extent *)(data + |
| 167 | sizeof(struct ext4_extent_header)); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | for (; !last_region(alloc); extent++, get_next_region(alloc)) { |
| 171 | u32 region_block; |
| 172 | u32 region_len; |
| 173 | |
| 174 | get_region(alloc, ®ion_block, ®ion_len); |
| 175 | extent->ee_block = file_block; |
| 176 | extent->ee_len = region_len; |
| 177 | extent->ee_start_hi = 0; |
| 178 | extent->ee_start_lo = region_block; |
| 179 | file_block += region_len; |
| 180 | } |
| 181 | |
| 182 | if (extent_block) |
| 183 | block_len += 1; |
| 184 | |
| 185 | blocks = (u64)block_len * info.block_size / 512; |
| 186 | |
| 187 | inode->i_flags |= EXT4_EXTENTS_FL; |
| 188 | inode->i_size_lo = len; |
| 189 | inode->i_size_high = len >> 32; |
| 190 | inode->i_blocks_lo = blocks; |
| 191 | inode->osd2.linux2.l_i_blocks_high = blocks >> 32; |
| 192 | |
| 193 | rewind_alloc(alloc); |
| 194 | |
| 195 | return alloc; |
| 196 | } |
| 197 | |
| 198 | /* Allocates enough blocks to hold len bytes, with backing_len bytes in a data |
| 199 | buffer, and connects them to an inode. Returns a pointer to the data |
| 200 | buffer. */ |
| 201 | u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len, |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 202 | u64 backing_len) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 203 | { |
| 204 | struct block_allocation *alloc; |
| 205 | u8 *data = NULL; |
| 206 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 207 | alloc = do_inode_allocate_extents(inode, len, NULL); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 208 | if (alloc == NULL) { |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 209 | error("failed to allocate extents for %"PRIu64" bytes", len); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | if (backing_len) { |
| 214 | data = extent_create_backing(alloc, backing_len); |
| 215 | if (!data) |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 216 | error("failed to create backing for %"PRIu64" bytes", backing_len); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | free_alloc(alloc); |
| 220 | |
| 221 | return data; |
| 222 | } |
| 223 | |
| 224 | /* Allocates enough blocks to hold len bytes, queues them to be written |
| 225 | from a file, and connects them to an inode. */ |
| Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 226 | struct block_allocation* inode_allocate_file_extents(struct ext4_inode *inode, u64 len, |
| Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 227 | const char *filename) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 228 | { |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 229 | struct block_allocation *alloc, *prealloc = base_fs_allocations, *prev_prealloc = NULL; |
| 230 | // TODO(mkayyash): base_fs_allocations is sorted by filename, consider |
| 231 | // storing it in an array and then binary searching for a filename match instead |
| 232 | while (prealloc && prealloc->filename != NULL) { |
| 233 | if (!strcmp(filename, prealloc->filename)) { |
| 234 | break; |
| 235 | } |
| 236 | prev_prealloc = prealloc; |
| 237 | prealloc = prealloc->next; |
| 238 | } |
| 239 | if (prealloc) { |
| 240 | if (!prev_prealloc) { |
| 241 | base_fs_allocations = base_fs_allocations->next; |
| 242 | } else { |
| 243 | prev_prealloc->next = prealloc->next; |
| 244 | } |
| 245 | prealloc->next = NULL; |
| 246 | } |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 247 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 248 | alloc = do_inode_allocate_extents(inode, len, prealloc); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 249 | if (alloc == NULL) { |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 250 | error("failed to allocate extents for %"PRIu64" bytes", len); |
| Doug Zongker | 9922135 | 2014-08-12 16:55:56 -0700 | [diff] [blame] | 251 | return NULL; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | extent_create_backing_file(alloc, len, filename); |
| Doug Zongker | 9922135 | 2014-08-12 16:55:56 -0700 | [diff] [blame] | 255 | return alloc; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* Allocates enough blocks to hold len bytes and connects them to an inode */ |
| 259 | void inode_allocate_extents(struct ext4_inode *inode, u64 len) |
| 260 | { |
| 261 | struct block_allocation *alloc; |
| 262 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 263 | alloc = do_inode_allocate_extents(inode, len, NULL); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 264 | if (alloc == NULL) { |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 265 | error("failed to allocate extents for %"PRIu64" bytes", len); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | free_alloc(alloc); |
| 270 | } |