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)
77 u32 block_len = DIV_ROUND_UP(len, info.block_size);
78 struct block_allocation *alloc = allocate_blocks(block_len + 1);
81 struct ext4_extent *extent;
85 error("Failed to allocate %d blocks\n", block_len + 1);
89 int allocation_len = block_allocation_num_regions(alloc);
90 if (allocation_len <= 3) {
91 reduce_allocation(alloc, 1);
93 reserve_oob_blocks(alloc, 1);
94 extent_block = get_oob_block(alloc, 0);
98 struct ext4_extent_header *hdr =
99 (struct ext4_extent_header *)&inode->i_block[0];
100 hdr->eh_magic = EXT4_EXT_MAGIC;
101 hdr->eh_entries = allocation_len;
103 hdr->eh_generation = 0;
106 extent = (struct ext4_extent *)&inode->i_block[3];
108 struct ext4_extent_header *hdr =
109 (struct ext4_extent_header *)&inode->i_block[0];
110 hdr->eh_magic = EXT4_EXT_MAGIC;
113 hdr->eh_generation = 0;
116 struct ext4_extent_idx *idx =
117 (struct ext4_extent_idx *)&inode->i_block[3];
119 idx->ei_leaf_lo = extent_block;
123 u8 *data = calloc(info.block_size, 1);
125 critical_error_errno("calloc");
127 sparse_file_add_data(ext4_sparse_file, data, info.block_size,
130 if (((int)(info.block_size - sizeof(struct ext4_extent_header) /
131 sizeof(struct ext4_extent))) < allocation_len) {
132 error("File size %"PRIu64" is too big to fit in a single extent block\n",
137 hdr = (struct ext4_extent_header *)data;
138 hdr->eh_magic = EXT4_EXT_MAGIC;
139 hdr->eh_entries = allocation_len;
140 hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) /
141 sizeof(struct ext4_extent);
142 hdr->eh_generation = 0;
145 extent = (struct ext4_extent *)(data +
146 sizeof(struct ext4_extent_header));
149 for (; !last_region(alloc); extent++, get_next_region(alloc)) {
153 get_region(alloc, ®ion_block, ®ion_len);
154 extent->ee_block = file_block;
155 extent->ee_len = region_len;
156 extent->ee_start_hi = 0;
157 extent->ee_start_lo = region_block;
158 file_block += region_len;
164 blocks = (u64)block_len * info.block_size / 512;
166 inode->i_flags |= EXT4_EXTENTS_FL;
167 inode->i_size_lo = len;
168 inode->i_size_high = len >> 32;
169 inode->i_blocks_lo = blocks;
170 inode->osd2.linux2.l_i_blocks_high = blocks >> 32;
177 /* Allocates enough blocks to hold len bytes, with backing_len bytes in a data
178 buffer, and connects them to an inode. Returns a pointer to the data
180 u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len,
183 struct block_allocation *alloc;
186 alloc = do_inode_allocate_extents(inode, len);
188 error("failed to allocate extents for %"PRIu64" bytes", len);
193 data = extent_create_backing(alloc, backing_len);
195 error("failed to create backing for %"PRIu64" bytes", backing_len);
203 /* Allocates enough blocks to hold len bytes, queues them to be written
204 from a file, and connects them to an inode. */
205 struct block_allocation* inode_allocate_file_extents(struct ext4_inode *inode, u64 len,
206 const char *filename)
208 struct block_allocation *alloc;
210 alloc = do_inode_allocate_extents(inode, len);
212 error("failed to allocate extents for %"PRIu64" bytes", len);
216 extent_create_backing_file(alloc, len, filename);
220 /* Allocates enough blocks to hold len bytes and connects them to an inode */
221 void inode_allocate_extents(struct ext4_inode *inode, u64 len)
223 struct block_allocation *alloc;
225 alloc = do_inode_allocate_extents(inode, len);
227 error("failed to allocate extents for %"PRIu64" bytes", len);