2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ext4_utils.h"
20 #include <sparse/sparse.h>
27 /* Creates data buffers for the first backing_len bytes of a block allocation
28 and queues them to be written */
29 static u8 *extent_create_backing(struct block_allocation *alloc,
32 u8 *data = calloc(backing_len, 1);
34 critical_error_errno("calloc");
37 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
41 get_region(alloc, ®ion_block, ®ion_len);
43 len = min(region_len * info.block_size, backing_len);
45 sparse_file_add_data(ext4_sparse_file, ptr, len, region_block);
53 /* Queues each chunk of a file to be written to contiguous data block
55 static void extent_create_backing_file(struct block_allocation *alloc,
56 u64 backing_len, const char *filename)
59 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
63 get_region(alloc, ®ion_block, ®ion_len);
65 len = min(region_len * info.block_size, backing_len);
67 sparse_file_add_file(ext4_sparse_file, filename, offset, len,
74 static struct block_allocation *do_inode_allocate_extents(
75 struct ext4_inode *inode, u64 len, struct block_allocation *prealloc)
77 u32 block_len = DIV_ROUND_UP(len, info.block_size), prealloc_block_len;
78 struct block_allocation *alloc;
81 struct ext4_extent *extent;
85 alloc = allocate_blocks(block_len + 1);
87 error("Failed to allocate %d blocks\n", block_len + 1);
91 prealloc_block_len = block_allocation_len(prealloc);
92 if (block_len + 1 > prealloc_block_len) {
93 alloc = allocate_blocks(block_len + 1 - prealloc_block_len);
95 error("Failed to allocate %d blocks\n",
96 block_len + 1 - prealloc_block_len);
99 region_list_merge(&prealloc->list, &alloc->list);
105 int allocation_len = block_allocation_num_regions(alloc);
106 if (allocation_len <= 3) {
107 reduce_allocation(alloc, 1);
108 // IMPORTANT: reduce_allocation may have changed allocation
109 // length, otherwise file corruption happens when fs thinks
110 // a block is missing from extent header.
111 allocation_len = block_allocation_num_regions(alloc);
113 reserve_oob_blocks(alloc, 1);
114 extent_block = get_oob_block(alloc, 0);
118 struct ext4_extent_header *hdr =
119 (struct ext4_extent_header *)&inode->i_block[0];
120 hdr->eh_magic = EXT4_EXT_MAGIC;
121 hdr->eh_entries = allocation_len;
123 hdr->eh_generation = 0;
126 extent = (struct ext4_extent *)&inode->i_block[3];
128 struct ext4_extent_header *hdr =
129 (struct ext4_extent_header *)&inode->i_block[0];
130 hdr->eh_magic = EXT4_EXT_MAGIC;
133 hdr->eh_generation = 0;
136 struct ext4_extent_idx *idx =
137 (struct ext4_extent_idx *)&inode->i_block[3];
139 idx->ei_leaf_lo = extent_block;
143 u8 *data = calloc(info.block_size, 1);
145 critical_error_errno("calloc");
147 sparse_file_add_data(ext4_sparse_file, data, info.block_size,
150 if (((int)(info.block_size - sizeof(struct ext4_extent_header) /
151 sizeof(struct ext4_extent))) < allocation_len) {
152 error("File size %"PRIu64" is too big to fit in a single extent block\n",
157 hdr = (struct ext4_extent_header *)data;
158 hdr->eh_magic = EXT4_EXT_MAGIC;
159 hdr->eh_entries = allocation_len;
160 hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) /
161 sizeof(struct ext4_extent);
162 hdr->eh_generation = 0;
165 extent = (struct ext4_extent *)(data +
166 sizeof(struct ext4_extent_header));
169 for (; !last_region(alloc); extent++, get_next_region(alloc)) {
173 get_region(alloc, ®ion_block, ®ion_len);
174 extent->ee_block = file_block;
175 extent->ee_len = region_len;
176 extent->ee_start_hi = 0;
177 extent->ee_start_lo = region_block;
178 file_block += region_len;
184 blocks = (u64)block_len * info.block_size / 512;
186 inode->i_flags |= EXT4_EXTENTS_FL;
187 inode->i_size_lo = len;
188 inode->i_size_high = len >> 32;
189 inode->i_blocks_lo = blocks;
190 inode->osd2.linux2.l_i_blocks_high = blocks >> 32;
197 /* Allocates enough blocks to hold len bytes, with backing_len bytes in a data
198 buffer, and connects them to an inode. Returns a pointer to the data
200 u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len,
203 struct block_allocation *alloc;
206 alloc = do_inode_allocate_extents(inode, len, NULL);
208 error("failed to allocate extents for %"PRIu64" bytes", len);
213 data = extent_create_backing(alloc, backing_len);
215 error("failed to create backing for %"PRIu64" bytes", backing_len);
223 /* Allocates enough blocks to hold len bytes, queues them to be written
224 from a file, and connects them to an inode. */
225 struct block_allocation* inode_allocate_file_extents(struct ext4_inode *inode, u64 len,
226 const char *filename)
228 struct block_allocation *alloc, *prealloc = base_fs_allocations, *prev_prealloc = NULL;
229 // TODO(mkayyash): base_fs_allocations is sorted by filename, consider
230 // storing it in an array and then binary searching for a filename match instead
231 while (prealloc && prealloc->filename != NULL) {
232 if (!strcmp(filename, prealloc->filename)) {
235 prev_prealloc = prealloc;
236 prealloc = prealloc->next;
239 if (!prev_prealloc) {
240 base_fs_allocations = base_fs_allocations->next;
242 prev_prealloc->next = prealloc->next;
244 prealloc->next = NULL;
247 alloc = do_inode_allocate_extents(inode, len, prealloc);
249 error("failed to allocate extents for %"PRIu64" bytes", len);
253 extent_create_backing_file(alloc, len, filename);
257 /* Allocates enough blocks to hold len bytes and connects them to an inode */
258 void inode_allocate_extents(struct ext4_inode *inode, u64 len)
260 struct block_allocation *alloc;
262 alloc = do_inode_allocate_extents(inode, len, NULL);
264 error("failed to allocate extents for %"PRIu64" bytes", len);